Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package cz.ascaria.borekboritel.controls;
- import com.jme3.bullet.PhysicsSpace;
- import com.jme3.bullet.PhysicsTickListener;
- import com.jme3.bullet.collision.shapes.CollisionShape;
- import com.jme3.bullet.control.PhysicsControl;
- import com.jme3.export.InputCapsule;
- import com.jme3.export.JmeExporter;
- import com.jme3.export.JmeImporter;
- import com.jme3.export.OutputCapsule;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.control.Control;
- import cz.ascaria.bullet.objects.PhysicsSpaceShip;
- import java.io.IOException;
- /**
- *
- * @author Ascaria Quynn
- */
- public class SpaceShipControl extends PhysicsSpaceShip implements PhysicsControl, PhysicsTickListener {
- protected Spatial spatial;
- protected boolean enabled = true;
- protected PhysicsSpace space = null;
- protected boolean added = false;
- protected Vector3f linear = Vector3f.ZERO.clone();
- protected Vector3f torque = Vector3f.ZERO.clone();
- protected Vector3f ascent;
- protected Vector3f accelerate;
- protected Vector3f strafe;
- protected Vector3f yaw;
- protected Vector3f pitch;
- protected Vector3f roll;
- /**
- * Creates a new PhysicsNode with the supplied collision shape
- * @param shape
- */
- public SpaceShipControl(CollisionShape shape) {
- super(shape);
- }
- public SpaceShipControl(CollisionShape shape, float mass) {
- super(shape, mass);
- }
- public boolean isApplyPhysicsLocal() {
- return motionState.isApplyPhysicsLocal();
- }
- /**
- * When set to true, the physics coordinates will be applied to the local
- * translation of the Spatial
- * @param applyPhysicsLocal
- */
- public void setApplyPhysicsLocal(boolean applyPhysicsLocal) {
- motionState.setApplyPhysicsLocal(applyPhysicsLocal);
- }
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
- if (physicsSpace != null) {
- if (enabled && !added) {
- if(spatial != null) {
- setPhysicsLocation(motionState.isApplyPhysicsLocal() ? spatial.getLocalTranslation() : spatial.getWorldTranslation());
- setPhysicsRotation(motionState.isApplyPhysicsLocal() ? spatial.getLocalRotation() : spatial.getWorldRotation());
- }
- physicsSpace.addCollisionObject(this);
- physicsSpace.addTickListener(this);
- added = true;
- } else if (!enabled && added) {
- physicsSpace.removeCollisionObject(this);
- physicsSpace.removeTickListener(this);
- added = false;
- }
- }
- }
- public boolean isEnabled() {
- return enabled;
- }
- public Control cloneForSpatial(Spatial spatial) {
- SpaceShipControl control = new SpaceShipControl(collisionShape, mass);
- control.setAngularFactor(getAngularFactor());
- control.setAngularSleepingThreshold(getAngularSleepingThreshold());
- control.setAngularVelocity(getAngularVelocity());
- control.setCcdMotionThreshold(getCcdMotionThreshold());
- control.setCcdSweptSphereRadius(getCcdSweptSphereRadius());
- control.setCollideWithGroups(getCollideWithGroups());
- control.setCollisionGroup(getCollisionGroup());
- control.setDamping(getLinearDamping(), getAngularDamping());
- control.setFriction(getFriction());
- control.setGravity(getGravity());
- control.setKinematic(isKinematic());
- control.setLinearSleepingThreshold(getLinearSleepingThreshold());
- control.setLinearVelocity(getLinearVelocity());
- control.setPhysicsLocation(getPhysicsLocation());
- control.setPhysicsRotation(getPhysicsRotationMatrix());
- control.setRestitution(getRestitution());
- control.setApplyPhysicsLocal(isApplyPhysicsLocal());
- control.setSpatial(spatial);
- return control;
- }
- public void setSpatial(Spatial spatial) {
- if (getUserObject() == null || getUserObject() == this.spatial) {
- setUserObject(spatial);
- }
- this.spatial = spatial;
- if (spatial == null) {
- if (getUserObject() == spatial) {
- setUserObject(null);
- }
- this.spatial = null;
- this.collisionShape = null;
- return;
- }
- setPhysicsLocation(motionState.isApplyPhysicsLocal() ? spatial.getLocalTranslation() : spatial.getWorldTranslation());
- setPhysicsRotation(motionState.isApplyPhysicsLocal() ? spatial.getLocalRotation() : spatial.getWorldRotation());
- }
- public void setPhysicsSpace(PhysicsSpace space) {
- createSpaceShip(space);
- if (space == null) {
- if (this.space != null) {
- this.space.removeCollisionObject(this);
- this.space.addTickListener(this);
- added = false;
- }
- } else {
- if( this.space == space) {
- return;
- }
- space.addCollisionObject(this);
- space.addTickListener(this);
- added = true;
- }
- this.space = space;
- }
- public PhysicsSpace getPhysicsSpace() {
- return space;
- }
- public void update(float tpf) {
- if (enabled && spatial != null) {
- if (getMotionState().applyTransform(spatial)) {
- spatial.getWorldTransform();
- }
- }
- }
- public void render(RenderManager rm, ViewPort vp) {
- }
- @Override
- public void write(JmeExporter ex) throws IOException {
- super.write(ex);
- OutputCapsule oc = ex.getCapsule(this);
- oc.write(enabled, "enabled", true);
- oc.write(motionState.isApplyPhysicsLocal(), "applyLocalPhysics", false);
- oc.write(spatial, "spatial", null);
- }
- @Override
- public void read(JmeImporter im) throws IOException {
- super.read(im);
- InputCapsule ic = im.getCapsule(this);
- enabled = ic.readBoolean("enabled", true);
- spatial = (Spatial) ic.readSavable("spatial", null);
- motionState.setApplyPhysicsLocal(ic.readBoolean("applyLocalPhysics", false));
- setUserObject(spatial);
- }
- /**
- * Apply ascending (positive value) or descending (negative value) force.
- * @param value Value of the axis, from 0f to 1f.
- */
- public void ascent(float value) {
- if(linear.y == 0f) {
- linear.y = mass * movementPower * value;
- }
- }
- /**
- * Apply accelerating (positive value) or reversing (negative value) force.
- * @param value Value of the axis, from 0f to 1f.
- */
- public void accelerate(float value) {
- if(linear.z == 0f) {
- linear.z = mass * movementPower * value;
- }
- }
- /**
- * Apply strafing force to the left (positive value) or right (negative value) side.
- * @param value Value of the axis, from 0f to 1f.
- */
- public void strafe(float value) {
- if(linear.x == 0f) {
- linear.x = mass * movementPower * value;
- }
- }
- /**
- * Apply yaw torque to the left (positive value) or to the right (negative value)
- * @param value
- */
- public void yaw(float value) {
- if(torque.y == 0f) {
- torque.y = mass * rotationPower * value;
- }
- }
- /**
- * Apply pitch torque to the up (negative value) or down (positive value)
- * @param value
- */
- public void pitch(float value) {
- if(torque.x == 0f) {
- torque.x = mass * rotationPower * value;
- }
- }
- /**
- * Apply roll torque to the left (negative value) or to the right (positive value)
- * @param value
- */
- public void roll(float value) {
- if(torque.z == 0f) {
- torque.z = mass * rotationPower * value;
- }
- }
- /**
- * Method is called before each step, here you apply forces (change the state).
- * @param space
- * @param tpf
- */
- public void prePhysicsTick(PhysicsSpace space, float tpf) {
- // Apply linear force
- if(!linear.equals(Vector3f.ZERO)) {
- applyCentralForce(getPhysicsRotation().mult(linear));
- linear.zero();
- }
- // Apply torque force
- if(!torque.equals(Vector3f.ZERO)) {
- applyTorque(getPhysicsRotation().mult(torque));
- torque.zero();
- }
- }
- /**
- * Method is called after each step, here you poll the results (get the current state).
- * @param space
- * @param tpf
- */
- public void physicsTick(PhysicsSpace space, float tpf) {
- //System.out.println("I TICK!!!!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement