Search in sources :

Example 1 with Force

use of com.b3dgs.lionengine.game.Force in project lionengine by b3dgs.

the class BodyTest method testFps.

/**
 * Test the gravity fps.
 */
@Test
public void testFps() {
    transformable.teleport(0, 6.0);
    body.prepare(object);
    body.setMass(1.0);
    body.setGravity(1.0);
    body.setDesiredFps(50);
    body.setVectors(new Force(0.0, -1.0));
    body.update(1.0);
    Assert.assertEquals(6.0, transformable.getOldY(), UtilTests.PRECISION);
    Assert.assertEquals(4.95, transformable.getY(), UtilTests.PRECISION);
    body.update(0.5);
    Assert.assertEquals(4.95, transformable.getOldY(), 0.01);
    Assert.assertEquals(4.41, transformable.getY(), 0.01);
}
Also used : Force(com.b3dgs.lionengine.game.Force) Test(org.junit.Test)

Example 2 with Force

use of com.b3dgs.lionengine.game.Force in project lionengine by b3dgs.

the class BodyTest method testGravity.

/**
 * Test the gravity on body.
 */
@Test
public void testGravity() {
    transformable.teleport(0, 6.0);
    body.prepare(object);
    body.setMass(2.0);
    body.setDesiredFps(50);
    Assert.assertEquals(2.0, body.getMass(), UtilTests.PRECISION);
    Assert.assertEquals(2.0 * Constant.GRAVITY_EARTH, body.getWeight(), UtilTests.PRECISION);
    body.setGravity(3.0);
    Assert.assertEquals(2.0, body.getMass(), UtilTests.PRECISION);
    Assert.assertEquals(6.0, body.getWeight(), UtilTests.PRECISION);
    body.setGravityMax(8.0);
    body.setVectors(new Force(0.0, 0.0));
    Assert.assertEquals(6.0, transformable.getOldY(), UtilTests.PRECISION);
    Assert.assertEquals(6.0, transformable.getY(), UtilTests.PRECISION);
    body.update(1.0);
    Assert.assertEquals(6.0, transformable.getOldY(), UtilTests.PRECISION);
    Assert.assertEquals(5.85, transformable.getY(), UtilTests.PRECISION);
    body.update(1.0);
    Assert.assertEquals(5.85, transformable.getOldY(), UtilTests.PRECISION);
    Assert.assertEquals(5.55, transformable.getY(), UtilTests.PRECISION);
}
Also used : Force(com.b3dgs.lionengine.game.Force) Test(org.junit.Test)

Example 3 with Force

use of com.b3dgs.lionengine.game.Force in project lionengine by b3dgs.

the class UtilLaunchable method createLaunchable.

/**
 * Create launchable.
 *
 * @param services The services.
 * @param featurable The featurable.
 * @return The launchable.
 */
public static Launchable createLaunchable(Services services, Featurable featurable) {
    featurable.addFeature(new IdentifiableModel());
    featurable.addFeature(new TransformableModel());
    final Launchable launchable = new LaunchableModel();
    launchable.prepare(featurable);
    launchable.setLocation(0.0, 0.0);
    launchable.setVector(new Force(0.0, 1.0));
    return launchable;
}
Also used : TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) Force(com.b3dgs.lionengine.game.Force) IdentifiableModel(com.b3dgs.lionengine.game.feature.IdentifiableModel)

Example 4 with Force

use of com.b3dgs.lionengine.game.Force in project lionengine by b3dgs.

the class LauncherModel method launch.

/**
 * Launch the launchable.
 *
 * @param config The launch configuration.
 * @param initial The fire launch initial direction for force transfer.
 * @param featurable The featurable representing the launchable.
 * @param launchable The launchable to launch.
 */
private void launch(LaunchableConfig config, Direction initial, Featurable featurable, Launchable launchable) {
    int sideX = 1;
    int sideY = 1;
    if (mirror && mirrorable != null) {
        if (mirrorable.is(Mirror.HORIZONTAL)) {
            sideX = -1;
        }
        if (mirrorable.is(Mirror.VERTICAL)) {
            sideY = -1;
        }
        launchable.getFeature(Mirrorable.class).mirror(mirrorable.getMirror());
    }
    final double x = transformable.getX() + (config.getOffsetX() + offsetX) * sideX;
    final double y = transformable.getY() + (config.getOffsetY() + offsetY) * sideY;
    launchable.setLocation(x, y);
    final Force vector = new Force(config.getVector());
    vector.addDirection(1.0, initial);
    final Force v = computeVector(vector);
    if (!v.isZero()) {
        launchable.setVector(v);
    }
    launchable.launch();
    config.getSfx().ifPresent(audioPlayer);
    for (final LaunchableListener listener : listenersLaunchable) {
        listener.notifyFired(launchable);
    }
    handler.add(featurable);
}
Also used : Force(com.b3dgs.lionengine.game.Force) Mirrorable(com.b3dgs.lionengine.game.feature.Mirrorable)

Example 5 with Force

use of com.b3dgs.lionengine.game.Force in project lionengine by b3dgs.

the class LauncherModel method computeVector.

/**
 * Compute the force vector depending of the target.
 *
 * @param vector The initial vector used for launch.
 * @param target The target reference.
 * @return The computed force to reach target.
 */
private Force computeVector(Force vector, Localizable target) {
    final double sx = transformable.getX();
    final double sy = transformable.getY();
    double dx = target.getX();
    double dy = target.getY();
    if (extrapolate && target instanceof Transformable) {
        final Transformable targetTransformable = (Transformable) target;
        final double ray = UtilMath.getDistance(transformable.getX(), transformable.getY(), target.getX(), target.getY());
        dx += (int) ((target.getX() - targetTransformable.getOldX()) / vector.getDirectionHorizontal() * ray);
        dy += (int) ((target.getY() - targetTransformable.getOldY()) / vector.getDirectionVertical() * ray);
    }
    final double dist = Math.max(Math.abs(sx - dx), Math.abs(sy - dy));
    final double vecX = (dx - sx) / dist * vector.getDirectionHorizontal();
    final double vecY = (dy - sy) / dist * vector.getDirectionVertical();
    final Force force = new Force(vector);
    force.setDestination(vecX, vecY);
    return force;
}
Also used : Force(com.b3dgs.lionengine.game.Force) Transformable(com.b3dgs.lionengine.game.feature.Transformable)

Aggregations

Force (com.b3dgs.lionengine.game.Force)23 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)4 Media (com.b3dgs.lionengine.Media)3 Xml (com.b3dgs.lionengine.Xml)3 Test (org.junit.Test)3 Transformable (com.b3dgs.lionengine.game.feature.Transformable)2 TransformableModel (com.b3dgs.lionengine.game.feature.TransformableModel)2 Tile (com.b3dgs.lionengine.game.feature.tile.Tile)2 Configurer (com.b3dgs.lionengine.game.Configurer)1 Handler (com.b3dgs.lionengine.game.feature.Handler)1 IdentifiableModel (com.b3dgs.lionengine.game.feature.IdentifiableModel)1 Mirrorable (com.b3dgs.lionengine.game.feature.Mirrorable)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1