Jump to content
  • Advertisement
Sign in to follow this  
Kurt-olsson

2D Collision detection and response complete, any suggestions?

This topic is 2567 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

Hello.

Finnaly after 4 days my collision detection and response routine is done. The sliding and response is smooth and it works against all angles. both in small angles between lines in a small space and
lines that just have a few % climb or slope. I havent found a bug so far.

But you gurus that know math better than me, if you find something strange of something that is done with not smart way, please tell me.
I am planning to implement this in 3D now when it is working in 2D.

Thanks for all help in previous posts to get this working.


//COLLISION RESPONSE
//vx = player velocity x
//vy = player velocity y (is set to standard of 4 for gravity) this is 2D, player can only move left and right
var move_circle = function (recursions) {
//This return collision package with time t and distance etc.
var overbounce = 0.1;
var RADIUS = 20;

//If we bounce more than five times, we are in a tricky place, bail out and set velocity to 0;
if (recursions > 5) {
vx = 0;
vy = 0;
return;
}
//Get collision returns a collision object that includes the earlist collision.
//time = time of collision
//nx = normal (x) of the line/plane OBS! this is the normal from the closest point on line to the sphere, not 90 degree angle from line.
//ny = normal (y) of the line/plane OBS! this is the normal from the closest point on line to the sphere, not 90 degree angle from line.
var c1 = new get_collision_time(vx, vy, c.x, c.y);
if (c1.time != 1) {
//First correct the first movement to get the sphere close to the intersection - overbounce, not quite there.
c.x += vx * c1.time - overbounce;
c.y += vy * c1.time - overbounce;
//Get a new collision object because we have moved the sphere.
var c1 = new get_collision_time(vx, vy, c.x, c.y);
//Calculate the speeds sliding, set new velocity to slide against the line/plane that was closest.
vx -= c1.nx * ((vx * c1.nx) + (vy * c1.ny));
vy -= c1.ny * ((vx * c1.nx) + (vy * c1.ny));
//Up with recursions and recalculate collision with new velocity;
recursions = recursions + 1;
move_circle(recursions);
}

//If the player is free and sets off for a new velocity, just check before that the sphere isn´t touching any lines /speher is too close.
//Free the player by pushing out from all lines.
for (var l = 0; l < lines.length; l++) {
var closestpoint = new ClosetPointOnLine(lines[l].x, lines[l].y, lines[l].x2, lines[l].y2, c.x, c.y);
var d = lineLength(c.x, c.y, closestpoint.x, closestpoint.y);
if (d < RADIUS) {
var nx = ((c.x - closestpoint.x) / d);
var ny = ((c.y - closestpoint.y) / d);
var EPSILON = 0.00001;
var push = (RADIUS - d) - EPSILON;
c.x += (nx * push);
c.y += (ny * push);
}
}
}

Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!