Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simplest JBullet HelloWorld  (Read 6149 times)
0 Members and 1 Guest are viewing this topic.
Offline DranoMax

Junior Newbie





« Posted 2011-06-16 07:06:56 »

Hello everyone.  I hate to ask, but I am in desperate need of a JBullet Tutorial.  I've been reading through everything available online for two weeks now and have yet to figure out how to get this to work.  If there is anyone out there, a saint, even, please could you give an example of how to draw a cube to a JFrame that has all the physics capabilities.  I learn best by looking at code and if I can just have this, I'd be able to go from there. 

So please, if anyone can help, I'd greatly greatly appreciate it!
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2011-06-16 09:41:25 »

Visit http://jbullet.advel.cz/ and download and unzip the latest version, then open "src/com/bulletphysics/demos/basic/BasicDemo.java".  This is a very simple demo of code.  There are many other examples included in the demos package, although some are certainly more advanced.

Offline DranoMax

Junior Newbie





« Reply #2 - Posted 2011-06-16 18:20:49 »

Thank you for the reply, but I have used this one.  The problem is that it is much too abstracted across several classes to be useful.  Plus, I cannot make changes to the BasicDemo source because of how the program requests for an already compiled version of it.  I've done a lot of work with pov-ray, isn't there a way to contain all of the code in just one or two classes?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lhkbob

JGO Knight


Medals: 32



« Reply #3 - Posted 2011-06-16 21:40:48 »

One or two ridiculously large classes.  Combining creating windows, 3D rendering and physics requires lots of classes to be useful.  It is considered good practice to separate concerns into different classes (like window management, rendering, and physics).  It might make it more difficult to learn right away but it is a skill you will have to learn at some point.

If the BasicDemo is too much for you, don't try to understand everything and just use its code for rendering and creating windows.  Or move to a simpler project until you're more comfortable working with large code bases.

Offline tom
« Reply #4 - Posted 2011-06-17 19:33:08 »

I agree with DranoMax. The example code sucks. In many ways.

I´ve got a stripped example at work. Can´t find a copy on the net, so if I remember I´ll post it on monday. It uses Java3D, but that should not matter much.

Offline tom
« Reply #5 - Posted 2011-06-20 16:20:44 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
import com.bulletphysics.collision.broadphase.AxisSweep3;
import com.bulletphysics.collision.broadphase.BroadphaseInterface;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.StaticPlaneShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.dynamics.constraintsolver.ConstraintSolver;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.linearmath.MotionState;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;

/**
 * Demonstrates JBullet with Java3D. Shows a box falling on plane. Scene can be
 * reset by pressing the R key.
 */

public class BouncingBox implements FrameListener {

    // contains all the physics objects and performs the simulation
   protected DynamicsWorld dynamicsWorld = null;

    // TransformGroups to the bouncing box and the non moving ground
   private TransformGroup boxTG;
    private TransformGroup groundTG;

    // size of bouncing box
   private static final float BOX_DIM = 0.3f;


    /**
     * Starts the application
     */

    public static void main(String[] args) throws Exception {
        new BouncingBox();
    }

    /**
     * Shows a JFrame containing a 3d canvas showing a box bouncing on a plane.
     */

    public BouncingBox() {
        Canvas3D canvas3D = initJava3DAndCreateScene();

        initPhysics();

        // reset the physics scene when R key is pressed
       canvas3D.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.VK_R) {
                    resetScene();
                }
            }
        });

        // show a JFrame containing the Canvas3D
       JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(BorderLayout.CENTER, canvas3D);
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    /**
     * Initialises Java3D and does the following:
     * - positions the view
     * - creates a box and ground
     * - creates a point light where the view is located
     * - creates a behavior that invokes the nextFrame method every frame
     * - adds the objects to the universe
     * @return the Java3D canvas that can be added to a frame
     */

    private Canvas3D initJava3DAndCreateScene() {
        // create canvas and universe
       Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3D);
        simpleUniverse.getViewer().getView().setFieldOfView(Math.toRadians(90));

        // position view
       TransformGroup viewTG = simpleUniverse.getViewingPlatform()
                .getMultiTransformGroup().getTransformGroup(0);
        Transform3D viewT3D = new Transform3D();
        Point3d viewPos = new Point3d(-5, 4, 1);
        Point3d viewTarget = new Point3d(0, 1, 0);
        Vector3d up = new Vector3d(0, 1, 0);
        viewT3D.lookAt(viewPos, viewTarget, up);
        viewT3D.invert();
        viewTG.setTransform(viewT3D);

        // use appearance with default white material
       Appearance app = new Appearance();
        app.setMaterial(new Material());

        // create a box with BOX_DIM as half dimensions located above ground
       Transform3D boxT3D = new Transform3D();
        boxT3D.rotX((float) Math.toRadians(60));
        boxT3D.setTranslation(new Vector3f(0.75f, 3, 0));
        boxTG = new TransformGroup(boxT3D);
        boxTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        boxTG.addChild(new Box(BOX_DIM, BOX_DIM, BOX_DIM, app));

        // create a large flat box to represent the ground plane
       groundTG = new TransformGroup();
        groundTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        groundTG.addChild(new Box(4, .0001f, 4, app));

        // create white point light at view position
       Color3f white = new Color3f(Color.WHITE);
        Point3f attenuation = new Point3f(1, 0, 0);
        Point3f lightPos = new Point3f(viewPos);
        PointLight pointLight = new PointLight(white, lightPos, attenuation);
        pointLight.setInfluencingBounds(SGUtils.INFINITE_BOUNDING_SPHERE);

        // create behavior that calls nextFrame before each frame
       FrameBehavior frameBehavior = new FrameBehavior(this);

        // add it all to the universe
       BranchGroup root = new BranchGroup();
        root.addChild(pointLight);
        root.addChild(boxTG);
        root.addChild(groundTG);
        root.addChild(frameBehavior);
        simpleUniverse.addBranchGraph(root);

        return canvas3D;
    }

    /**
     * Creates dynamicsWorld and adds RigidBodies
     */

    private void initPhysics() {
        dynamicsWorld = createDynamicsWorld();
        dynamicsWorld.setGravity(new Vector3f(0f, -10f, 0f));
        dynamicsWorld.addRigidBody(createBoxBody());
        dynamicsWorld.addRigidBody(createGroundBody());
    }

    /**
     * Creates a dynamics world
     */

    private DynamicsWorld createDynamicsWorld() {
        // collision configuration contains default setup for memory, collision setup
       DefaultCollisionConfiguration collisionConfiguration
                = new DefaultCollisionConfiguration();
        // calculates exact collision given a list possible colliding pairs
       CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);

        // the maximum size of the collision world. Make sure objects stay
       // within these boundaries. Don't make the world AABB size too large, it
       // will harm simulation quality and performance
       Vector3f worldAabbMin = new Vector3f(-1000, -1000, -1000);
        Vector3f worldAabbMax = new Vector3f( 1000,  1000,  1000);
        // maximum number of objects
       final int maxProxies = 1024;
        // Broadphase computes an conservative approximate list of colliding pairs
       BroadphaseInterface broadphase = new AxisSweep3(
                worldAabbMin, worldAabbMax, maxProxies);

        // constraint (joint) solver
       ConstraintSolver solver = new SequentialImpulseConstraintSolver();

        // provides discrete rigid body simulation
       return new DiscreteDynamicsWorld(
                dispatcher, broadphase, solver, collisionConfiguration);
    }

    /**
     * Creates a rigid body representing a box
     */

    private RigidBody createBoxBody() {
        // must be same as Java3D box
       CollisionShape colShape = new BoxShape(new Vector3f(BOX_DIM, BOX_DIM, BOX_DIM));

        // any positive non zero value would do
       float mass = 1f;

        return createRigidBody(colShape, mass, boxTG);
    }

    /**
     * Creates a RigidBody representing the ground.
     */

    private RigidBody createGroundBody() {
        // collision object is a horisantal plane located at y = 0
       Vector3f normal = new Vector3f(0, 1, 0);
        CollisionShape colShape = new StaticPlaneShape(normal, 0);

        // 0 mass means body is static
       float mass = 0f;

        return createRigidBody(colShape, mass, groundTG);
    }

    /**
     * Creates a RigidBody from the specified CollisionShape and mass.
     * A TGMotionState is used to bind the rigid body to the specified TransformGroup.
     */

    private RigidBody createRigidBody(CollisionShape shape, float mass, TransformGroup tg) {
        // calculates moment of inertia, the rotational "mass"
       Vector3f localInertia = new Vector3f(0, 0, 0);
        shape.calculateLocalInertia(mass, localInertia);

        MotionState boxMotionState = new TGMotionState(tg);

        // create and return
       RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(
                mass, boxMotionState, shape, localInertia);
        return new RigidBody(rbInfo);
    }

    /**
     * Resets the scene to its start state
     */

    public void resetScene() {
        // iterate rigid bodies
       for (int i = 0; i < dynamicsWorld.getNumCollisionObjects(); i++) {
            CollisionObject colObj = dynamicsWorld.getCollisionObjectArray().get(i);
            RigidBody body = RigidBody.upcast(colObj);
            if (body != null) {
                if (body.getMotionState() instanceof TGMotionState) {
                    // reset body to its start position
                   ((TGMotionState) body.getMotionState()).reset(body);
                }

                // removed cached contact points
               dynamicsWorld.getBroadphase().getOverlappingPairCache().cleanProxyFromPairs(
                        colObj.getBroadphaseHandle(), dynamicsWorld.getDispatcher());

                // stop the body from moving and spinning
               if (!body.isStaticObject()) {
                    body.setLinearVelocity(new Vector3f(0f, 0f, 0f));
                    body.setAngularVelocity(new Vector3f(0f, 0f, 0f));
                }
            }
        }
    }

    /**
     * Updates the simulation.
     * @param frameTimeSec the duration of the last frame in seconds
     */

    @Override
    public void nextFrame(float frameTimeSec) {
        // will increment simulation in 1/60 second steps and interpolate
       // between the two last steps to get smooth animation
       dynamicsWorld.stepSimulation(frameTimeSec);
    }
}

Offline tom
« Reply #6 - Posted 2011-06-21 09:37:24 »

I guess the code for TGMotionState can be useful aswell:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

/**
 * Custom MotionState that gets the world transform from a TransformGroup
 */

public class TGMotionState implements MotionState {

    private Transform3D startTransform = new Transform3D();
    private TransformGroup tg;

    /**
     * The current TransformGroup transform will be used when reset.
     * @param tg the TransformGroup that is wrapped
     */

    public TGMotionState(TransformGroup tg) {
        this.tg = tg;
        tg.getTransform(startTransform);
    }

    /**
     * Gets a reference to the wrapped TransformGroup.
     * @return the transformGroup
     */

    public TransformGroup getTransformGroup() {
        return tg;
    }

    /**
     * Gives bullet the world transform of tg.
     */

    @Override
    public Transform getWorldTransform(Transform out) {
        Transform3D t3d = SGUtils.getWorldTransform(tg);
        t3d.get(out.origin);
        t3d.get(out.basis);
        return out;
    }

    /**
     * Sets the world transform of tg from the transform provided by bullet.
     */

    @Override
    public void setWorldTransform(Transform worldTrans) {
        Transform3D t3d = new Transform3D();
        t3d.setTranslation(worldTrans.origin);
        t3d.setRotationScale(worldTrans.basis);
        SGUtils.setWorldTransform(tg, t3d);
    }

    /**
     * Resets the wrapped TransformGroup and the specified body to where it
     * was when this TGMotionState was created.
     */

    public void reset(RigidBody body) {
        tg.setTransform(startTransform);
        body.setWorldTransform(getWorldTransform(new Transform()));
        body.setInterpolationWorldTransform(getWorldTransform(new Transform()));
        body.activate();
    }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (6 views)
2014-07-29 18:09:19

Riven (4 views)
2014-07-29 18:08:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!