Search in sources :

Example 1 with Rotations

use of net.drewke.tdme.engine.Rotations in project tdme by andreasdr.

the class EngineTest method doPlayerControl.

/**
	 * Do player control
	 * @param idx
	 * @param key left
	 * @param key right
	 * @param key up
	 */
private void doPlayerControl(int idx, boolean keyLeft, boolean keyRight, boolean keyUp) {
    float fps = engine.getTiming().getCurrentFPS();
    Object3D player = players.get(idx);
    BoundingVolume playerBoundingVolumeTransformed = playerBoundingVolumesTransformed.get(idx);
    Rotations rotations = player.getRotations();
    Rotation r = rotations.get(0);
    player.update();
    Vector3 movement = new Vector3();
    // left, right
    if (keyRight)
        r.setAngle(r.getAngle() - (135f / fps));
    if (keyLeft)
        r.setAngle(r.getAngle() + (135f / fps));
    if (keyRight || keyLeft) {
        player.update();
        playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
    }
    // forward
    if (keyUp) {
        // apply movement
        r.getQuaternion().multiply(new Vector3(0f, 0f, 1f), movement);
        movement.scale(1.50f / fps);
        player.getTranslation().add(movement);
        player.update();
        playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
        // walking animation
        if (player.getAnimation().equals("walk") == false) {
            player.setAnimation("walk");
        }
    } else {
        // still animation
        if (player.getAnimation().equals("walk") == true) {
            player.setAnimation("still");
        }
    }
    // check if collides with cube
    if (playerBoundingVolumeTransformed.doesCollideWith(cubeBoundingVolumeTransformed, movement, collision) == true && collision.hasPenetration() == true) {
        System.out.println("cube: " + collision);
        // yep, move object out of collision 
        player.getTranslation().sub(collision.getNormal().clone().scale(collision.getPenetration()));
        player.update();
        playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
    }
    // check if collides with barrel
    if (CollisionDetection.getInstance().doCollide((Capsule) playerBoundingVolumeTransformed, (ConvexMesh) barrelBoundingVolumeTransformed, movement, collision) == true && collision.hasPenetration() == true) {
        System.out.println("barrel: " + collision);
        // yep, move object out of collision 
        player.getTranslation().sub(collision.getNormal().clone().scale(collision.getPenetration()));
        player.update();
        playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
    }
    // check if collides with other players
    for (int i = 0; i < players.size(); i++) {
        // do not check with same player
        if (idx == i)
            continue;
        // do collide?
        if (playerBoundingVolumeTransformed.doesCollideWith(playerBoundingVolumesTransformed.get(i), movement, collision) == true && collision.hasPenetration()) {
            System.out.println("player: " + collision);
            // yep, move object out of collision
            player.getTranslation().sub(collision.getNormal().clone().scale(collision.getPenetration()));
            player.update();
            playerBoundingVolumeTransformed.fromBoundingVolumeWithTransformations(playerBoundingVolume, player);
        }
    }
}
Also used : BoundingVolume(net.drewke.tdme.engine.primitives.BoundingVolume) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation) Rotations(net.drewke.tdme.engine.Rotations) Object3D(net.drewke.tdme.engine.Object3D)

Example 2 with Rotations

use of net.drewke.tdme.engine.Rotations in project tdme by andreasdr.

the class World method update.

/**
	 * Update world
	 * @param delta time
	 */
public void update(float deltaTime) {
    // lazy initiate constraints solver
    if (constraintsSolver == null) {
        constraintsSolver = new ConstraintsSolver(rigidBodies);
    }
    // apply gravity
    for (int i = 0; i < rigidBodies.size(); i++) {
        // update rigid body
        RigidBody rigidBody = rigidBodies.get(i);
        // skip on disabled, static
        if (rigidBody.enabled == false) {
            // System.out.println("World::update()::gravity::skipping " + rigidBody.id + "::disabled");
            continue;
        }
        if (rigidBody.isStatic == true) {
            continue;
        }
        // unset sleeping if velocity change occured
        if (rigidBody.checkVelocityChange() == true) {
            rigidBody.awake(true);
        }
        // skip on sleeping
        if (rigidBody.isSleeping == true) {
            continue;
        }
        // add gravity
        rigidBody.addForce(worldPosForce.set(rigidBody.getPosition()).setY(10000f), gravityForce.set(0f, -rigidBody.getMass() * MathTools.g, 0f));
    }
    // do the collision tests,
    // take every rigid body with every other rigid body into account
    int collisionsTests = 0;
    rigidBodyTestedCollisions.clear();
    for (int i = 0; i < rigidBodies.size(); i++) {
        RigidBody rigidBody1 = rigidBodies.get(i);
        // skip on disabled
        if (rigidBody1.enabled == false) {
            // System.out.println("World::update()::collision::skipping " + rigidBody1.id + "::disabled");
            continue;
        }
        /**
			for (int j = 0; j < rigidBodies.size(); j++) {
				RigidBody rigidBody2 = rigidBodies.get(j);
			*/
        int nearObjects = 0;
        // dont test test which had been done in reverse order
        for (RigidBody rigidBody2 : partition.getObjectsNearTo(rigidBody1.cbv)) {
            // skip on disabled
            if (rigidBody2.enabled == false) {
                // System.out.println("World::update()::collision::skipping " + rigidBody2.id + "::disabled");
                continue;
            }
            // skip if both are static
            if (rigidBody1.isStatic == true && rigidBody2.isStatic == true)
                continue;
            // skip on same rigid body
            if (rigidBody1 == rigidBody2)
                continue;
            // skip on rigid body 1 static, 2 non static and sleeping
            if (rigidBody1.isStatic == true && rigidBody2.isStatic == false && rigidBody2.isSleeping == true) {
                continue;
            }
            // skip on rigid body 2 static, 1 non static and sleeping
            if (rigidBody2.isStatic == true && rigidBody1.isStatic == false && rigidBody1.isSleeping == true) {
                continue;
            }
            // check if rigid body 2 want to have collision with rigid body 1
            if (((rigidBody1.typeId & rigidBody2.collisionTypeIds) == rigidBody1.typeId) == false) {
                continue;
            }
            // check if rigid body 1 want to have collision with rigid body 2
            if (((rigidBody2.typeId & rigidBody1.collisionTypeIds) == rigidBody2.typeId) == false) {
                continue;
            }
            //
            nearObjects++;
            // create rigidBody12 key
            Key rigidBodyKey = constraintsSolver.allocateKey();
            rigidBodyKey.reset();
            rigidBodyKey.append(rigidBody1.idx);
            rigidBodyKey.append(",");
            rigidBodyKey.append(rigidBody2.idx);
            // check if collision has been tested already
            if (rigidBodyTestedCollisions.get(rigidBodyKey) != null) {
                constraintsSolver.releaseKey();
                continue;
            }
            /*
				// create rigidbody21 key
				rigidBodyKey.reset();
				rigidBodyKey.append(rigidBody2.idx);
				rigidBodyKey.append(",");
				rigidBodyKey.append(rigidBody1.idx);

				if (rigidBodyTestedCollisions.get(rigidBodyKey) != null) {
					constraintsSolver.releaseKey();
					continue;
				}
				*/
            // nope, add 12 key
            rigidBodyTestedCollisions.put(rigidBodyKey, rigidBodyKey);
            //
            collisionsTests++;
            // determine collision movement
            collisionMovement.set(rigidBody1.movement);
            if (collisionMovement.computeLength() < MathTools.EPSILON) {
                collisionMovement.set(rigidBody2.movement);
                collisionMovement.scale(-1f);
            }
            // do collision test
            if (rigidBody1.cbv.doesCollideWith(rigidBody2.cbv, collisionMovement, collision) == true && collision.hasPenetration() == true) {
                // check for hit point count
                if (collision.getHitPointsCount() == 0)
                    continue;
                // we have a collision, so register it
                Key rigidBodyCollisionKey = rigidBodyCollisionsKeyPoolCurrentFrame.allocate();
                rigidBodyCollisionKey.reset();
                rigidBodyCollisionKey.append(rigidBody1.idx);
                rigidBodyCollisionKey.append(",");
                rigidBodyCollisionKey.append(rigidBody2.idx);
                rigidBodyCollisionsCurrentFrame.put(rigidBodyCollisionKey, rigidBodyCollisionKey);
                // 	on collision begin
                if (rigidBodyCollisionsLastFrame.get(rigidBodyCollisionKey) == null) {
                    rigidBody1.fireOnCollisionBegin(rigidBody2, collision);
                }
                // 	on collision
                rigidBody1.fireOnCollision(rigidBody2, collision);
                // unset sleeping if both non static and colliding
                if (rigidBody1.isStatic == false && rigidBody2.isStatic == false) {
                    rigidBody1.awake(true);
                    rigidBody2.awake(true);
                }
                // add constraint entity
                constraintsSolver.allocateConstraintsEntity().set(rigidBody1, rigidBody2, constraintsSolver.allocateCollision().fromResponse(collision));
            }
        }
    // System.out.println(rigidBody1.id + ":" + nearObjects);
    }
    //	check each collision last frame that disappeared in current frame
    for (Key key : rigidBodyCollisionsLastFrame.getKeysIterator()) {
        Key rigidBodyCollisionKey = rigidBodyCollisionsCurrentFrame.get(key);
        if (rigidBodyCollisionKey == null) {
            char[] keyData = key.getData();
            int rigidBodyIdx1 = 0;
            int rigidBodyIdx2 = 0;
            rigidBodyIdx1 += (int) keyData[0] << 0;
            rigidBodyIdx1 += (int) keyData[1] << 8;
            rigidBodyIdx1 += (int) keyData[2] << 16;
            rigidBodyIdx1 += (int) keyData[3] << 24;
            rigidBodyIdx2 += (int) keyData[5] << 0;
            rigidBodyIdx2 += (int) keyData[6] << 8;
            rigidBodyIdx2 += (int) keyData[7] << 16;
            rigidBodyIdx2 += (int) keyData[8] << 24;
            RigidBody rigidBody1 = rigidBodies.get(rigidBodyIdx1);
            RigidBody rigidBody2 = rigidBodies.get(rigidBodyIdx2);
            rigidBody1.fireOnCollisionEnd(rigidBody2);
        }
    }
    // swap rigid body collisions current and last frame
    Pool<Key> rigidBodyCollisionsKeyPoolTmp = rigidBodyCollisionsKeyPoolLastFrame;
    HashMap<Key, Key> rigidBodyCollisionsTmp = rigidBodyCollisionsLastFrame;
    rigidBodyCollisionsLastFrame = rigidBodyCollisionsCurrentFrame;
    rigidBodyCollisionsKeyPoolLastFrame = rigidBodyCollisionsKeyPoolCurrentFrame;
    rigidBodyCollisionsCurrentFrame = rigidBodyCollisionsTmp;
    rigidBodyCollisionsKeyPoolCurrentFrame = rigidBodyCollisionsKeyPoolTmp;
    // reset current frame
    rigidBodyCollisionsCurrentFrame.clear();
    rigidBodyCollisionsKeyPoolCurrentFrame.reset();
    // do the solving
    constraintsSolver.compute(deltaTime);
    constraintsSolver.updateAllBodies(deltaTime);
    constraintsSolver.reset();
    // update transformations for rigid body 
    for (int i = 0; i < rigidBodies.size(); i++) {
        RigidBody rigidBody = rigidBodies.get(i);
        // skip if enabled and remove partition
        if (rigidBody.enabled == false) {
            partition.removeRigidBody(rigidBody);
            continue;
        }
        // skip on static
        if (rigidBody.isStatic == true || rigidBody.isSleeping == true) {
            continue;
        }
        // set up transformations, keep care that only 3 rotations exists (x, y, z axis)
        Rotations rotations = rigidBody.transformations.getRotations();
        while (rotations.size() > 1) {
            rotations.remove(rotations.size() - 1);
        }
        while (rotations.size() < 1) {
            rotations.add(new Rotation());
        }
        // set up orientation
        rotations.get(0).fromQuaternion(rigidBody.orientation);
        rotations.get(0).getAxix().getArray()[1] *= -1f;
        //	second set up position
        Transformations transformations = rigidBody.transformations;
        transformations.getTranslation().set(rigidBody.position);
        // update
        transformations.update();
        // update bounding volume
        rigidBody.cbv.fromBoundingVolumeWithTransformations(rigidBody.obv, transformations);
        // update partition
        partition.updateRigidBody(rigidBody);
    }
}
Also used : Transformations(net.drewke.tdme.engine.Transformations) Rotation(net.drewke.tdme.engine.Rotation) Key(net.drewke.tdme.utils.Key) Rotations(net.drewke.tdme.engine.Rotations)

Aggregations

Rotation (net.drewke.tdme.engine.Rotation)2 Rotations (net.drewke.tdme.engine.Rotations)2 Object3D (net.drewke.tdme.engine.Object3D)1 Transformations (net.drewke.tdme.engine.Transformations)1 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)1 Vector3 (net.drewke.tdme.math.Vector3)1 Key (net.drewke.tdme.utils.Key)1