Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Dice physics  (Read 9845 times)
0 Members and 1 Guest are viewing this topic.
Offline Hangout

Senior Newbie





« Posted 2008-06-13 21:50:43 »

Hi,

In my LWJGL app I load some dice and a table from obj files. Now I need to add the physics so that the dice fall on the table and turn correctly. I then want to be able with the physics engine to correctly pick one of the dice and read what value it has (which number is pointing upwards the positive Z axis)

Whats the best and easiest way to do this? I first thought about JBullet but its hard to implement it in my existing source. As for odejava I'm not sure if its good to use it.

Can anyone help me out here? I need to get a solution soon.

Thanks
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #1 - Posted 2008-06-14 02:04:33 »

Hello. Just to communicate to everyone I am still around.

I always fancied doing a real dice simulation so I have written one as an example application in JOODE.

Its called Dice and its in the examples directory. Although the renderer is Xith I have tried to make it obvious where you need to chop that bit out for your own renderer.

The example throws two dice, and prints the results as each dice has landed and stopped moving on the table.

SPACEBAR rethrows the dice.

For your app you will need just the JOODE jar and the Openmali jar once you adapt the code to your own renderer.

The simulation features bouncy dice, velocity dampening, dice-to-dice collisions and gravity

Tom

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline Hangout

Senior Newbie





« Reply #2 - Posted 2008-06-14 08:48:28 »

Hi, thats nice

I think this example can help me a lot.

Just a few problems: with my downloaded joode.jar I do not have the BodyDamper class which is used in the example as well as the negate function used in the settledFace method. I have commented out the following:
1  
2  
 // new BodyDamper(d1, 10,.1f);
     //  new BodyDamper(d2, 10,.1f);


and wen I run my app it calculates the positions of di1 and di2 but never gets d1SettleCount or d2SettleCount to 0. So I cannot even get to where the settledFace method is called.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #3 - Posted 2008-06-14 10:26:34 »

Strange you don't have those classes. Where did you check the files out. They are in the repository. Damper is in the force directory. If you have downloaded the jar off the website that is a old version. Get the source directly from the subversion repository

I expect that because the body dampner is not around (which applied a force against the direction of both angular and linear velocity) there is not a loss of energy in the simulation. I set the bounce to 1 meaning things should bounce for ever. So change bounce down low. Say, .1f. Energy loss should be alot more then.

The 4 parameters that control the simulation are:-
bounce
world ERP (error reduction parameter)
world cfm (constraint force mixing)
and mu (coeffecient of friction) You could try changing this up high as well.

But the simulation is setup pretty well as it is. Get the subversion version.

The negate use is just a math hack. Replace the negate with a new contruction of a Vector3 called down (0,-1,0) and use that subsequenty instead of up



Tom

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline Hangout

Senior Newbie





« Reply #4 - Posted 2008-06-14 20:43:00 »

Ok, yes I was a bit lazy to get the source and build it my self.

Now I've come to another problem:
1  
 net.java.dev.joode.util.pool cannot be resolved to a type


and

1  
net.java.dev.joode.collision.collider.INSTANCE cannot be resolved to a type


Would be nice to know how to solve that so that I can buld joode.jar
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #5 - Posted 2008-06-15 10:28:35 »

I will check the project out fresh and work out what all these errors are.

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #6 - Posted 2008-06-15 14:32:51 »

Hmmm. Well I just checked out the project anonymously into a fresh directory and the src compiled as is, and the dice app ran.

I used java 1.6 to run it, but I believe it should work fine with 1.5 and above.

Check your dependencies are pointing to the right libraries etc.

Anyone else got any ideas??

Tom

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline Hangout

Senior Newbie





« Reply #7 - Posted 2008-06-15 15:59:46 »

With what IDE did you build it? I use eclipse but I really hate this IDE. Could not do anything with the checked out project and I somehow had to copy my files into a new Java project. Probably that is the the problem.

Normally I'm using Netbeans. I much prefer this one.
Offline Hangout

Senior Newbie





« Reply #8 - Posted 2008-06-15 17:01:11 »

OK, I could build it with Netbeans. Strange that I haven't seen that before.

One thing now: Do your dice have only the values 1-3? I never got 4,5 or 6.
Offline Hangout

Senior Newbie





« Reply #9 - Posted 2008-06-15 21:33:40 »

Hmm,

And what do I have to set for my table object? I've used TriMesh but dont know how to use Mass and BodyDamper.
I used setGravityMode(false), setFlag(Body.BODY_FLAG_NO_FORCE, true) and setFlag(Body.BODY_FLAG_GRAVITY, false)

The table gets not pulled down by the gravity but as soon as the first dice hits the table, it flies away.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2008-06-16 15:22:12 »

Stay away from TriMesh. Physics are much easier to calculate with basic geometry, like boxes and spheres


For the table: just use 4 long boxes and 1 flat box - it might collapse, so you might want to add some constraints.

For the dice: 8 spheres with strong springs separating them, will do the job just fine.

To get which side is up, just see which 4 spheres are highest, and lookup the number (1..6) for that comb.


With 8 spheres (on the corners of the imaginary cube), you get nice (perfect) rounded corners, like a real dice has, so they will roll/bounce much smoother. The empty space among the spheres won't be a problem when rolling on a flat surface like a table.



You need 3*4 (edges) + 6x2(cross faces)+ 2x2(cross center) = 28 springs for the dice

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #11 - Posted 2008-06-16 16:36:34 »

Quote
For the dice: 8 spheres with strong springs separating them, will do the job just fine.

+1, far away the best method to use. fast to compute and realistic results, and in case of, for the inner space you can use a fifth centered sphere with its diameter equals to the height of the dice.

I use such technic (only 4  spheres) to simulate a rigid car and it works pretty well (http://demo.dzzd.net/ROAD4/), 4 spheres constrained to have a constant distance == some kind of  physic triangulation.

EDIT: using about 4/5 recursive "constraints step" in this case (rigid body) may help.

if s1 to far or to near from s2 move s1 towards/backward s2, and s2 towards/backward  s1, use about half of the overlength of the wanted distance, do the same for all other constraints between sphere.

recursivly do previous step N times. N == approx 4

Offline Hangout

Senior Newbie





« Reply #12 - Posted 2008-06-16 18:39:32 »

Ok, I'll try out that with the dice later.

I need to speed up a little. It does not have to look perfectly but I need to be finished this friday.

One point, even if i use boxes for the table instead of TriMesh what do I have to do inside JOODE that the table does not get affected by the force of the dice?

Another thing I need to solve:
How can I let the player select one of the dice?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2008-06-16 20:13:28 »

IIRC a Geom does not move. A Body does.

So use a Geom for the table.



Anyway, I say you want to use my approach for later, while I *think* mine could be faster to get working, but I dunno... Lips Sealed

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Hangout

Senior Newbie





« Reply #14 - Posted 2008-06-16 22:15:52 »

Phew,

So many problems. All my objects are a bit more complex as simple object even my dice. They are loaded from an obj and its hard for me to get the right body for my dice and the table. When I use TriMesh for the table, the app runs. But when I use TriMesh for the dice it crashes. when I use Box for the dice I don't know how to set it to the right size. Right now it just looks wrong, because the collistion is not show right in the app.

To do it with different spheres and connect them with joints, I don't know where to start. I don't know which side is which and how long or high the edges are.
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #15 - Posted 2008-06-16 22:27:50 »

Yeah use a box for the table top. But create a geom and add it to the space. Don't attach a body. That should work fine. Thats called static geometry. It should stay put.

My dice was working with numbers 4-6. Is it that you removed the  negate function and did not put it back (semantically or otherwise)?

8 spheres instead of one box for a dice is very clever actually. That should stop the unstable bobbling that my simulation has which is caused by edge-edge collision hacks. Then you can remove disabling the body functionality I put in which kicks in when the a dice has settled. In JOODE you don't need to put a strong spring between the spheres. You just encase the spheres with a GeomTransform object so that the spheres can be offset from the center of gravity of a single Body object. So you end up with 8 GeomTransforms which you  then add to a single body which gets added to the World object.

My computer doesn't have a gfx card at the moment so I my simulation runs extermely slowly here anyway. Thus I can't help on tuning the performance. Increase the world step size. You will then probably have to adjust the world erp, cfm, mu (SurfaceParameters) and bounciness parameters to keep the dice realistic. Setting mu to 0 or Infinity has additional speed benifits. Also setting the dice to be a spherical mass also has some extra performance benifits. I would be pretty annoyed if running the physics for 2 bodies is a strain for the dynamics engine though, but well there could be a bug in some subset of the code that is slowing performance down unecissarily. You would need to profile to solve that and I doubt you have time.

Selecting a dice is the graphics renderer's problem, not the physics engine. Xith has picking routines so you will have to look thats stuff up yourself.





Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #16 - Posted 2008-06-16 22:53:29 »

Sorry you posted at same time as me.

Trimesh trimesh collisions don't work in all cases. So you can't use them for both. But theoretically you really don't want to do that anyway.
Surely you can just adjust the ground plane height to be the correct height for whatever graphical representation you are using for the table.

As for dice size, you just have to fiddle. It may be that your graphic representation doesn't have the center of the dice as the origin, in which case you will have to offset the geometry. You don't want to do that in the physics. The Body should be in the center of the box representation which it is currently. You just need to adjsut how the graphics are overlayed with respect to the body. Read the obj file and work out where the coordinates of the verteces are. Mutiple the origin offset by the 3x3 rotational matrix of the body, then add the body's positional vector to that result (negate your offset, jiggle the order of operations until it works).

Tom

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline DzzD
« Reply #17 - Posted 2008-06-17 01:40:28 »

you seems to make things more commplexe than what they are. I mean you just need to be able to perform sphere to any object of your world collision (maybe only sphere => box(table/dice) collision) and that's all.

define 8 sphere
define 8 velocity vector (one per sphere)

do
 for each sphere s in the eight sphere
  move s with its current velocity vector (perform collision)
 perform each constraints between sphere.
end do

finally add some inertia by doing : for each sphere save new velocity vector == (end sphere pos - start sphere pos) *coeff




Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2008-06-17 05:43:26 »

Verlet Integration FTW. (google for the gamasutra.com article!)


You'll have realistic dices rolling around without any matrix/cos/sin/sqrt or basically anything - everything happens with a few lines of code.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #19 - Posted 2008-06-17 06:56:20 »

good article, very well explained,

and I just discover that what I did in 3DzzD was knew as the Verlet Integration and blablabla

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2008-06-17 16:55:31 »

Well, Verlet Integration has neither Velocity nor Acceleration.

It just has these: [Vec3 @ t=0], [Vec3 @ t=-1], [Vec3 @ t=-2] for each sphere/point.
t=0 means current location
t=-1 means location in previous step
t=-2 means location in step before previous step

Velocity = [Vec3 @ t=0] minus [Vec3 @ t=-1]
Acceleration = ([Vec3 @ t=0] minus [Vec3 @ t=-1]) minus ([Vec3 @ t=-1] minus [Vec3 @ t=-2])
    --> basically: current_velocity - last_velocity


Minor diff, yet extremely nice for backtracking during a collision!!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #21 - Posted 2008-06-17 17:16:17 »

hey, you point out something very interresting for me, acceleration is maybe something I have missed!! , this should explain why I had so much difficulties to get a proper inertia.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #22 - Posted 2008-06-17 17:18:59 »

Actually, you can have enertia without acceleration.

The downside is that you need *lots* of spheres, so that the 'shockwaves' of collisions can travel through them. The 'shape' will compress and naturally bounch back in the opposite direction.

For a good result you might need 4x4x4 spheres (looooooooooaaaaaaaads of springs).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Hangout

Senior Newbie





« Reply #23 - Posted 2008-06-17 19:18:59 »

Can I somehow render the geoms or bodies from JOODE?? So it would be much easier to place and scale them correctly.
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #24 - Posted 2008-06-18 01:16:52 »

I have little experience of doing fancy rendering. However, you can setup a custom Scenergraph node for each geom. Create the dice with a Box like it is in the SVN repository allready. Before adding the Geom to the space though, you would need to define a custom scenegraph node to describe how that would be drawn. The class is XithGeomAppearance and you would need to set the scene subtree (look for the method setSceneSubgraph or something). You need to do this before the geom is added to a space (so that the XithManager gets to see the graphics representation when it detects a new Geom has been added to a space).

This will not really help getting you getting you graphic aligning with the collision bounds though. It will mearly take care of all keeping a graphics appearing in sync with the bodies position.

The XithManager inserts a TransformNode into the scenegraph that is adjusted everytime the world updates . This Transform node is synced with a geoms position so that when the body moves around, so do the geoms and their associated transform nodes are moved with the body. So you can hang off arbitary graphics code there for wonderfully complex graphical representations. But JOODE cannot possible ensure this representation relates in any way to the Collision geometry.

Tom

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline Hangout

Senior Newbie





« Reply #25 - Posted 2008-06-19 09:51:58 »

Ok,

I've done it with the 8 spheres. For that I calculated minX, minY, minZ and maxX, maxY, maxZ. Then I created 8 spheres and set their position with setPosition to something like this (minZ,minZ,minZ) and so on.

When subtract minX from maxX i get 0.5 and now i don't know what size to set for the spheres radius. Is it 1f,0.1f or something else? What would you suggest?

My real problem is, that the collisions are completely wrong. Even with the dice it self it doesn't work.
I have not set the position of the dice Mesh. It gets it position from the Geom object. Is it possible that the spheres and their GeomTransform are not correctly offset? How do i set the Mass and center of the object correctly? And how do I offset the GeomTransform anyway?

Hope you can help me here
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #26 - Posted 2008-06-19 10:48:39 »

Setting the Mass object up is not important. Just set the mass up as 1, and set it up as a sphere of radius 1 or something. (or a box). An incorrect Mass is not usually detectable by the user unless the object being represented is really long and thin. The mass should have the correct center of gravity though, which is implicit by using just one body to represent the dice

The spheres need to be equally distributed around the body, not offcenter. So for every sphere places at +x,+y,+z there needs to be another sphere at -x,-y,z. Common sense.

So to use a GeomTransform you simply set your spheres up first (with setPosition, using the offsets, not global coordinates), and *don't* add them to a space. Then you create a geomTransform encapsulating each sphere (I think this is done at construction time of the GEomTRansform). Then you add the GeomTRansform object to the body, and add the GeomTransform to the space. So nowhere do you add the sphere object directly to the body or space. The GeomTransform uses the coordinates in the Shere object to offset the geometry relative to the body.

If you use the default graphics behaviour of the XithManager, the above description will create graphical representations of the spheres and that should all look realistic w.r.t colli8sion behavior. You then need to draw you graphics mesh using the positional information obtained from one of the geoms or the body object. The body positional information is most intuitive because that will be centered w.r.t to the center of mass. Although using a sphere's might actually correct a mesh that is described using only positive coordinates. There is a chance your graphical representation is not centered, in which case you will have to repositions the graphics from the body's geometry. You need to learn some basic computer geometry concepts if you can't do this.

The radius of the spheres is kinda dependant on the scale of the dice. If the dice is 10,000 units in diameter you probably want spheres of radius 100 or something. For this application you aren't really worried about a small object penetrating the dice. So maybe set the diameter of the spheres to about 10% of the total diameter of the dice. It shouldn't really be a critical thing though anyway.

Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline Hangout

Senior Newbie





« Reply #27 - Posted 2008-06-19 11:44:50 »

Ok,

it works now so that it is acceptable. It is not perfect but its OK.

My next problem is the rotation the code Matrix3.setRandomRotationMatrix(new Matrix3()) gives my only small values back (never bigger than one) what do I have to calculate that I can use that rotation in
1  
2  
3  
            GL11.glRotatef(rotation.x, 1, 0, 0);
            if (rotation.y != 0) GL11.glRotatef(rotation.y, 0, 1, 0);
            if (rotation.z != 0) GL11.glRotatef(rotation.z, 0, 0, 1);
Offline t_larkworthy

Senior Member


Medals: 1
Projects: 1


Google App Engine Rocks!


« Reply #28 - Posted 2008-06-19 14:10:58 »

The elements in a rotation matrix will always be less than or equal to one. What is GL11.glRotatef(rotation.x, 1, 0, 0); supposed to do? Is it an axis angle rotation.

You really don't ever want to try and cut a rotation into three Euler angle rotations when you have the full rotation matrix available. You will run into the Gimblelock problem (look that up!). Basically the order in which you apply the axis of rotation become imporant If you are desperate to use an axis angle rotation then find the methods in the math library that converts the matrix3 into an axix angle representation.

Now JOODE has its own math representation (Matrix3) which does not have all the axis angle stuff. Openmali does, so convert your Matrix3 to a Matrix3f. There is a converter implemented somewhere (MathUtils?) but you can do it manually by copying the 9 elements in the matrix across into a Matrix3F. Just make sure that you don't accidentally transpose the matrix during the operation.

Openmali has routines for then converting the Matrix3f into an AxixAngle4f object which you can then read off the values to insert into:-

 GL11.glRotatef(angle?, axis.x, axis.y, axis.z);

Tom


Runesketch: an Online CCG built on Google App Engine where players draw their cards and trade. Fight, draw or trade yourself to success.
Offline Hangout

Senior Newbie





« Reply #29 - Posted 2008-06-19 21:09:58 »

So, it works almost now.

A few issues:
- Sometimes only 3 or 4 of my 5 dice get rendered (even though the physics work and return me the die-value like in the example)
- Sometimes the collision with the table is not correct and my die jumps up as if it has hit a trampoline.

So far, thanks a lot for that great support.
Pages: [1] 2
  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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!