Optimize Marc's Character Controller / Batching Convex Casts

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Wed Aug 29, 2012 12:22 pm

In core 300 the collision routine are now directional, as opposed to core 200 that was a general collider tah solev convex hull to convex hull pairs.
this is because in core 300 the low lever code take advantage of the geometry features, for example a is define by a line segment and a radius.
therefore if a convex hull collide with a capsule, it is better to calculate contacts from capsule to convex that from convex hull to capsule.
because of this in core 300, if a pair is convex-capsule, the conatct solver makes a local pair capsule-convexhull, calculate the contacts and then negate the normal and transform the contacts. this is faster and more acurate.

The resutl should be the same for the high lever application, where are you getting that code fragment from?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Wed Aug 29, 2012 12:46 pm

I had to put that code fragment in to get the normals I got before. Before my last svn update, I didn't need it. Now, after I updated from svn, I need it because I get the normals flipped from the convex cast. So what I'm saying is, something has happened lately in newton that made the normals in the result of the convex cast flip. I cast a cylinder shape and hit a box.
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Wed Aug 29, 2012 1:46 pm

Oh I see, no that is not intentional, I provably forget to add that last part when writing the contact solver.
you are right the contacts normals should point to the same directing they were pointing in core 200.
I will fix it that tonight.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Fri Aug 31, 2012 7:11 am

Just for your info: I just tried again with the current svn version and the normals are still flipped.

:)
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Fri Aug 31, 2012 9:19 am

Tel me something, if a sphere is casted again a floor plane.
the sphere is over teh plane an dteh plane norla is (0, 1, 0)

wha the result should be a normla (0, 1, 0) or (0, -1, 0)
the code is producing (0, -1, 0)

for the engine is does not really matter the normal direccion, it is only important for the applcations teh part tha does that is here


Code: Select all
dgInt32 dgWorld::CollideContinue (
   const dgCollisionInstance* const collisionSrcA, const dgMatrix& matrixA, const dgVector& velocA, const dgVector& omegaA,
   const dgCollisionInstance* const collisionSrcB, const dgMatrix& matrixB, const dgVector& velocB, const dgVector& omegaB,
   dgFloat32& retTimeStep, dgTriplex* const points, dgTriplex* const normals, dgFloat32* const penetration, dgInt32 maxSize, dgInt32 threadIndex)
{
   dgBody collideBodyA;
   dgBody collideBodyB;
   dgCollisionInstance collisionA(*collisionSrcA, collisionSrcA->GetChildShape());
   dgCollisionInstance collisionB(*collisionSrcB, collisionSrcB->GetChildShape());

   dgContactPoint contacts[DG_MAX_CONTATCS];

   dgInt32 count = 0;
   dgFloat32 time = retTimeStep;
   retTimeStep = dgFloat32 (1.0e10f);
   maxSize = GetMin (DG_MAX_CONTATCS, maxSize);

   collideBodyA.m_world = this;
   collideBodyA.SetContinuesCollisionMode(true);
   collideBodyA.m_matrix = matrixA;
   collideBodyA.m_collision = &collisionA;
   collideBodyA.m_masterNode = NULL;
   collideBodyA.m_collisionCell = NULL;
   collideBodyA.m_veloc = dgVector (velocA[0], velocA[1], velocA[2], dgFloat32 (0.0f));
   collideBodyA.m_omega = dgVector (omegaA[0], omegaA[1], omegaA[2], dgFloat32 (0.0f));
   collideBodyA.m_predictiveVeloc = collideBodyA.m_veloc;
   collideBodyA.m_predictiveOmega = collideBodyA.m_omega;
   collideBodyA.m_accel = dgVector (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
   collideBodyA.m_alpha = dgVector (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
   collideBodyA.m_invMass = dgVector (dgFloat32 (1.0f), dgFloat32 (1.0f), dgFloat32 (1.0f), dgFloat32 (1.0f));
   collisionA.SetGlobalMatrix(collisionA.GetLocalMatrix() * matrixA);

   collideBodyB.m_world = this;
   collideBodyB.SetContinuesCollisionMode(true);
   collideBodyB.m_matrix = matrixB;
   collideBodyB.m_collision = &collisionB;
   collideBodyB.m_masterNode = NULL;
   collideBodyB.m_collisionCell = NULL;
   collideBodyB.m_veloc = dgVector (velocB[0], velocB[1], velocB[2], dgFloat32 (0.0f));
   collideBodyB.m_omega = dgVector (omegaB[0], omegaB[1], omegaB[2], dgFloat32 (0.0f));
   collideBodyB.m_predictiveVeloc = collideBodyB.m_veloc;
   collideBodyB.m_predictiveOmega = collideBodyB.m_omega;
   collideBodyB.m_accel = dgVector (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
   collideBodyB.m_alpha = dgVector (dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f), dgFloat32 (0.0f));
   collideBodyB.m_invMass = dgVector (dgFloat32 (1.0f), dgFloat32 (1.0f), dgFloat32 (1.0f), dgFloat32 (1.0f));
   collisionB.SetGlobalMatrix(collisionB.GetLocalMatrix() * matrixB);


   dgContactMaterial material;
   material.m_userId = 0;
   material.m_penetration = dgFloat32 (0.0f);
   material.m_isEdgeContact = 0;
   material.m_deformableIndex0 = 0;
   material.m_deformableIndex1 = 0;

   dgContact contactJoint (this, &material);
   contactJoint.SetBodies (&collideBodyA, &collideBodyB);

   dgCollidingPairCollector::dgPair pair;
   pair.m_contact = &contactJoint;
   pair.m_contactsBuffer = contacts;
   pair.m_timeOfImpact = dgFloat32 (0.0f);
   pair.m_contactCount = 0;
   pair.m_isTrigger = 0;
   pair.m_isDeformable = 0;
   pair.m_cacheIsValid = 0;
   pair.m_continueCollision = 1;
   CalculateContacts (&pair, time, threadIndex);

   if (pair.m_timeOfImpact < dgFloat32 (1.0f)) {
      retTimeStep = pair.m_timeOfImpact;
   }

   count = pair.m_contactCount;
   if (count) {
      if (count > maxSize) {
         count = PruneContacts (count, contacts, maxSize);
      }

// here we flip teh normal fo rteh applcation convenient
      dgFloat32 swapContactScale = (contactJoint.GetBody0() != &collideBodyA) ? dgFloat32 (-1.0f) : dgFloat32 (1.0f);
      for (dgInt32 i = 0; i < count; i ++) {
         points[i].m_x = contacts[i].m_point.m_x;
         points[i].m_y = contacts[i].m_point.m_y;
         points[i].m_z = contacts[i].m_point.m_z;
         normals[i].m_x = contacts[i].m_normal.m_x * swapContactScale; 
         normals[i].m_y = contacts[i].m_normal.m_y * swapContactScale; 
         normals[i].m_z = contacts[i].m_normal.m_z * swapContactScale; 
         penetration[i] = contacts[i].m_penetration;
      }
   }
   return count;
}


I checked in the fix now, try again.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Fri Aug 31, 2012 9:56 am

Before the last change, the resulting normal in your scenario was (0, 1, 0).

The orientation itself is not important to me either, as long as I know it and as long as it is always the same and doesn't change randomly. So if you prefer (0, -1, 0) as a result from now on, that's fine with me, I can adjust that in my code.
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Fri Aug 31, 2012 10:01 am

I tested it again and now the normals are oriented like in newton2xx again.
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Fri Aug 31, 2012 10:30 am

Ok good, remeber I still need to finish some ot the edge cases of the closest disance, plus I need to conver the old contact calculation when in penetrations. I will do that this weekend.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Fri Aug 31, 2012 11:51 am

Ok I just fix the last bug on closet distance whne teh shape are no intersectiong, now I need to go ove teh part when teh intersect.

that part is still fleaky, but I will fix thsi weekend.

The test that I check in has calst 225 closest disatnce between a cylinder and a generic conves shape. the tah cast part take abput .5 milisecund in one core.
that's 2.2 microsecund per closest distance between tow generic convex shapes using one core in x87 code in visual studion 2008

in visual studio 2010, 64 bit mode this is actually twice as fast. I do not knwo why but I am glad it is.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Tue Sep 04, 2012 4:59 pm

I tried it again. It still looks as if it behaves the same. NewtonCreateBody got split into dynamic and kinetic - I'm not exactly sure what the difference is. I just replaced all my current bodies with dynamics. Also NewtonCollisionUpdate got removed. I remember you said some time ago that it isn't needed anymore in newton3xx.

Any update/decision on the user collision in newton3xx?
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Tue Sep 04, 2012 5:34 pm

what does this mean? I am confused.
"tried it again. It still looks as if it behaves the same"

on the collision step, yes the collsion step is not needed, I thonk, you can just get the contact from a shape by iteration over ths contact joints
like this
Code: Select all
      for (NewtonJoint* joint = NewtonBodyGetFirstContactJoint(m_tornadoBody); joint; joint = NewtonBodyGetNextContactJoint (m_tornadoBody, joint)) {
         dFloat Ixx;
         dFloat Iyy;
         dFloat Izz;
         dFloat mass;

         NewtonBody* const body0 = NewtonJointGetBody0(joint);
         NewtonBody* const body1 = NewtonJointGetBody1(joint);
         NewtonBody* const body = (NewtonBodyGetUserData(body0) == this) ? body1 : body0;   

also I will make a function like convex cast that will allow to collide an arvitrary shape against the world.
they will make up for the collision step.


on the last question, the kinematic body demo is for testing the the new collsion in very deep penetration.
currentlly all version of newton fail to calculate contact if a body is fully immerse onto anothe body. only the special case shape like sphere and capsuel dealt with that.
now all shape must support that funtionality for good triggers and kinematic behaviour
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Thu Sep 06, 2012 5:07 am

Julio Jerez wrote:what does this mean? I am confused.
"tried it again. It still looks as if it behaves the same"

I just wanted to say: nothing got broke or changed significantly since my last update.

on the collision step, yes the collsion step is not needed, I thonk, you can just get the contact from a shape by iteration over ths contact joints
like this
Code: Select all
      for (NewtonJoint* joint = NewtonBodyGetFirstContactJoint(m_tornadoBody); joint; joint = NewtonBodyGetNextContactJoint (m_tornadoBody, joint)) {
         dFloat Ixx;
         dFloat Iyy;
         dFloat Izz;
         dFloat mass;

         NewtonBody* const body0 = NewtonJointGetBody0(joint);
         NewtonBody* const body1 = NewtonJointGetBody1(joint);
         NewtonBody* const body = (NewtonBodyGetUserData(body0) == this) ? body1 : body0;   


Ok. so this does give me the same as the collision callback gave me before?
So right now, when I add a body to the scene, the collisions get updated immediately? But the collision callback only gets called on a NewtonUpdate - not every time a body gets added and new collisions get gerenated by that? Is that correct?

also I will make a function like convex cast that will allow to collide an arvitrary shape against the world.
they will make up for the collision step.

That would come in handy :)

on the last question, the kinematic body demo is for testing the the new collsion in very deep penetration.
currentlly all version of newton fail to calculate contact if a body is fully immerse onto anothe body. only the special case shape like sphere and capsuel dealt with that.
now all shape must support that funtionality for good triggers and kinematic behaviour

I see. Is this a mandatory requirement for every shape in newton3xx? I guess I can say the usercollision like it was in newton2xx goodbye then?
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Thu Sep 06, 2012 9:04 am

Oh I see, you were updation collsion afte you were adding a new body to the world.

The problem wit teh old collsion update, is that I casue problems with if some one call it form a newton update, this si becaus ethe engine use a two threads to mak ethe engine update concurrent and also run on his own thread.
a new update will brake tah model.
whe I can do is liek I said before add a funtion to coplemnet teh conve cast function. thsi will be asycrosnis and can be called from anywhere just like convex cast is now, and tha will solve that problem.

we have this funtion now
int NewtonWorldConvexCast (const NewtonWorld* const newtonWorld, const dFloat* const matrix, const dFloat* const target, const NewtonCollision* const shape, dFloat* const hitParam, void* const userData,
NewtonWorldRayPrefilterCallback prefilter, NewtonWorldConvexCastReturnInfo* info, int maxContactsCount, int threadIndex);

I can add this one:
int NewtonWorldColide (const NewtonWorld* const newtonWorld, const dFloat* const matrix, const NewtonCollision* const shape, void* const userData, NewtonWorldRayPrefilterCallback prefilter, NewtonWorldConvexCastReturnInfo* info, int maxContactsCount, int threadIndex);



on the user shape, just let ne complete the Kinamtic body feature and I will complete that after.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Marc » Thu Sep 06, 2012 12:04 pm

Ok.

Just for completeness: I didn't use NewtonCollisionUpdate() in the world I use NewtonUpdate() in. I have a world that simulated physics and that gets updated with NewtonWorld(). I have another newtonworld parallel to that where logical collisionshapes are in that never get physically simulated. For example enemies have different hit collisions than they have for physical collision. So I use their physical collision shapes in the world I simulate the physics in and then update the position in the game logic world. After that I only did a NewtonCollisionUpdate() there.

I might have been able to do all that with only one world and a lot of materials and filtering, but I think that'll get quite complicated fast. Besides I'm not sure if its that efficient. Basically there would be two classes of bodies and only class 0 interferes with bodies from class0 and only class 1 intereferese with bodies from class 1. Then again, I'm not sure if having two newtonworlds and always copying the positions from one world to the other is most efficient. So only the complication argument stands.
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: Optimize Marc's Character Controller / Batching Convex C

Postby Julio Jerez » Thu Sep 06, 2012 12:23 pm

I think that it may be better using one world, and now wit teh kinamtic body you can make your proxy a kenematoc body and it will be update and filterd autiomatically.
kinamatic body get contacts by they do no intefered with teh dynamics bodies.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron