Search in sources :

Example 6 with Rotation

use of net.drewke.tdme.engine.Rotation 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 7 with Rotation

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

the class PhysicsTest1 method init.

/*
	 * (non-Javadoc)
	 * @see com.jogamp.opengl.GLEventListener#init(com.jogamp.opengl.GLAutoDrawable)
	 */
public void init(GLAutoDrawable drawable) {
    drawable.getGL().setSwapInterval(0);
    engine.init(drawable);
    Object3D entity;
    // cam
    Camera cam = engine.getCamera();
    cam.setZNear(0.10f);
    cam.setZFar(50.00f);
    cam.getLookFrom().set(0f, 4f * 2.5f, -6f * 2.5f);
    cam.getLookAt().set(0f, 0.0f, 0f);
    cam.computeUpVector(cam.getLookFrom(), cam.getLookAt(), cam.getUpVector());
    // lights
    Light light0 = engine.getLightAt(0);
    light0.getAmbient().set(1.0f, 1.0f, 1.0f, 1.0f);
    light0.getDiffuse().set(0.5f, 0.5f, 0.5f, 1f);
    light0.getSpecular().set(1f, 1f, 1f, 1f);
    light0.getPosition().set(0f, 20000f, 0f, 1f);
    light0.getSpotDirection().set(0f, 0f, 0f).sub(new Vector3(light0.getPosition().getArray()));
    light0.setConstantAttenuation(0.5f);
    light0.setLinearAttenuation(0f);
    light0.setQuadraticAttenuation(0f);
    light0.setSpotExponent(0f);
    light0.setSpotCutOff(180f);
    light0.setEnabled(true);
    // ground
    OrientedBoundingBox ground = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(8f, 1f, 8f));
    Model groundModel = PrimitiveModel.createModel(ground, "ground_model");
    groundModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    groundModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    entity = new Object3D("ground", groundModel);
    // entity.getRotations().add(new Rotation(10f, OrientedBoundingBox.AABB_AXIS_Z.clone()));
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("ground", true, RIGID_TYPEID_STANDARD, entity, ground, 0.5f);
    // side
    OrientedBoundingBox side = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(1f, 16f, 8f));
    Model sideModel = PrimitiveModel.createModel(side, "side_model");
    sideModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    sideModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    // far
    OrientedBoundingBox nearFar = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(8f, 16f, 1f));
    Model nearFarModel = PrimitiveModel.createModel(nearFar, "far_model");
    nearFarModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.8f, 0.8f, 1f);
    nearFarModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 1f, 1f, 1f);
    // far
    entity = new Object3D("far", nearFarModel);
    entity.getTranslation().addZ(+9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("far", true, RIGID_TYPEID_STANDARD, entity, nearFar, 0.5f);
    // near
    entity = new Object3D("near", nearFarModel);
    entity.getTranslation().addZ(-9f);
    entity.getEffectColorMul().set(1f, 1f, 1f, 0f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("near", true, RIGID_TYPEID_STANDARD, entity, nearFar, 0.5f);
    // side left
    entity = new Object3D("sideright", sideModel);
    entity.getTranslation().addX(-9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("sideright", true, RIGID_TYPEID_STANDARD, entity, side, 0.5f);
    // side right
    entity = new Object3D("sideleft", sideModel);
    entity.getTranslation().addX(9f);
    entity.update();
    engine.addEntity(entity);
    world.addStaticRigidBody("sideleft", true, RIGID_TYPEID_STANDARD, entity, side, 0.5f);
    // box
    OrientedBoundingBox box = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(0.6f, 0.6f, 0.6f));
    Model boxModel = PrimitiveModel.createModel(box, "box_model");
    boxModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.5f, 0.5f, 1f);
    boxModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    // boxes
    for (int i = 0; i < BOX_COUNT; i++) {
        entity = new Object3D("box" + i, boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(10f + i * 3.0f);
        entity.getTranslation().addX(-2f + i * 0.1f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + i, true, RIGID_TYPEID_STANDARD, entity, box, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
    // stack
    for (int i = 0; i < BOXSTACK_COUNT; i++) {
        entity = new Object3D("box" + (BOX_COUNT + i), boxModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(1.6f + (i * 1.2f));
        entity.getTranslation().addX(+3f);
        entity.getTranslation().addZ(-5f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("box" + (BOX_COUNT + i), true, RIGID_TYPEID_STANDARD, entity, box, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(box, 100f, 1f, 1f, 1f));
    }
    // sphere
    Sphere sphere = new Sphere(new Vector3(0f, 0f, 0f), 0.4f);
    Model sphereModel = PrimitiveModel.createModel(sphere, "sphere_model");
    sphereModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.5f, 0.8f, 0.8f, 1f);
    sphereModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(0f, 1f, 1f, 1f);
    // spheres
    for (int i = 0; i < SPHERE_COUNT; i++) {
        entity = new Object3D("sphere" + i, sphereModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(12f + (i * 1f));
        entity.getTranslation().addX(0.45f * i - 3f);
        entity.getTranslation().addZ(0.1f * i - 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("sphere" + i, true, RIGID_TYPEID_STANDARD, entity, sphere, 0.75f, 0.4f, 10f, RigidBody.computeInertiaMatrix(sphere, 10f, 1f, 1f, 1f));
    }
    // sphere
    Capsule capsule = new Capsule(new Vector3(0f, 0.5f, 0f), new Vector3(0f, -0.5f, 0f), 0.25f);
    Model capsuleModel = PrimitiveModel.createModel(capsule, "capsule_model");
    capsuleModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(0.8f, 0.0f, 0.8f, 1f);
    capsuleModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 1f, 1f);
    //
    for (int i = 0; i < CAPSULE_COUNT; i++) {
        entity = new Object3D("capsule" + i, capsuleModel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(14f + (i * 2f));
        entity.getTranslation().addX((i * 0.5f));
        // entity.getPivot().set(capsule.getCenter());
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("capsule" + i, true, RIGID_TYPEID_STANDARD, entity, capsule, 0.0f, 0.4f, 3f, RigidBody.computeInertiaMatrix(capsule, 3f, 1f, 1f, 1f));
    }
    // sphere
    //Capsule capsuleBig = new Capsule(
    //	new Vector3(0f,+1f,0f),
    //	new Vector3(0f,-1f,0f),
    //	0.5f
    //);
    OrientedBoundingBox capsuleBig = new OrientedBoundingBox(new Vector3(0f, 0f, 0f), OrientedBoundingBox.AABB_AXIS_X.clone(), OrientedBoundingBox.AABB_AXIS_Y.clone(), OrientedBoundingBox.AABB_AXIS_Z.clone(), new Vector3(0.5f, 1f, 0.5f));
    Model capsuleBigModel = PrimitiveModel.createModel(capsuleBig, "capsulebig_model");
    capsuleBigModel.getMaterials().get("tdme.primitive.material").getAmbientColor().set(1f, 0.8f, 0.8f, 1f);
    capsuleBigModel.getMaterials().get("tdme.primitive.material").getDiffuseColor().set(1f, 0f, 0f, 1f);
    System.out.println(capsuleBig.getCenter());
    //
    entity = new Object3D("capsulebig1", capsuleBigModel);
    entity.setDynamicShadowingEnabled(true);
    entity.getTranslation().addY(5f);
    entity.getTranslation().addX(-2f);
    entity.update();
    engine.addEntity(entity);
    world.addRigidBody("capsulebig1", true, RIGID_TYPEID_STANDARD, entity, capsuleBig, 0f, 1f, 80f, RigidBody.getNoRotationInertiaMatrix());
    //
    entity = new Object3D("capsulebig2", capsuleBigModel);
    entity.setDynamicShadowingEnabled(true);
    entity.getTranslation().addY(5f);
    entity.getTranslation().addX(+2f);
    entity.update();
    engine.addEntity(entity);
    world.addRigidBody("capsulebig2", true, RIGID_TYPEID_STANDARD, entity, capsuleBig, 0f, 1f, 100f, RigidBody.getNoRotationInertiaMatrix());
    try {
        // load barrel, set up bounding volume
        Model _barrel = DAEReader.read("resources/tests/models/barrel", "barrel.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh barrelBoundingVolume = new ConvexMesh(new Object3DModel(_barrel));
        // set up barrel 1 in 3d engine
        entity = new Object3D("barrel1", _barrel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(+4f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("barrel1", true, RIGID_TYPEID_STANDARD, entity, barrelBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(barrelBoundingVolume, 100f, 1f, 1f, 1f));
        // set up barrel 2 in 3d engine
        entity = new Object3D("barrel2", _barrel);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(+6f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("barrel2", true, RIGID_TYPEID_STANDARD, entity, barrelBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(barrelBoundingVolume, 100f, 1f, 1f, 1f));
        // load cone, set up bounding volume
        Model _cone = DAEReader.read("resources/tests/models/cone", "cone.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh coneBoundingVolume = new ConvexMesh(new Object3DModel(_cone));
        // set up cone 1 in 3d engine
        entity = new Object3D("cone1", _cone);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-4f);
        entity.getScale().set(3f, 3f, 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("cone1", true, RIGID_TYPEID_STANDARD, entity, coneBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(coneBoundingVolume, 100f, 1f, 1f, 1f));
        // set up cone 1 in 3d engine
        entity = new Object3D("cone2", _cone);
        entity.setDynamicShadowingEnabled(true);
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-5f);
        entity.getScale().set(3f, 3f, 3f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("cone2", true, RIGID_TYPEID_STANDARD, entity, coneBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(coneBoundingVolume, 100f, 1f, 1f, 1f));
        // load cone, set up bounding volume
        Model _tire = DAEReader.read("resources/tests/models/tire", "tire.dae");
        // _barrel.getImportTransformationsMatrix().scale(2f);
        ConvexMesh tireBoundingVolume = new ConvexMesh(new Object3DModel(_tire));
        // set up tire 1 in 3d engine
        entity = new Object3D("tire1", _tire);
        entity.setDynamicShadowingEnabled(true);
        entity.getRotations().add(new Rotation(90f, new Vector3(1f, 0f, 0f)));
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-4f);
        entity.getTranslation().addZ(-2f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("tire1", true, RIGID_TYPEID_STANDARD, entity, tireBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(tireBoundingVolume, 100f, 1f, 1f, 1f));
        // set up tire 1 in 3d engine
        entity = new Object3D("tire2", _tire);
        entity.setDynamicShadowingEnabled(true);
        entity.getRotations().add(new Rotation(90f, new Vector3(1f, 0f, 0f)));
        entity.getTranslation().addY(5f);
        entity.getTranslation().addX(-6f);
        entity.getTranslation().addZ(-2f);
        entity.getScale().set(2f, 2f, 2f);
        entity.update();
        engine.addEntity(entity);
        world.addRigidBody("tire2", true, RIGID_TYPEID_STANDARD, entity, tireBoundingVolume, 0f, 1f, 100f, RigidBody.computeInertiaMatrix(tireBoundingVolume, 100f, 1f, 1f, 1f));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Vector3(net.drewke.tdme.math.Vector3) Capsule(net.drewke.tdme.engine.primitives.Capsule) ConvexMesh(net.drewke.tdme.engine.primitives.ConvexMesh) Object3DModel(net.drewke.tdme.engine.Object3DModel) Rotation(net.drewke.tdme.engine.Rotation) Object3D(net.drewke.tdme.engine.Object3D) Sphere(net.drewke.tdme.engine.primitives.Sphere) OrientedBoundingBox(net.drewke.tdme.engine.primitives.OrientedBoundingBox) Light(net.drewke.tdme.engine.Light) Model(net.drewke.tdme.engine.model.Model) PrimitiveModel(net.drewke.tdme.engine.primitives.PrimitiveModel) Object3DModel(net.drewke.tdme.engine.Object3DModel) Camera(net.drewke.tdme.engine.Camera)

Example 8 with Rotation

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

the class LevelEditorView method updateGrid.

/**
	 * Update dynamic grid
	 */
private void updateGrid() {
    // check if to display grid
    if (gridEnabled == false)
        return;
    // TODO: use pool
    // check if to move grid
    int centerX = (int) gridCenter.getX();
    int centerZ = (int) gridCenter.getZ();
    int centerLastX = gridCenterLast == null ? centerX : (int) gridCenterLast.getX();
    int centerLastZ = gridCenterLast == null ? centerZ : (int) gridCenterLast.getZ();
    if (gridCenterLast != null && (centerLastX != centerX || centerLastZ != centerZ) == false) {
        return;
    }
    int gridDimensionLeft = GRID_DIMENSION_X + (centerLastX < centerX ? centerX - centerLastX : 0);
    int gridDimensionRight = GRID_DIMENSION_X + (centerLastX > centerX ? centerLastX - centerX : 0);
    int gridDimensionNear = GRID_DIMENSION_Y + (centerLastZ < centerZ ? centerZ - centerLastZ : 0);
    int gridDimensionFar = GRID_DIMENSION_Y + (centerLastZ > centerZ ? centerLastZ - centerZ : 0);
    // create ground plates
    int addedCells = 0;
    int removedCells = 0;
    int reAddedCells = 0;
    for (int gridZ = -gridDimensionNear; gridZ < gridDimensionFar; gridZ++) for (int gridX = -gridDimensionLeft; gridX < gridDimensionRight; gridX++) {
        String objectId = "leveleditor.ground@" + (centerX + gridX) + "," + (centerZ + gridZ);
        Entity _object = engine.getEntity(objectId);
        if (gridX < -GRID_DIMENSION_X || gridX >= GRID_DIMENSION_X || gridZ < -GRID_DIMENSION_Y || gridZ >= GRID_DIMENSION_Y) {
            if (_object != null) {
                engine.removeEntity(objectId);
                removedCells++;
            }
        } else if (_object == null) {
            _object = selectedObjectsById.get(objectId);
            if (_object != null) {
                engine.addEntity(_object);
                reAddedCells++;
            } else {
                _object = new Object3D(objectId, levelEditorGround);
                _object.getRotations().add(new Rotation(0f, level.getRotationOrder().getAxis0()));
                _object.getRotations().add(new Rotation(0f, level.getRotationOrder().getAxis1()));
                _object.getRotations().add(new Rotation(0f, level.getRotationOrder().getAxis2()));
                _object.getTranslation().set(centerX + (float) gridX * groundPlateWidth, gridY - 0.05f, centerZ + (float) gridZ * groundPlateDepth);
                //_object.setDynamicShadowingEnabled(false); // TODO
                _object.setEnabled(true);
                _object.setPickable(true);
                _object.update();
                setStandardObjectColorEffect(_object);
                engine.addEntity(_object);
                addedCells++;
            }
        }
    }
    // System.out.println("readded: " + reAddedCells + ", added: " + addedCells + ", removed: " + removedCells + ", total:" + engine.getEntityCount());
    if (gridCenterLast == null)
        gridCenterLast = new Vector3();
    gridCenterLast.set(gridCenter);
}
Also used : Entity(net.drewke.tdme.engine.Entity) LevelEditorEntity(net.drewke.tdme.tools.shared.model.LevelEditorEntity) FacesEntity(net.drewke.tdme.engine.model.FacesEntity) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation) Object3D(net.drewke.tdme.engine.Object3D)

Example 9 with Rotation

use of net.drewke.tdme.engine.Rotation 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)

Example 10 with Rotation

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

the class EntityBoundingVolumeSubScreenController method onBoundingVolumeObbApply.

/**
	 * On bounding volume OBB apply
	 * @param entity
	 * @param idx
	 */
public void onBoundingVolumeObbApply(LevelEditorEntity entity, int idx) {
    try {
        // rotation axes by rotation angle for x,y,z
        Transformations rotations = new Transformations();
        rotations.getRotations().add(new Rotation(Tools.convertToFloat(boundingvolumeObbRotationZ[idx].getController().getValue().toString()), OrientedBoundingBox.AABB_AXIS_Z));
        rotations.getRotations().add(new Rotation(Tools.convertToFloat(boundingvolumeObbRotationY[idx].getController().getValue().toString()), OrientedBoundingBox.AABB_AXIS_Y));
        rotations.getRotations().add(new Rotation(Tools.convertToFloat(boundingvolumeObbRotationX[idx].getController().getValue().toString()), OrientedBoundingBox.AABB_AXIS_X));
        rotations.update();
        // extract axes from matrix
        Vector3 xAxis = new Vector3();
        Vector3 yAxis = new Vector3();
        Vector3 zAxis = new Vector3();
        rotations.getTransformationsMatrix().getAxes(xAxis, yAxis, zAxis);
        // delegate to view
        view.applyBoundingVolumeObb(entity, idx, Tools.convertToVector3(boundingvolumeObbCenter[idx].getController().getValue().toString()), xAxis, yAxis, zAxis, Tools.convertToVector3(boundingvolumeObbHalfextension[idx].getController().getValue().toString()));
    } catch (NumberFormatException nfe) {
        showErrorPopUp("Warning", "Invalid number entered");
    }
}
Also used : Transformations(net.drewke.tdme.engine.Transformations) Vector3(net.drewke.tdme.math.Vector3) Rotation(net.drewke.tdme.engine.Rotation)

Aggregations

Rotation (net.drewke.tdme.engine.Rotation)14 Vector3 (net.drewke.tdme.math.Vector3)12 Transformations (net.drewke.tdme.engine.Transformations)8 Object3D (net.drewke.tdme.engine.Object3D)6 LevelEditorEntity (net.drewke.tdme.tools.shared.model.LevelEditorEntity)5 Camera (net.drewke.tdme.engine.Camera)4 Model (net.drewke.tdme.engine.model.Model)4 LevelEditorObject (net.drewke.tdme.tools.shared.model.LevelEditorObject)4 File (java.io.File)3 Light (net.drewke.tdme.engine.Light)3 Object3DModel (net.drewke.tdme.engine.Object3DModel)3 BoundingVolume (net.drewke.tdme.engine.primitives.BoundingVolume)3 ConvexMesh (net.drewke.tdme.engine.primitives.ConvexMesh)3 OrientedBoundingBox (net.drewke.tdme.engine.primitives.OrientedBoundingBox)3 PrimitiveModel (net.drewke.tdme.engine.primitives.PrimitiveModel)3 Sphere (net.drewke.tdme.engine.primitives.Sphere)3 IOException (java.io.IOException)2 Entity (net.drewke.tdme.engine.Entity)2 Rotations (net.drewke.tdme.engine.Rotations)2 FacesEntity (net.drewke.tdme.engine.model.FacesEntity)2