package uk.co.flyingsquirrels.models; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.Body; import uk.co.flyingsquirrels.utils.MathUtils; public abstract class Aircraft implements Vehicle { private Body body; private Component wing; private ControllableComponent stab; private ControllableComponent engine; protected void setBody(Body body) { this.body = body; } public void setWing(ControllableComponent wing) { this.wing = wing; } public void setStab(ControllableComponent stab) { this.stab = stab; } public Vec2 getPosition() { return body.getWorldCenter(); } public Vec2 getVelocity() { return body.getLinearVelocity(); } public float getPitch() { return body.getAngle(); } public float getAltitude() { return getPosition().y; } public float getAlpha() { float pitch = getPitch(); Vec2 v = getVelocity(); float airflowDirection = MathUtils.clampAngle(Double.valueOf(Math.atan2(v.y, v.x)).floatValue()); return MathUtils.clampAngle(pitch - airflowDirection); } public void update(float seconds) { engine.update(seconds); wing.update(seconds); stab.update(seconds); Vec2 v = getVelocity(); float airflowDirection = MathUtils.clampAngle(Double.valueOf(Math.atan2(v.y, v.x)).floatValue()); Vec2 wingForce = rotate(wing.getForce(), airflowDirection); Vec2 stabForce = rotate(stab.getForce(), airflowDirection); body.applyForce(wingForce, offset(wing.getLocalPosition())); body.applyForce(stabForce, offset(stab.getLocalPosition())); body.applyTorque(wing.getMoment()); body.applyForce(rotate(engine.getForce(), getPitch()), offset(engine.getLocalPosition())); } public Vec2 rotate(Vec2 vector, float angle) { return MathUtils.rotate(vector, angle); } public Vec2 offset(Vec2 offset) { return getPosition().add(rotate(offset, getPitch())); } public void setPosition(Vec2 position) { body.setXForm(position, getPitch()); } public void setPitch(float pitch) { body.setXForm(getPosition(), pitch); } public void setVelocity(Vec2 velocity) { body.setLinearVelocity(velocity); } public void addEngine(Engine engine) { this.engine = engine; } protected ControllableComponent getStab() { return stab; } protected ControllableComponent getEngine() { return engine; } }