/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.anzix.fsz.player;
import com.ardor3d.bounding.BoundingBox;
import com.ardor3d.math.ColorRGBA;
import com.ardor3d.math.Vector3;
import com.ardor3d.scenegraph.shape.Box;
import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.Transform;
import com.google.inject.Inject;
import javax.vecmath.Vector3f;
import net.anzix.fsz.NodeRepository;
import net.anzix.fsz.eventbus.Event;
import net.anzix.fsz.eventbus.EventListener;
import net.anzix.fsz.eventbus.event.Init;
import net.anzix.fsz.world.MotionStateBind;
import net.anzix.fsz.world.Physics;
/**
*
* @author elek
*/
public class Player implements EventListener {
@Inject
private NodeRepository repository;
@Inject
Physics physics;
Box box;
public void initBox(){
// Create a new box centered at (0,0,0) with width/height/depth of size 10.
box = new Box("Box", new Vector3(0, 0, 0), 5, 5, 5);
// Set a bounding box for frustum culling.
box.setModelBound(new BoundingBox());
// Move the box out from the camera 15 units.
box.setTranslation(new Vector3(0, 0, 0));
// Give the box some nice colors.
box.setSolidColor(ColorRGBA.BLUE);
// Attach the box to the scenegraph root.
}
private void initPhysics(){
// setup the physics
float mass = 10.0f;
// set extent based on box
Vector3f halfExtents = new Vector3f((float) box.getXExtent(),
(float) box.getYExtent(), (float) box.getZExtent());
BoxShape shape = new BoxShape(halfExtents);
// fix local inertia
Vector3f localInertia = new Vector3f(0, 0, 0);
shape.calculateLocalInertia(mass, localInertia);
// set initial transform based on box
Transform startTransform = new Transform();
startTransform.setIdentity();
startTransform.origin.set(box.getTranslation().getXf(), box.getTranslation().getYf(), box.getTranslation().getZf());
MotionStateBind motionState = new MotionStateBind(startTransform);
motionState.setSpatial(box);
// create the RigidBody for our box
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass,
motionState, shape, localInertia);
RigidBody body = new RigidBody(rbInfo);
// body.setRestitution(0.1f);
// body.setFriction(0.50f);
// body.setDamping(0f, 0f);
//body.setUserPointer(box);
// add the box to the world
physics.getWorld().addRigidBody(body);
}
public void onEvent(Event event) {
if (event instanceof Init) {
initBox();
initPhysics();
repository.getNode(NodeRepository.ROOT_NODE).attachChild(box);
}
}
}