Game Development Community

figuring and calculating, dstObject collision velocities

by rennie moffat · in iTorque 2D · 11/14/2010 (1:26 am) · 4 replies

This may sound like a very newbish question, but if I have an object, we'll call it object A, and it collides with objectB, both are moving, have velocities. Upon collision, in objectA's behavior, I want to calculate not only the owner (objectA) velocity but that of the objectB at collision time. How would I do that?



This is where I am confused...


function onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
if(%dstObject $= "objectBClass")
%srcObject.reactToCollision()
}

function reactToCollision(%this)
{
	%playerVelocityX = %this.owner.getLinearVelocityX();
	%playerVelocityY = %this.owner.getLinearVelocityY();
	
	/// and this is where I get confused, how do I insure I calculate objectB's velocity
	%oppPlayerVelocityX = ???????
	%oppPlayerVelocityY = ???????

///then upon getting the velocities, I want to calculate the result and am not sure which vector calculation to use
}


I am wondering, a. how do I get that objects velocity and calculate the resulting vectors and b. should I just use the engines collision responses?





Thanks for any feedback.





About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
11/14/2010 (10:39 am)
Rennie, you have both the objects that took part in the collision in the objects that get passed to the onCollision callback:

%srcObject is object A
%dstObject is object B

You can get the velocities using the various getVelocity methods.

I'm not sure which vectors you are trying to get hold of in your question. The object positions at impact will be in the getPosition methods on %srcObject and %dstObject.
#2
11/14/2010 (11:04 am)
Looking at your code some more and keeping what you have ... try this ...

function onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)  
{  
if(%dstObject $= "objectBClass")  
%srcObject.reactToCollision(%dstObject)  
}  
  
function reactToCollision(%this, %dstObject)  
{  
    %playerVelocityX = %this.owner.getLinearVelocityX();  
    %playerVelocityY = %this.owner.getLinearVelocityY();  
      
    /// and this is where I get confused, how do I insure I calculate objectB's velocity  
    %oppPlayerVelocityX = %dstObject.getLinearVelocityX();  
    %oppPlayerVelocityY = %dstObject.getLinearVelocityY();  
  
///then upon getting the velocities, I want to calculate the result and am not sure which vector calculation to use  
}
#3
11/14/2010 (12:59 pm)
Oh ok. I am not sure why I always thought of onCollision functions to be a "one off", "one shot" deal. So I will just use that.

Regarding the calculations of the vectors, what I am doing is building a sports game. Figure this scenario.

Two players collide, one player who was holding the ball has it dislodged. Now the resulting vectors of each object must be figured. Calculate playerA, playerB and the balls new vectors.

That is what I am working with and what I am trying to figure out. I figure that one of the vector calculations should do it but not sure which, if any. I know I should revisit my grade 12 physics text.
#4
11/14/2010 (4:06 pm)
Of course the other question is do I just use the engines collision responses. I have not yet tested. Will be. Thanks.