if(collision) do something?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
dybalabj
Posts: 6
Joined: Mon Oct 29, 2007 6:42 pm

if(collision) do something?

Post by dybalabj »

I've been working on a hovercraft racing game for a school project. It mostly works, but now we're trying to implement some basic physics. In particular, I want to call a function based on a collision event; i.e. if my craft collides with the track wall, then add damage points and bounce off (rather than just stop or get all jittery on me). The collision itself works just fine, I just need to know how to perform some action based on the collision.

I'd like to use the ISceneCollisionManager function getCollisionPoint(), but I don;'t know what line I should pass to the function. The triangle selector is obviously the one for my track mesh. I cannot simply do collision by bounding box of the track b/c my racer is completely w/in the bounding box of the track. Below is a screenshot:

Image

I know the common suggestion is to use a separate physics engine like Newton and its various wrappers. I've already tried that and didn't have much luck. In particular I tried using IPhysics and the problem there was figuring out how to manually apply forces to move my craft (simple changing position effectively cancels out the collision and my craft falls through the floor). I think part of the issue is also that the engines I've tried are not fully compatible with Irrlicht 1.3.1. I am compiling with Visual Studio 2005 Standard Edition.

Thanks,
Dybs
PI
Posts: 176
Joined: Tue Oct 09, 2007 7:15 pm
Location: Hungary

Re:

Post by PI »

Hello. It's possible to check if a ray hits any of the surfaces of your triangle selector, so my workaround idea for you would be to check e.g. 6 or X directions (with a little longer ray than the radius of the craft is) if there was a collision or not. From the actual velocity strength of the craft you can compute the value of the bounce; and from the direction and velocity vector you can get the desirable rotations. This is how my ball-physics works, however, this is just a slow and not very punctual workaround. I'd also enbrave you to use a physics engine, or to create your own collision-physics library, which is really tough, though.

Cheers,
PI
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Off the top of my head...

Don't use a collision response animator. Instead, create a movement vector for your hovercraft and call getCollisionResultPosition(). If the returned position isn't what you'd expect (i.e. != (ellipsoidPosition + ellipsoidDirectionAndSpeed)), then a collision has occurred.

However, instead of just moving your node to there, call triout.getNormal() to get the normal for the collision. You don't know the exact position, but then your collision ellipse is only approximate anyway, so you could just assume that it's an average of the triangle's 3 points. This will allow you to calculate a reflection vector for the hovercraft, i.e. the direction it should bounce off in, and presumably its new direction vector.

Code: Select all

			core::vector3df const normal(hitTriangle.getNormal().normalize());
			core::vector3df const collisionPosition((hitTriangle.pointA + hitTriangle.pointB + hitTriangle.pointC) / 3);
			core::vector3df const incidentVector((collisionPosition - hoverCraftPosition).normalize());

			// R = V - ( 2 * V [dot] N ) N 
			core::vector3df const reflectionVector((incidentVector - ((2.f * incidentVector.dotProduct(normal)) * normal)).normalize());
The position after the collision won't be the one returned by getCollisionResultPosition(). Instead, calculate the distance to the collision point, subtract that from how far the hovercraft should have moved, and use that to work out how far it should bounce after the collision:

Code: Select all

			f32 distanceToMove = movementVector.getLength();
			distanceToMove -= (collisionPosition - hovercraftPosition).getLength();
			hovercraftPosition = collisionPosition + (reflectionVector * distanceToMove);
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
dybalabj
Posts: 6
Joined: Mon Oct 29, 2007 6:42 pm

Post by dybalabj »

PI,

Thanks for the suggestion. Although it may be slow, that makes good sense and I can't believe I didn't think of it before :)

Dybs
PI
Posts: 176
Joined: Tue Oct 09, 2007 7:15 pm
Location: Hungary

Re:

Post by PI »

I'm glad I could help. I can support you with some code if you're interested, though it's for ball physics, it might be useful, just let me know.

Cheers,
PI
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

post your code (link to the project) and maybe i could help.
Post Reply