ContactCallback without using physics?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

ContactCallback without using physics?

Postby Beauty » Mon Jun 02, 2008 8:10 am

Hi,

I use the Newton engine with Mogre (Ogre wrapper for .NET) and want to use it for collision detection.

For this I create bodies with 2 materialIDs joint by a MaterialPair. With the ContactCallback class I check for its collision / intersection. This generally works fine, but for my case is a problem.

After some experimentations I found out something. It seems so that:

Only when a colliding Body was moved by physics it will be checked for collision.
(Then the debug lines are green instead of red)
If Bodies are only moved by Body.SetPositionOrientation() there is no callback.

I think this is for optimization reason. Is there a way to get a Callback without using (moving by) physics?
e.g. by enable a Newton internal "has moved flag" of a Body? Body.Unfreeze() doesn't work.


Greetings from Germany

Beauty




If it's from interest:
Here is my (simplified) code written with Mogre / C# / MogreNewt:

Code: Select all
// -- initialization --

MogreNewt.World nWorld = new MogreNewt.World();
MaterialID objectColMaterial = new MaterialID(nWorld);
MaterialID lobeColMaterial = new MaterialID(nWorld);
MaterialPair materialPair = new MaterialPair(nWorld, lobeColMaterial, objectColMaterial);
collisionCallback = new CollisionCallback();
materialPair.SetContactCallback(collisionCallback);
materialPair.SetDefaultCollidable(1);



    class CollisionCallback : ContactCallback
    {
        public CollisionCallback()
        {  }
        public override int UserBegin()
        {
            Console.Write("  Collision ?  ");
            return 1;
        }
        public override int UserProcess()
        {
            Console.Write("  Collision !!  ");
            return 1;
        }
    }

Code: Select all
//-- adding 2 types of objects --

MogreNewt.CollisionPrimitives.ConvexHull colHull;
MogreNewt.Body body;

// -- add object type 1 --
colHull = new MogreNewt.CollisionPrimitives.ConvexHull(nWorld, targetNode);
    // targetNode is a SceneNode with mesh entity
body = new MogreNewt.Body(nWorld, colHull);
body.SetPositionOrientation(targetNode.WorldPosition,
                            targetNode.WorldOrientation);
body.MaterialGroupID = objectColMaterial
// body.AttachToNode(targetNode); 
   // no moving by Newton wanted;   move only by SetPositionOrientation()

// -- add object type 2 --
colHull = new MogreNewt.CollisionPrimitives.ConvexHull(
    nWorld, vertices, new Quaternion(0, Vector3.UNIT_Y));
body = new MogreNewt.Body(nWorld, colHull);
body.SetPositionOrientation(targetNode.WorldPosition,
                            targetNode.WorldOrientation);
body.MaterialGroupID = lobeColMaterial

Code: Select all
// -- calculation --
nWorld.Update(0.001f);  // called for each collision check
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Postby Julio Jerez » Mon Jun 02, 2008 9:04 am

can you set all bodies active.

The new version allow eto the king of functionallity.
you can take a shape ann call convex cast agaist the world and get the contacts, or you can ste a flag in teh collision call trigger and it will report no calculater contact it will only notify the intersection.

I guues for you the convex cast will be the solution, you can just take eeh collision of a body, and call convex cast an din teh call back you reject the body itself.
The function coonvert Netwon into a full fledged collision system with scene managemenet, with was a missing funtion ality in 1.53

if you are interested semd me a PM
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Postby Beauty » Mon Jun 02, 2008 10:27 am

Thanks for the quick answer (-:

I'm sad that the Object browser of Visual studio doesn't found anything like active or trigger in the DLL. Maybe it's not supported for Ogre/Mogre.

You are telling about a new version. You mean the one of SDK 1.53? A really new one I didn't found. You mean a newer (unofficial) one you can send? This would be nice, although I suppose that this is not supported by Mogre wrapper. I could try it out.
My e-mail address is beautyod @ gmx de

Alternatively I have to apply a physial property after each use of Body.SetPositionOrientation(), e.g. set a force.
This should produce a really small CPU load and I must be shure that all intersections will be detected.
(I make an application for sonar sensor simulation which needs many intersection checks per second)
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Postby Julio Jerez » Mon Jun 02, 2008 11:34 am

This is Netwon 2.0 Archemedia, the API had chnge a litel bit but it sould be trivial to mak eteh change.
you will need to do the integration to the wraper or maybe mogre can do the integration.
I beleive it provides the funtionality you are looking for, and it is much faster.

you need to send me a PM, I keep a list in teh save message of the forum.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: ContactCallback without using physics?

Postby Beauty » Mon Jul 21, 2008 8:15 am

After a while of trying I found out what was the problem:
I have to set body.SetMassMatrix() and also I forgot to set the world size. Now I get a callback (-:

But now there other problems:

(1)
I have the impression that the contactCallback does not came in the timespan while calling world.update().

In my application a body will be set by body.SetPositionOrientation().
Then I have to know if the body collides with an other one.
So I call world.update(). After this I look if a contactCallback happened and set a collision flag (boolean).
On this point I have to be shure that the collision flag is right.
Now the loop ist starting again and the body will be set to the next point (dependent of the collision flag).

My problem: If the callback comes with time delay, then my application is working wrong. It seems so. Any ideas how to manage this problem?
By the way - will this be the same with Newton 2.0? (And when Newton 2.0 could be ready?)


(2)
When the UserProcess() returns 1 then there is a strange effect:
UPDATE: UserProcess() means NewtonContactProcess() in Newton API
After starting my application the newton bodies seems to get a random rotation impulse. The rotation is getting slower and stopping after a while. (if UserProcess() returns 0 this doen't happen)
But nowhere in my application is a force call or similar. For the bodies are only defined a collision hull, materialID and body.SetMassMatrix(0.1f, MogreNewt.MomentOfInertia.CalcBoxSolid(0, Vector3.ZERO)). (without mass matrix is no callback)
Image The red/green lines are the newton bodies.
Last edited by Beauty on Mon Jul 21, 2008 11:02 am, edited 1 time in total.
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Re: ContactCallback without using physics?

Postby Julio Jerez » Mon Jul 21, 2008 9:37 am

wow, jus just gives me the idea for a new inteface. :mrgreen:

NetwonWorldCollide();

this could be use to opdate teh world collision without updating teh physics world.
The will give all of the call backs you need fo epeoople usien teh engien col collison only.
does that answere you question?

The only catch is the I will add it to 2.0
Are you using 2.0?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: ContactCallback without using physics?

Postby Beauty » Mon Jul 21, 2008 10:15 am

I think this would be good for my application (-:

If I don't need world.Update() problem (2) will not happen. (However I don't know why there is a twist)

For problem (1) the best would be a function to get collisions of a specific body.
e.g. GetCollisions(myBody) that returns a list with all bodies that collides myBody (or null if no collision).

My problem with CallBacks is the unknown time delay. I don't know if the callback comes really immediatelly after calling world.Update() || NetwonWorldCollide() or somewhen later.


(My application simulates sonar sensors of underwater vehicles. So the time for collision checking must be much more higher than the fps of screen. So the time for one collision check should be shorter then 1 milli second.)


I don't have Newton 2.0. Where can I get it?
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Re: ContactCallback without using physics?

Postby Julio Jerez » Mon Jul 21, 2008 11:48 am

Beauty wrote:For problem (1) the best would be a function to get collisions of a specific body.
e.g. GetCollisions(myBody) that returns a list with all bodies that collides myBody (or null if no collision).

My problem with CallBacks is the unknown time delay. I don't know if the callback comes really immediatelly after calling world.Update() || NetwonWorldCollide() or somewhen later.


(My application simulates sonar sensors of underwater vehicles. So the time for collision checking must be much more higher than the fps of screen. So the time for one collision check should be shorter then 1 milli second.)

Oh in the case you are in luck, the last beta of Archimidea supports non callback iteration over, bodies, joints, contact, and material.

The interface of the form
object = GetFistObject(parent)
object = GetNextObject(parent, object);

Therefore you can iterate over the bodies you are interested in and collect the information without setting any callback.
Even the callback now use that interface.
The SDK is on the site already and people are trying it.
Beauty wrote:I think this would be good for my application (-:

If I don't need world.Update() problem (2) will not happen. (However I don't know why there is a twist)

There is not twist, In the Newton world when you change the position of a body the body transformation matrix is updated, but the proxy position (aabb) of the body in the multi resolution grid is no changed. It is the integrator that the responsible for changing that.
The reason for that is for the large majority of bodies when they move their AABB do not change much, so the integrator have that knowledge already because the aabb are calculate in molten but the collision system. So it can just make the comparison between the current and the next.
Updating the multi grid each time a body matrix is change will be a big performance hit.

Say you have a 100 bodies and you move then all but small amount you will make 100 update,
Since the muligrid update is log (n) cost, you will have n * log (n)
However the cost for updating the multigrain completely is k * n an ka is very small value
Therefore you will don the per object update will have a very big cost as soon as you have a couple of docent bodies.
The reason I suggest a function NewtonWorldCollisionUpdate.
If the It will use the same collision update, followed by a pseudo integration step that will update the bodies that have change position in the multigrid.
The loop is liek this

CollisionUpdate
Dynamics Update
IntagrationUpdate


For people using scene collision will be

CollisionUpdate
pseudoIntegrationUpdate


Beauty wrote:(My application simulates sonar sensors of underwater vehicles. So the time for collision checking must be much more higher than the fps of screen. So the time for one collision check should be shorter then 1 milli second.)

Believe me you will be surprise how fast the collision system in newton is, and with the change I propose I believe you will have a full legitimate scene base collision system.
Plus in addition the Newton SDK have the ability to set collision shape like triggers that can give you feedback for sensing and prediction.
Beauty wrote:I don't have Newton 2.0. Where can I get it?

That’s is the easy part, send me a PM for the download link
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: ContactCallback without using physics?

Postby Beauty » Mon Jul 21, 2008 3:50 pm

Very thanks for the long and detailed answer !!
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Re: ContactCallback without using physics?

Postby Beauty » Tue Jul 22, 2008 12:28 pm

this sounds nice, also that Newton supports multi processor work :)

I don't know what you mean with Alchemedia, but the next 3 weeks I have nearly no more time for programming.
Now I have to learn for a hard exam of university. It's the last of my study. If I fail, the years of study could be demolished.
After this I will go on my work here.
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Re: ContactCallback without using physics?

Postby Julio Jerez » Tue Jul 22, 2008 1:16 pm

absolutly, the more important thing is your education, it will be million time more productive than a hobby project. and beside the project will still be here not rush.

Alchemedia is the second name of the engine. it is no because alchimedes but because Netwon secund pation beside math and Physsics was Alchemy. http://www.alchemylab.com/isaac_newton.htm
Beleive or not Newton was one of those guys that secrectlly beleive he could fidn magical posions by mastering the laws of chesmistry. he was also a 33 degre master of the Mason sociaty.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Re:

Postby Beauty » Thu Aug 14, 2008 11:05 am

Julio Jerez wrote:absolutly, the more important thing is your education, it will be million time more productive than a hobby project.

You are quite right!
It was hard to understand the theoretically telecommunications (the lecture was 5 years ago), but now I passed the test :D

Julio Jerez wrote:Ok I added the function NetwonCollisionUpdate you use it like this

NewtonCollisionUpdate (m_world);
And then you can either use the body iteratior to collet the contacts, or us the callbacks.
Since you have some time to integrate Alchemedia I will hold until I finish another feacture before the update, but of you need it just let me know.

You note that I updated (the needed parts) of MogreNewt.
Which iterator shall I use to detect collision contacts without callback?
NewtonBodyGetFirstContactJoint and NewtonBodyGetNextContactJoint?
Or NewtonContactJointGetFirstContact and NewtonContactJointGetFirstContact?

Julio Jerez wrote:]you can take a shape ann call convex cast agaist the world and get the contacts,
or you can ste a flag in teh collision call trigger and it will report no calculater contact it will only notify the intersection.

This you wrote as an other alternative. Is the trigger working without callback?
Or is it better to use the iterator method?

I suppose the contact/intersection only will be detected if the right MaterialPairs are defined. Or is this only related to ContactCallbacks?


My application (without use of physics) basically should work like this:

LOOP {
* update the positions/orientations of one or a few bodies/collisions
* call NewtonCollisionUpdate
* check if the collision of one specific body does intersect with other collisions
* do something dependent of interaction / noninteraction
}

If other collisions does intersect don't care.
So maybe it's possible to optimize the contact detection when I limit the "checking area" to the AABB of the specific body.
On the other hand the position/size of the "checking area" is changing for each (very short) loop and should not use much CPU time. Maybe use SetWorldSize for this?
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Re: Re:

Postby Julio Jerez » Thu Aug 14, 2008 11:33 am

Beauty wrote:You note that I updated (the needed parts) of MogreNewt.
Which iterator shall I use to detect collision contacts without callback?
NewtonBodyGetFirstContactJoint and NewtonBodyGetNextContactJoint?
Or NewtonContactJointGetFirstContact and NewtonContactJointGetFirstContact?


you use both, after you can Collision update you sopdul write something liek this;

Code: Select all
GeContacts (NetwonNody * body)
{
   NetwoinJoint* joint;
 
  //find the collison with oteh bodies
  for (joint = NewtonBodyGetFirstContactJoint(body); joint; joint = NewtonBodyGetNextContactJoint(joint)
  {
      // for each collision get teh contacts
      NetwonBody* otshBody = (NetwonJointGetBody0(joint) != body) ? NetwonJointGetBody0(joint) : NetwonJointGetBody1(joint);

    // get all contact point
    for (NetwonMaterial* contacPoint = NewtonContactJointGetFirstContact(joint); contacPoint; contacPoint = NewtonContactJointGetNexttContact (contacPoint) )
    {
         // do what ever you want with the contacts here, teh contact have all teh infimation you nee, teh point, normal, fritcion, resitition, shape etc.
 
    }

}


the materials work exactly teh same way as when using physics. if you so nto wnat to make complex just do nto set any callback and it will provide default behavoir whic is everything collides.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: ContactCallback without using physics?

Postby Beauty » Fri Aug 15, 2008 5:11 am

Nice, now I know how to use them. Very thanks :D
I thought Joints are only for fixing bodies together (as describted in this wiki article)

I changed the commentline for this line. If I'm wrong, you can tell me.
// for each collision get the contacts //OLD
// get the other contact body // NEW
NewtonBody* otherBody = (NetwonJointGetBody0(joint) != body) ? NetwonJointGetBody0(joint)
: NewtonJointGetBody1(joint);


For people who need this code snippit, here I repeate it without mistypes :wink:
Code: Select all
GetContacts (NewtonBody * body)
{
  NewtonJoint* joint;
 
  //find the collison with other bodies
  for (joint = NewtonBodyGetFirstContactJoint(body);  joint;
                                      joint = NewtonBodyGetNextContactJoint(joint))
  {
      // get the other contact body
      NewtonBody* otherBody = (NewtonJointGetBody0(joint) != body) ?  NewtonJointGetBody0(joint)
                                      :  NewtonJointGetBody1(joint);

      // get all contact points
      for (NewtonMaterial* contactPoint = NewtonContactJointGetFirstContact(joint);  contactPoint;
                           contactPoint = NewtonContactJointGetNextContact(contactPoint) )
      {
           // do what ever you want with the contacts here,
           // the contact have all the information you need,
           // the point, normal, fritcion, resitition, shape etc.
      }
  } // for
} // GetContacts()
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Re: ContactCallback without using physics?

Postby Beauty » Wed Aug 27, 2008 8:10 am

It tooks much time and trouble to update the wrapper for using Newton2 with Mogre.
Then Bekas (the author of Mogre and MogreNewt) helped me.
Now my tiny test application is working.
Many thanks to Julio who helped me very much!
I'm so happy!! :D
Mogre user
User avatar
Beauty
 
Posts: 30
Joined: Tue Apr 29, 2008 7:37 am
Location: Germany

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 16 guests