[Odejava] OO Issues - round 3

I wish to add multi-geom support to the API and have been discussing it with Jani over the last week. I thought I would move the discussion here :slight_smile: When I looked into this I have thought up a few improvements to the API as well.

Here’s a few things I think should be changed:

#1
Geom has a set/getBody methods - I think these should just be dropped as otherwise you can have this situation:


Geom g = new Geom ();
Body b1 = new Body (world);
Body b2 = new Body (world);

b1.setGeom(g);
g.setBody(b2)

which is very misleading (in actual fact - the setBody in this context means zip).

#2
Body needs to be expanded to support multiple Geom’s. This is fairly easy, we would just have a linked list of Geoms instead of one.

“setGeom” would be changed to “addGeom” - and
Iterator getGeomIterator() would replace getGeom. set/getGeom could be retrofitted to simply add a Geom to the list - and return the first Geom in the list.

#3
Space shouldn’t really know about Bodies - hence both “add” methods would be removed.

addGeom would be added to add a Geom to the space.
addBodyGeoms would be created as a conveniance method to add all the geoms in that body to the space (but not the body itself).

An assert would be added to ensure no Geom is added twice (Matt’s problem in the other thread).

Instead of Space maintaining a list of Bodies - I think the World should. So world could have:

addBody() and
Iterator getBodyIterator()

All of these suggested changes can be made without breaking the API as far as I can see. The benifits are: basic multi-geom support and a less confusing API.

Full Multi-Geom’s support will need a little more work but the basic support would be there.

Cheers,

Will.

I have gone ahead and made the changes so you can see what I mean.

If you don’t like them they can easily be reversed. Note: Everything is backward compatable - the old demo’s work fine unmodified.

Basically the changes are these:

  • Avoid using set/getBody on Geom.
  • Use World to manage your Bodies instead of Space (all Bodies are added to the master Body list in their world on creation). This was changed to bring Odejava more inline with ODE regarding World and Body, Space and Geom.
  • Use Space.addBodyGeoms(Body) instead of Space.add(Body) when you use point 2.
  • You can now add multiple geoms to a body.

So long as these changes are ok - I’ll convert the demos (very easy to convert I just want to make sure the changes are ok first).

I apologise for several recent API changes, I hope I am not out of line. I am just trying to correct some problems that I see and add support for new features before we have too much legacy code to make it a real pain :slight_smile:

Cheers,

Will.

nothing eh?

I guess that means people are either a) busy b) happy with the changes or c) pissed off at me for yet another change :wink:

I’ve got some time tonight so I’ll modify the demos.

Cheers,

Will.

These changes sound good, any API changes we can make that direct people on how and where to use Geoms versus Bodies is going to really help people understand things better. I’ll have more concrete feedback once I work with the changes directly (which usually results in a couple rapid fire posts like last time ;))

Matt

[quote]nothing eh?

I guess that means people are either a) busy b) happy with the changes or c) pissed off at me for yet another change :wink:
[/quote]
Heh, I’ll choose option a, I’m pretty sure it will soon be option b :slight_smile:

Seriously, I try to participate a bit more to discussions in the near future.

ok, cool :slight_smile:

I’ll change the demos. These changes are actually very minor but I think they will help new users (and only confuse current users a little ;))

Here’s my cvs email (if you’re not already on the cvs list I strongly urge you to join it!)


User: willdenniss
Date: 04/03/25 18:25:51

Modified:
 /odejava/odejava/src/org/odejava/test/simple/
  HighLevelApiExample.java

Log:
 removed use of deprecated methods.
 
 Moving to the new way of using World for bodies and Space for geom is very easy
 just follow the example of this file.
 
 Change use of space.add(Body) to space.addBodyGeoms(Body)
 space.add(Geom) to space.addGeom(Geom)
 and the use of space.getBody() to world.getBody()
 
 Note - you don't need to manually add Bodies to World - they are added
 automatically in the constructor
 

File Changes:

Directory: /odejava/odejava/src/org/odejava/test/simple/
========================================================

File [changed]: HighLevelApiExample.java
Url: https://odejava.dev.java.net/source/browse/odejava/odejava/src/org/odejava/test/simple/HighLevelApiExample.java?r1=1.9&r2=1.10
Delta lines:  +4 -4
-------------------
--- HighLevelApiExample.java    29 Feb 2004 08:03:05 -0000      1.9
+++ HighLevelApiExample.java    26 Mar 2004 02:25:49 -0000      1.10
@@ -158,7 +158,7 @@
                // Create a sphere geom and set it to body (dynamic object)
                // Sphere radius is 1. Position sphere above the ground
                sphere = new Body("sphere",world, new GeomSphere(1f));
-               space.add(sphere);
+               space.addBodyGeoms(sphere);
                sphere.setPosition(0f, 0f, 2f);
                sphere.adjustMass(1f);
                sphereId = sphere.getGeom().getNativeAddr();
@@ -167,7 +167,7 @@
                // Box x,y,z lengths are 2,1,1. Position box above the sphere, slightly
                // right to sphere
                box = new Body("box",world, new GeomBox(2f, 1f, 1f));
-               space.add(box);
+               space.addBodyGeoms(box);
                box.setPosition(0.1f, 0f, 5f);
                box.adjustMass(1f);
                boxId = box.getGeom().getNativeAddr();
@@ -192,9 +192,9 @@
                                System.out.println(", quaternion=" + box.getQuaternion());
                                // Another way of getting position and quaternion data
                                System.out.print(
-                                       "  Sphere pos=" + space.getBody("sphere").getPosition());
+                                       "  Sphere pos=" + world.getBody("sphere").getPosition());
                                System.out.println(
-                                       ", quaternion=" + space.getBody("sphere").getQuaternion());
+                                       ", quaternion=" + world.getBody("sphere").getQuaternion());
                        }
                }
        }


I’ll update the other demos now :slight_smile:

Cheers,

Will.

Everything in the API is now uptodate and inline with all my changes phew.

Both odejava and odejava-xith3d can be compiled with no deprecation warnings - so if you are getting them it is probably in your code. I am happy to help anyone who is getting these warnings in converting their code if they don’t understand what I have said and can’t do the changes by looking at the official Odejava examples. Email me or post in these forums if you need some help.

The only thing I ended up doing differently is that I have kept the setBody and getBody methods of Geom. This is because ODE clearly has those methods and we want Odejava to be similar to ODE for compatability of the docs.

But what I did do is add a bunch of javadoc comments and asserts to warn and prevent people trying to give a Geom two parents (as this is not possible in ODE - but it was previously possible for the situation to exist in Odejava, even though in actual fact only the most recently set parent Body would have any affect).

It is still possible to give a Geom two parents if you are very sneaky but I have prevented the obvious cases - and warned about the sneaky ones in the javadocs.

Cheers,

Will.