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);
}
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);
}
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;
}
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);
}
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;
}
Aggregations