Package net.anzix.fsz.world

Source Code of net.anzix.fsz.world.Physics

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.anzix.fsz.world;

import com.ardor3d.scenegraph.shape.Box;
import com.bulletphysics.collision.broadphase.AxisSweep3;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;
import com.google.inject.Singleton;

import javax.vecmath.Vector3f;
import net.anzix.fsz.eventbus.Event;
import net.anzix.fsz.eventbus.EventListener;
import net.anzix.fsz.eventbus.event.Init;

/**
*
* @author elek
*/
@Singleton
public class Physics implements EventListener {

    private DiscreteDynamicsWorld world;

    public void init() {
        Vector3f worldAabbMin = new Vector3f(-10000, -10000, -10000);
        Vector3f worldAabbMax = new Vector3f(10000, 10000, 10000);
        Vector3f gravity = new Vector3f(0, -10, 0);

        DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
        CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
        AxisSweep3 overlappingPairCache = new AxisSweep3(worldAabbMin, worldAabbMax);
        SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();
        world = new DiscreteDynamicsWorld(dispatcher, overlappingPairCache,
                solver, collisionConfiguration);
        world.setGravity(gravity);
        world.getDispatchInfo().allowedCcdPenetration = 0f;
    }

    public void update() {
    }

    public void addFloor(Box floor) {
        float mass = 0f; // make it static
        // create the CollisionShape
        CollisionShape groundShape = new BoxShape(new Vector3f((float) floor.getXExtent(), (float) floor.getYExtent(), (float) floor.getZExtent()));
        // set initial transform based on floor
        Transform groundTransform = new Transform();
        groundTransform.setIdentity();
        groundTransform.origin.set(new Vector3f(floor.getTranslation().getXf(), floor.getTranslation().getYf(), floor.getTranslation().getZf()));
        Vector3f localInertia = new Vector3f(0, 0, 0);
        // let's use DefaultMotionState b/c the floor won't need any movement
        // updates
        DefaultMotionState motionState = new DefaultMotionState(groundTransform);
        // create the RigidBody for our floor
        RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass,
                motionState, groundShape, localInertia);
        RigidBody body = new RigidBody(rbInfo);
        body.setUserPointer(floor);
        // add the floor to the world
        world.addRigidBody(body);
    }

    public void onEvent(Event event) {
        if (event instanceof Init) {
            init();
        }
    }

    public DiscreteDynamicsWorld getWorld() {
        return world;
    }
}
TOP

Related Classes of net.anzix.fsz.world.Physics

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.