Vehicle/Rigid Collision Fixes
by Ross Pawley · in Torque Game Engine · 06/11/2007 (10:23 pm) · 115 replies
Hey everyone, I just wanted to post these early fixes to the frequent and frustrating vehicle collision problems people have had with TGE. We spent a few hours today at the office going through things and this new code works relatively well with some small issues, mainly interpenetration. However, it's a far sight better than what was there, which was just calling into the collision routine for immobile ridgid objects (and thus unsuitable for vehicles entirely).
In rigid.cpp replace these functions:
(CONTINUED)
In rigid.cpp replace these functions:
bool Rigid::resolveCollision(const Point3F& p, Point3F normal, Rigid* rigid)
{
atRest = false;
Point3F v1,v2,r1,r2;
getOriginVector(p,&r1);
getVelocity(r1,&v1);
rigid->getOriginVector(p,&r2);
rigid->getVelocity(r2,&v2);
// Make sure they are converging
F32 nv = mDot(v1,normal);
nv -= mDot(v2,normal);
if (nv > -0.001f)
return false;
// Compute impulse
F32 d, n = -nv * (1 + restitution * rigid->restitution);
Point3F a1,b1,c1;
mCross(r1,normal,&a1);
invWorldInertia.mulV(a1,&b1);
mCross(b1,r1,&c1);
Point3F a2,b2,c2;
mCross(r2,normal,&a2);
rigid->invWorldInertia.mulV(a2,&b2);
mCross(b2,r2,&c2);
Point3F c3 = c1 + c2;
d = oneOverMass + rigid->oneOverMass + mDot(c3,normal);
Point3F impulse = normal * (n / d);
applyImpulse(r1,impulse);
impulse.neg();
rigid->applyImpulse(r2,impulse);
return true;
}
bool Rigid::resolveCollision(const Point3F& p, Point3F normal)
{
atRest = false;
Point3F v,r;
getOriginVector(p,&r);
getVelocity(r,&v);
F32 n = -mDot(v,normal);
if ( n < 0 )
return false;
// Collision impulse, straight forward force stuff.
F32 d = getZeroImpulse(r,normal);
F32 j = n * (1 + restitution) * d;
Point3F impulse = normal * j;
// Friction impulse, calculated as a function of the
// amount of force it would take to stop the motion
// perpendicular to the normal.
Point3F uv = v + (normal * n);
F32 ul = uv.len();
if (ul) {
uv /= -ul;
F32 u = ul * getZeroImpulse(r,uv);
j *= friction;
if (u > j)
u = j;
impulse += uv * u;
}
//
applyImpulse(r,impulse);
return true;
}(CONTINUED)
#42
06/28/2007 (10:38 am)
@Harvey - I'm not sure. For our purposes this was all for LAN play, so we haven't event tested that. I doubt the collision changes will make an impact, positive or negative, on network performance. The only thing you can do there is reduce the amount of data your game is sending between the server and client.
#43
06/28/2007 (11:29 am)
@Tom - Yeah, sounds like a plan, we think it's something to do with us using the advanced camera too for some reason but haven't tested it. As I said, good work, and best of luck with the collision stuff!!
#44
www.garagegames.com/mg/forums/result.thread.php?qt=61473
Try what I posted there (it just doesn't belong here is why I link)
06/28/2007 (2:08 pm)
@Harvey see:www.garagegames.com/mg/forums/result.thread.php?qt=61473
Try what I posted there (it just doesn't belong here is why I link)
#45
06/28/2007 (2:53 pm)
Massive THANK YOU Lee, that looks very promising, we'll have a look into it. 8) .H
#46
07/01/2007 (12:26 pm)
This has been used in TGE, but we're seeing using it in TGEA with much success although a bit rough at points still. The only issue we're having now is it has affected the generation of Atlas Terrain. :( It seems that it can't write out collision data for some reason without crashing, I haven't had time to debug it though
#47
07/08/2007 (12:25 am)
So um.... anything new?
#49
working on another collision aspect at the moment, and ended up realising that there was no client-side collision list, so added in
and replaced
can't remmeber offhand if there wasn't also a check i needed to delete or not, but the main point is adding in client-side impulse addition so it's not waiting that 10th of a second(see comentary for the CollisionTimeout definition for explaination there)+transmission time before it gets the impulses form the server... don't see any *obvious* flaws in the logic, but could be missing something...
07/16/2007 (11:17 am)
Interesting observation this end:working on another collision aspect at the moment, and ended up realising that there was no client-side collision list, so added in
void Vehicle::updatePos(F32 dt)
...
else {
notifyCollision();
// Play impact sounds on the client.
if (collided) {and replaced
bool Vehicle::resolveCollision(Rigid& ns,CollisionList& cList)
// Keep track of objects we collide with
//if (!isGhost() && c.object->getTypeMask() & ShapeBaseObjectType)
if (c.object->getTypeMask() & ShapeBaseObjectType)can't remmeber offhand if there wasn't also a check i needed to delete or not, but the main point is adding in client-side impulse addition so it's not waiting that 10th of a second(see comentary for the CollisionTimeout definition for explaination there)+transmission time before it gets the impulses form the server... don't see any *obvious* flaws in the logic, but could be missing something...
#50
07/18/2007 (7:46 pm)
Ross or Tom, anything new happening with this?
#51
07/19/2007 (9:37 am)
@Ron - We've been slogging thru it the last couple of weeks on and off. The collision issues are hard. ;)
#52
07/19/2007 (1:53 pm)
Hard, pfah! That's why everyone codes collision fixes...oh erm um...nevermind.
#53
08/14/2007 (3:19 pm)
Well its been nearly a month, Anything new to report?
#54
tweet!
08/14/2007 (3:33 pm)
We are like newborn sparrows in your garage, mouths permanently open to the sky, pleading "feed me, feed me!"tweet!
#55
I have a question though. How did you go about making the vehicle and rigidshape recognize each other as freinds so they could look at each other's rigid info? Did you make them friend classes or what?
I ask because using your code I get this:
C:\Torque\SDK\engine\game\vehicles\vehicle.cc(1171): error C2027: use of undefined type 'RigidShape'
../engine\game\shapeBase.h(73) : see declaration of 'RigidShape'
09/03/2007 (4:02 am)
Lee, I am doing what you said above for the RigidShapes. Turns out I need to for other reasons dealing with vehicle radar. Doesn't really do to have boulders registering as vehicles.I have a question though. How did you go about making the vehicle and rigidshape recognize each other as freinds so they could look at each other's rigid info? Did you make them friend classes or what?
I ask because using your code I get this:
C:\Torque\SDK\engine\game\vehicles\vehicle.cc(1171): error C2027: use of undefined type 'RigidShape'
../engine\game\shapeBase.h(73) : see declaration of 'RigidShape'
#56
Find in Files is your friend :-)
09/03/2007 (3:07 pm)
Ron, I'm working on getting a more direct explanation from my buddy who did that. I recall that the key bit was "a new SimObjectType enum for RigidShape (previously, the mTypeMask of RigidShape was VehicleObjectType)."Find in Files is your friend :-)
#57
I'm reasonably sure it's objectTypes.h, the section that looks like this:
GameBaseObjectType = BIT(10),
ShapeBaseObjectType = BIT(11),
CameraObjectType = BIT(12),
StaticShapeObjectType = BIT(13),
PlayerObjectType = BIT(14),
ItemObjectType = BIT(15),
VehicleObjectType = BIT(16),
VehicleBlockerObjectType = BIT(17),
ProjectileObjectType = BIT(18),
ExplosionObjectType = BIT(19),
CorpseObjectType = BIT(20),
DebrisObjectType = BIT(22),
PhysicalZoneObjectType = BIT(23),
StaticTSObjectType = BIT(24),
AIObjectType = BIT(25),
StaticRenderedObjectType = BIT(26),
What Kurt did was swap out one of these that I wasn't using and created RigidShapeType. I'll find it. The reason we didn't post it at the time was because we knew it's a bit of a hack (*ahemslightly*), and might not work for everybody. I want to say we subbed AIObjectType, 'cause my game doesn't use 'em.
09/03/2007 (7:12 pm)
Ron, I'm going to search my backups tomorrow. I knew I'd backed this stuff out for my alpha release, but I thought I'd left some of the infrastructure behind--apparently not.I'm reasonably sure it's objectTypes.h, the section that looks like this:
GameBaseObjectType = BIT(10),
ShapeBaseObjectType = BIT(11),
CameraObjectType = BIT(12),
StaticShapeObjectType = BIT(13),
PlayerObjectType = BIT(14),
ItemObjectType = BIT(15),
VehicleObjectType = BIT(16),
VehicleBlockerObjectType = BIT(17),
ProjectileObjectType = BIT(18),
ExplosionObjectType = BIT(19),
CorpseObjectType = BIT(20),
DebrisObjectType = BIT(22),
PhysicalZoneObjectType = BIT(23),
StaticTSObjectType = BIT(24),
AIObjectType = BIT(25),
StaticRenderedObjectType = BIT(26),
What Kurt did was swap out one of these that I wasn't using and created RigidShapeType. I'll find it. The reason we didn't post it at the time was because we knew it's a bit of a hack (*ahemslightly*), and might not work for everybody. I want to say we subbed AIObjectType, 'cause my game doesn't use 'em.
#58
What I want to achieve is a car colliding with an arbitrary road (with a a lot of hills and valleys), so we don't have to generate brushes or use the terrain for collisions.
I have integrated polysoup and the changes proposed in this thread in TGEA 1.0.3.
The car collides, but in a few meters it gets stuck. It seems that the wheels are penetrating the road...
Has anyone achieved this to work well? Tom, you say that you have it working with Polysoup... what are u testing it against, an arbitrary road or something more simple?
Thank u!!
10/10/2007 (7:28 am)
Hi everybody!!What I want to achieve is a car colliding with an arbitrary road (with a a lot of hills and valleys), so we don't have to generate brushes or use the terrain for collisions.
I have integrated polysoup and the changes proposed in this thread in TGEA 1.0.3.
The car collides, but in a few meters it gets stuck. It seems that the wheels are penetrating the road...
Has anyone achieved this to work well? Tom, you say that you have it working with Polysoup... what are u testing it against, an arbitrary road or something more simple?
Thank u!!
#59
Perhaps you should try backing out this code as a test? I don't think it was written with polysoup collision in mind. Another thing I know matters with polysoup is not to let your vertices get too dense in one place.
10/10/2007 (3:08 pm)
@Adternative: I'm using Polysoup as well in TGEA 1.0.3, but I haven't yet implemented any of the code in this thread. Cars are working just fine for me, though. It's planes that have collision issues. Perhaps you should try backing out this code as a test? I don't think it was written with polysoup collision in mind. Another thing I know matters with polysoup is not to let your vertices get too dense in one place.
#60
Also, Any updates to this? And thank you for your hard work.
10/28/2007 (11:48 pm)
While I am getting a huge improvement over my collisions prior to this code, I occasionaly get a crash from multiple doing such things as demolition derby style collisions. Any idea why?Also, Any updates to this? And thank you for your hard work.
Harvey Greensall
Valiant effort in tackling this by the way, I'll have to head over to torsion to support you guys. .. H.