2D Physics System

Resolved the issues - demo updated. No new demos so don’t get too excited - but energy in the system with restituion is now correctly conserved it seems.

Kev

Updated again:

  • Collision reactored so it can be used separately
  • Collision callback listeners added
  • Line collision detection touched up - seems more reasonable now
  • Body exlcusion added - you can now define sets of bodies that don’t collide with each other
  • Fixed Joints added
  • Elastic like joints added though not nearly right yet
  • Few more demos added to the box to test out things above

Webstart is still: http://www.cokeandcode.com/phys2d/phys2d.jnlp
SVN: http://code.google.com/p/phys2d/
Builds: http://www.cokeandcode.com/phys2d/

Kev

Neat ;D

In an attempt to make the phys2d project look more cool and trendy (coolness factor is everything) I’m putting together a more polished looking demo:

http://www.cokeandcode.com/physiball/physiball.png

It’s currently called PhysiBall.

Controls are left/right cursors to apply force to the ball, space to jump and R to restart the demo. You need LWJGL compatible hardware (i.e. some up to date OpenGL). Source isn’t currently available - will make it so if anyone wants but it’s utitlises my main code base (which is also free source) so it might not be too useful.

Webstart is here: http://www.cokeandcode.com/physiball/physiball.jnlp

My aims are:

  1. To have a nicer looking demo
  2. To weed out problems with the API and missing bits of functionality need for games
  3. To find problems with the physics engine through real scenarios

So far, updates/discoveries include:

  • A Triangulator Utilty for polygon type shapes
  • Adding damping as a world property - allows you to cause bodies to lose energy through heat/sound type things.
  • The ability to determine all current contacts with a given body from the World (used to work out when you can jump)
  • Two bugs with the physics code that currently prevent you pushing boxes off the edge :frowning:

If you try the demo and find issues let me know,

Kev

Nice job. But it would be nice if you couldn’t fall off the edge forever & don’t know how to restart. Also, its nice the way the demo with the billiards triangle breaks symmetrically!

Thanks for doing the circle-circle collision by the way, I’m beginning to understand the way the code works now. May I ask why the custom math & geometry classes are used rather than say java.awt.geom.Point2D.Float?

About polygons: intuitively all we need to do a collision is the

depth of penetration,
point of contact
the normal (this is just the angle of penetration entry, right?)

Using the java.awt.geom.Area class we can find polygons of intersection, and we could work out the above things so could polygon collision be done in this way? I’d like your view on this method before I waste my time.
Thanks,
Keith

Originally there was a dependency on vecmath.jar - however I used so little of it seemed a waste of bandwidth including given how simple the math required is. The custom geometry classes are so there isn’t any depdency on AWT/SWT/Whatever. It’s a data model not a renderer.

Using AWT classes doesn’t seem like a good idea to me, bringing in Area brings in Shape which probably brings in a whole host of AWT stuff - this prevents us building a small binary later if so desired (GCJing out AWT is what I do for instance). I’m not keen on bringing in prescribing the rendering method like that. Also I’ve used Area in games beforce and do collisions between areas is pretty darn slow. However, I’m open to doing it if there was a really good reason - anyone?

To do the collision you just determine if the polygons intersect. To do the response you need the things you list but the normal is the normal of the surface from which the body is rebounding, not the inverse of penetration velocity (if thats what you meant?) since that’d mean things only ever rebounded straight back on themselfs.

I’ve not really thought about Polygon/Polygon intersection yet - I guess Area seems like the easiest route - I’m not sure how hard it is otherwise. If you can figure out Triangle/Triangle intersection in some quick form that might work since I recently added a quick&dirty triangulator to the source.

Kev

Area & Shape classes do suck especially the way underlying points can’t be got at except through PathIterator & you can’t extend them (final class) >:(. I’ve got my own Area implementation that is badly designed & has bugs but it can test for intersections & can produce polygons of intersection. It could also be useful because it can find the centre of mass of a shape. I just wanted to know if my ideas about collision were on the right track. I will clean up my polygon class & try to implement your code with polygons then.

My (faulty) Area-like polygon class extends java.awt.geom.Shape so it can be painted by Java2D but it can be re-worked without that (if I can conjure the talent to get polygons working, that is :-).

Thanks for the help,
Keith

Added a couple of new toys to physiball above. You can now play with

a) A stack of boxes woo
b) A seesaw/teeter
c) A rope bridge type contraption

Fixed the issues I’ve seen/been told about so far.

Kev

I’m sill here i’m afraid :wink:

Woa… awesome :slight_smile:

It also seems to run way faster now, but i cant tell for sure since the older version was w/o frame counter.

Idea from a newbie:

If you get dynamic line collisions working… couldn’t you just make a polygon a group of lines that are linked together?

very cool ;D

I don’t think that we could get dynamic lines working since with line-line there’s no penetration, only intersection. I’m working on polygons right now but my vector maths skills suck, I’m more into angles & trigonometry.

Kev, that see-saw is awesome, trying to balance it was fun enough, some really amazing games will come out of this project I’m sure!

Looks (feels!) really cool!

Would it be hard to introduce a third but limited dimension?:
Rotations can only occur as now - around one axis (the “height” axis, if you consider the current view as top-down)
Geoms can only have “height” (apart from what they have now) so that circle would become a standing cylinder (ie aligned to the “height” axis)
Some more math for sure, but would it be “fairly easy” or “a total rethink of everything”?

This sort-of-3d would come in handy for a isometric style games (not necessarily isometric, but for that kind of view)

/M

Of what material is the ball? Or is the gravity that high? I like the demo, but for a ball it does not jump and bounce off enough.

Looks like a medicine ball…

Think I’ve solved your issue (with the physics) now. Maybe?

Thanks. The speed up is most due to removing the black lines around elements.

Dynamic lines might actually work as polygons, but it might not be a very efficient scenario. I’d considered when implementing lines but it was just a quick way to get some of the effects I wanted.

Heh on the see-saw - daft isn’t it? I’d like to see some games but I’m not sure it’s really something anyone is going to use other than for toy projects.

Interesting idea. Probably pretty easy - very little extra math, you’d just do a precheck on all collisions (could even be done outside the main collision code) to see if the “height” values overlapped. Yeah, I reakon this would be pretty simple to add.

The ball has a low resitution yes, as does the surfaces it’s bouncing off. This is set as simply as Body.setRestitution() so not too tricky to change. Combined (surface and ball) restitution is very low so you don’t see much bounce. However, with lots of bounce the ball is hard(er) to control which wouldn’t make for a very enjoyable demo.

Kev

Insane ! Congratulations ! :wink:

PS : At the begining it says to move the ball using the mouse… The ball is moved using the keyboard :stuck_out_tongue:

Hi Kev,

Very well done!
Definitely the polish is doing wonders to the engine!

I balanced the ball on different objects and it was fun for 5 minutes!!

When playing it fellt a bit like Professor FizzWizzle (http://grubbygames.com/)

Keep those briliant java games coming Kev!

Regards from

I’ve run into a snag with the polygon stuff.

Arbiters - the things that referee collisions between polygons (& other shapes) - can only seem to handle 2 Contacts between between a pair of shapes. Since you need 2 contacts for when a corner of a shape hits a flat face, for convex polygons there may be many intersecting vertices so Arbiters need to handle multiple Contacts.

Kev I haven’t even looked at the Arbiter & physics code yet, the polygons & vector math are hard enough. I was wondering if you’d know anything about making the Arbiter referee 2+ Contacts? Here’s the stack trace of the exception I’m getting as soon as more than two contacts are given to the Arbiter. I set Arbiter.MAX_POINTS = 10 and I still get this error when a thrid Contact was added in the KPolygonKPolygonCollider.collide(Contact[], Body, Body) method:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 2
at net.phys2d.raw.Arbiter.update(Arbiter.java:169)
at net.phys2d.raw.CollisionSpace.resolve(CollisionSpace.java:167)
at net.phys2d.raw.strategies.BruteCollisionStrategy.collideBodies(BruteCollisionStrategy.java:55)
at net.phys2d.raw.CollisionSpace.collide(CollisionSpace.java:61)
at net.phys2d.raw.World.broadPhase(World.java:227)
at net.phys2d.raw.World.step(World.java:163)
at net.phys2d.raw.test.AbstractDemo.start(AbstractDemo.java:183)
at net.phys2d.raw.test.Demo1.main(Demo1.java:184)

Let me know if you’re busy because then I’ll have a crack at it over the weekend.
Keith

Great! Thanks a lot for the contribution… one tiny request: some docs/tutorials besides the sources? Thanks again