Collision detection and sliding problem.

Subscribe to Collision detection and sliding problem. 6 posts, 5 voices

Sign in to reply


 
avatar for GhostIV GhostIV 15 posts
Flag Post

So ok. Im using CDK for my collision detection. So far, its been really helpfull.
But the function i wrote(with the help of cdk examples) just makes the players object(the tank) stop instead of continuing along the border of the object.

this is the game so far:
http://www.swfcabin.com/open/1257644383

the code:


function checkCollisions(){
colResultArray=colList.checkCollisions();

if(colResultArray.length)
{
var collision:Object = colResultArray0;
var angle:Number = collision.angle;
var overlap:int = collision.overlapping.length;

var sin:Number = Math.sin(angle);
var cos:Number = Math.cos(angle);

var vx0:Number = Chasis.vx * cos + Chasis.vy * sin;
var vy0:Number = Chasis.vy * cos – Chasis.vx * sin;

vx = vx0 * cos – vy0 * sin;
vy = vy0 * cos + vx0 * sin;
}
}

arrow rotates in the direction of vx and vy.

Chasis.vx and Chasis.vy is the current x and y speed of the tank.
after vx and vy are calculated, the same movement algorythm used to mave the object normally is used, but the vx and vy are reversed(-= instead of +=)

How do i make it continue moving along the border? Making it less bumpy would also help.

 
avatar for UnknownGuardian UnknownGuardian 1704 posts
Flag Post

Make it something like this

if hit test with object
{
    inverse(multiply by -1) velocity of both x and y
}
 
avatar for SuperMarioJump SuperMarioJump 223 posts
Flag Post

I’ve not used the CDK, but in my collision detection stuff I’ve used the overlap to push the player out of the object it’s colliding with, which you seem not to.

 
avatar for Wordblind Wordblind 119 posts
Flag Post
var vx0:Number = Chasis.vx * cos + Chasis.vy * sin;
var vy0:Number = Chasis.vy * cos - Chasis.vx * sin;
vx = vx0 * cos - vy0 * sin;
vy = vy0 * cos + vx0 * sin;

You just performed an identity operation. It is like you did this:

var mat:Matrix = new Matrix();
mat.rotate(-angle);
mat.rotate(angle);
v = mat.transformPoint(v);

If you want to kill of the velocity perpendicular to the impact, what you want is more like:

mat.rotate(-angle);
mat.scale(0,1);
mat.rotate(angle);

I think that wold translate to:

var vy0:Number = Chasis.vy * cos - Chasis.vx * sin;
vx = - vy0 * sin;
vy = vy0 * cos;
 
avatar for thepeasant thepeasant 160 posts
Flag Post

3 steps for collision detection:

1) check when two things overlap
2) make the objects no longer overlap
3) adjust velocity

Most people forget the second step.

 
avatar for GhostIV GhostIV 15 posts
Flag Post

Thank you everyone for posting, but none of your advice helped:(
But after some thinking and testing i found out that all i need is to find a way to get the exact angle towards the collision. Or at least in which 90 degree sector it happened. Then i can just block the tanks movement in that direction, while still enabling it to move along the border.
Is there a way to do that in CDK?

Sign in to reply


Click Here