Clamping
by Dannon Gruver · in Torque Game Builder · 06/06/2005 (3:17 pm) · 7 replies
I know its coming in the next release, but does anyone have code to perform a clamping collision response for tile maps?
I'm practically foaming at the mouth for this one :)
You guys are great!!
I'm practically foaming at the mouth for this one :)
You guys are great!!
#2
%srcObj.setAtRest()
in the onCollision event when it hits a tile. Not sure if thats what you want ?
06/09/2005 (6:57 pm)
.setAtRest() is that the type you want ? disable physics and set the object to do a collision callback%srcObj.setAtRest()
in the onCollision event when it hits a tile. Not sure if thats what you want ?
#3
06/09/2005 (10:34 pm)
What is clamping? sorry for my ignorance.......trying hard though...
#4
Scott: A good example of "clamping" is when a character hits a wall and slides against instead of bouncing off it.
06/10/2005 (7:19 pm)
Mathew: I *really* appreciate the help. Thanks :) I'll try this tonight.Scott: A good example of "clamping" is when a character hits a wall and slides against instead of bouncing off it.
#5
Vf = Vo - ((N DOT Vo)N)
where:
Vf = Velocity Final
Vo = Velocity Original
N = Collision Normal
DOT = Dot Product of two vectors
That is to get the objects linear velocity and subtract from this, the product of (the collision normal DOT the original velocity) by the collision normal.
Here it is in C++...
... and in script...
This is just typed and untested so you may want to check the syntax!!!!
Note that this version does not maintain the original velocity magnitude although this is the standard behaviour required in games. If you push directly against a wall, you don't move but if you push forward and slightly to the side, you'll slide along the wall albeit slowly.
Of course, you won't have to do any of this junk in the next version. :)
Hope this helps,
- Melv.
06/11/2005 (2:04 am)
The way to achieve this is to do a bit of simple algebra like so...Vf = Vo - ((N DOT Vo)N)
where:
Vf = Velocity Final
Vo = Velocity Original
N = Collision Normal
DOT = Dot Product of two vectors
That is to get the objects linear velocity and subtract from this, the product of (the collision normal DOT the original velocity) by the collision normal.
Here it is in C++...
[b]// Fetch Linear Velocity / Collision Normal.[/b] t2dVector velOriginal = getLinearVelocity(); t2dVector collisionNormal = mCollisionNormal; [b]// Normalise Collision Normal (unit normal).[/b] collisionNormal.normalise(); [b]// Calculate New Clamped Velocity.[/b] t2dVector velFinal(velOriginal-(collisionNormal*(collisionNormal * velOriginal)) ); // The final * is dot-product! [b]// Set new Linear Velocity.[/b] setLinearVelocity( velFinal );
... and in script...
[b]// Fetch Linear Velocity[/b] %velOriginal = %obj.getLinearVelocity(); [b]// Calculate New Clamped Velocity.[/b] %velFinal = vectorSub2D( %velOriginal, vectorScale2D(%collisionNormal, vectorDot2D(%collisionNormal, %velOriginal) ) ); [b]// Set new Linear Velocity.[/b] %obj.setLinearVelocity( %velFinal );
This is just typed and untested so you may want to check the syntax!!!!
Note that this version does not maintain the original velocity magnitude although this is the standard behaviour required in games. If you push directly against a wall, you don't move but if you push forward and slightly to the side, you'll slide along the wall albeit slowly.
Of course, you won't have to do any of this junk in the next version. :)
Hope this helps,
- Melv.
#6
Melv: Thank you very much!! I know you're busy and sorry about the impatience. The snippet helped a lot and I combined it with Edo Broekman's code
(b/c of a tiny problem w/ gravity which u already know about) and things work very nicely. :)
JOY!!
06/13/2005 (3:36 pm)
Matt: I tried the setAtRest() but it stops the sprite from sliding along the wall. Again though, I appreciate it.Melv: Thank you very much!! I know you're busy and sorry about the impatience. The snippet helped a lot and I combined it with Edo Broekman's code
(b/c of a tiny problem w/ gravity which u already know about) and things work very nicely. :)
JOY!!
Torque Owner Dannon Gruver