Jump to content
  • Advertisement
Sign in to follow this  
homojedi

collision response using MTD

This topic is 2948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

greetings,

ive just managed to implement MTD but its acting rather strange, the collision response ends up in jumps between the two colliding bodies, i dont have velocity or anything as such i just wish for the mtd to push back the intersecting bodies, too a point when there not intersecting and keep them there, say like a character im controlling going into a wall or even something that isnt static, rather than the jumpy behaviour ive got going on now.

so here is how i compute the mtd if there is an intersection, it looks right but just in case

//if it reaches this point there is an intersection
float intersectionDepth = (minThisPoly < minOtherPoly) ? (maxThisPoly - minThisPoly) : (minThisPoly - maxOtherPoly);
float mtdLength2 = [CInfo MTD].x * [CInfo MTD].x + [CInfo MTD].y * [CInfo MTD].y;
float mtdLength = sqrt(mtdLength2);

if((intersectionDepth < mtdLength) || (mtdLength == 0.0f))
{
DS2DVector MTV = DS2DVectorMultiply( axis, intersectionDepth );
[CInfo setMTD:MTV];
}



and this is how i apply it

info = [collidableA isIntersectingWith:collidableB];
if( [info isIntersecting] )
{
DS2DTransform *tn1 = [[[DS2DTransform alloc]init]autorelease];
DS2DTransform *tn2 = [[[DS2DTransform alloc]init]autorelease];
DS2DVector MTDhalf = [info MTD];
MTDhalf = DS2DVectorMultiply(MTDhalf, 0.5);

DS2DVector posB = DS2DVectorAdd([[objectA get_entityCurrentPos] pos], MTDhalf);
DS2DVector posA = DS2DVectorSubtract([[objectB get_entityCurrentPos] pos], MTDhalf);

[tn1 setPos:posA];
[tn2 setPos:posB];
[tn1 setRot:[[objectA get_entityCurrentPos]rot]];
[tn2 setRot:[[objectB get_entityCurrentPos]rot]];

//resolve collision
[objectA set_entityCurrentPos:tn1];
[objectB set_entityCurrentPos:tn2];
}



i know how im applying it is probably where im going wrong but its 2:30 and i'm brain dead, maybe i should include inverse masses, and just make anything the player collides into have an inversemass of 0. this makes sense to me now, but as i said its rather early, this may purely be delusional

cheers

EDIT: realised what i was doing to find the over lap instead of

(minThisPoly < minOtherPoly) ? (maxThisPoly - minThisPoly) : (minThisPoly - maxOtherPoly);

it is now

(minThisPoly < minOtherPoly) ? (minOtherPoly - maxThisPoly) : (maxOtherPoly - minThisPoly);

and this

DS2DVector posB = DS2DVectorAdd([[objectA get_entityCurrentPos] pos], MTDhalf);
DS2DVector posA = DS2DVectorSubtract([[objectB get_entityCurrentPos] pos], MTDhalf);

has been changed but all ive done is swapped posA for PosB so the mtdHlad gets added to and subtracted from B

Although the results are slightly more aesthetic, it still jumps sometimes upon collision, i have heard of people having similiar problems then they collide a tri angle against a rectangle, the MTD becomes much larger than it should be and thye kind of shoot off away from each other, in my case sometimes not even along the correct axis to seperate them. Any clues?



[Edited by - homojedi on August 26, 2010 2:03:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
I fixed it, and i write this with the intention on telling you how i achieved this, but I honestly dont know i shall just post my revised code which did the rtick in case anyone else has similar problems in the future


//if it reaches this point there is an intersection
/*which side are we colliding on? since we have reached this far into the code it means
there is an overlap, therefore if the min of this poly is less than the min of the other poly
it means this polly is colliding on the left of the other poly if its not its on the right*/

float intersectionDepth = ( minThisPoly - minOtherPoly )?(minOtherPoly - maxThisPoly):(maxOtherPoly - minThisPoly);

//This is good to have regardless if your axis are normalised or not as if they are it wont affect them anyway
float axislength_sqr = DS2DVectorDot(axis, axis);
DS2DVector seperation = DS2DVectorMultiply( axis, (intersectionDepth/axislength_sqr) ) ;

float sepSquared = DS2DVectorDot(seperation, seperation);

if( (sepSquared < [CInfo MTDLengthSqr]) || ([CInfo MTDLengthSqr] < 0.0f) )
{
[CInfo setMTDLengthSqr:sepSquared];
[CInfo setMTD:seperation];
}


good luck

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!