collision code

Started by
15 comments, last by WillTice 12 years ago
Well I am studying this piece of code.I am trying to develop a bounding box collision detection algorithm.this one appears to use 2 circles.I will do more research using google.
[font="Consolas"][size="2"][font="Consolas"][size="2"][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]bool detectcollision(int x1, int y1, int radis1, int x2, int y2, int radis2)
{
double xd, yd, Distance;

//2d
xd = x2-x1;
yd = y2-y1;
Distance = Math.Sqrt(xd * xd + yd * yd);


if( radis1 + radis2 >= Distance)
return true; //collision
return false; //no collision
}
[/font][/font]
Advertisement
What exactly are you asking? Basically, the logic is that you find the distance between the two centers of the circles using the distance formula. Then you compare this to the sum of the radii. If the distance between the centers is less than the sum of the radii than your circles have to overlap and you have a collision. The closest two circles can get is adjacent when the center to center distance would be the sum of the radii, anything less and they hit.
is there anyway to convert this code into a rectangle to rectangle collision
Rectangles don't allow this easy collision detection system as they can have uniform shapes, which a circle or sphere can't have. For rectangles you are going to have to write a contains method and if one point of a rectangle lies within the area of another one it is colliding with it.
This book (http://books.google.co.uk/books?id=WGpL6Sk9qNAC&source=gbs_book_similarbooks) is collision detections bible btw.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

well I am working with a small piece of code,for some reason it never gets to label1.Text="true" or label1.Text='false",

const int LEFT = 0;
const int TOP = 0;
const int RIGHT = 5;
const int BOTTOM = 3;
int [] rect1 = new int[4];
int [] rect2 = new int[4];
public bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1

< rect2

||
rect1

> rect2

||
rect1[TOP] < rect2[BOTTOM] ||
rect1[BOTTOM] > rect2[TOP])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}

well I have done alot of research on this code but I am still stuck.
It looks like PhysicsEngine in game,about the

collision detection.



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Last month,[/font]


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I had the same problem as you .Now,i still not solve this problem,i think that it surely not easy.[/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

If the thing is ball,you can use my code.[/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I'm not sure this will help you a lot.But ,we are in the same problem ,i'm very happy to share that.[/font]



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

This code use the ActionScript in Adobe Flash.[/font]



There are two balls,one called ball,the other is balll.(not the same).
function onEnterFrame(e:Event)
{
ball.y += ball.vy;
ball.x += ball.vx;
balll.x += balll.vx;
balll.y += balll.vy;
sina = (ball.y - balll.y)/(ball.radius + balll.radius); //their sin corner and cos corner (Rectangle Function)
cosa = (ball.x - balll.x)/(ball.radius + balll.radius);
distance = Math.sqrt((ball.x - balll.x)*(ball.x - balll.x) +(ball.y - balll.y)*(ball.y - balll.y)); //this is the distance between two balls
if (distance <= ball.radius +balll.radius)
{
double v;
v = Math.sqrt(ball.vx * balll.vx + ball.vy * balll.vy);
ball.vx = (v * cosa);
ball.vy = (v * sina);
balll.vx = -(v * cosa);
balll.vy = -(v * sina);
}
}


My problem is that if two balls overlap,the ball will not move , i can't find a good formula to not not let them overlap when "distance == ball.radius+balll.radius",
My idea is
if (distance <= ball.radius +balll.radius)
{
Remove two ball to the position where they crash.//it will be seen that they are not overlap .
.........
}
But i cannot do it!It is still a question.
Sorry,I think i [color=#000000][font=Arial]give an irrelevant answer,and add a new question.But they are looked same.You can refer to it or ignore it.[/font]
What do you mean by

for some reason it never gets to label1.Text="true" or label1.Text='false",
?

How does the code not executed either? Either the conditional will be true and it show be false or it will go to the else and read true. Are you getting any errors or anything? it looks like this shouldn't work.

const int RIGHT = 5;
const int BOTTOM = 3;
int [] rect1 = new int[4];
int [] rect2 = new int[4];
public bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1

< rect2

||



You are saying rect1 and ret2 are arrays of 4 ints. Then you try to get the 5th element of the array when you call rect1

. Thats out of bounds

well it does not get to either true or false it simply prints out "label1"

This topic is closed to new replies.

Advertisement