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:
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
if(collision) do something?
Re:
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
Cheers,
PI
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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.
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:
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());
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way