• Create Account

## collision code

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

### #1phil67rpg  Members

213
Like
0Likes
Like

Posted 23 March 2012 - 04:02 PM

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.
[/size][/font][/size][/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);

return true; //collision
return false;    //no collision
}


### #2bd36  Members

118
Like
0Likes
Like

Posted 23 March 2012 - 05:25 PM

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.

### #3phil67rpg  Members

213
Like
0Likes
Like

Posted 23 March 2012 - 05:29 PM

is there anyway to convert this code into a rectangle to rectangle collision

### #4NightCreature83  Members

4779
Like
0Likes
Like

Posted 23 March 2012 - 05:56 PM

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

### #5phil67rpg  Members

213
Like
0Likes
Like

Posted 24 March 2012 - 02:34 PM

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[RIGHT] < rect2[LEFT] ||
rect1[LEFT] > rect2[RIGHT] ||
rect1[TOP] < rect2[BOTTOM] ||
rect1[BOTTOM] > rect2[TOP])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}


### #6phil67rpg  Members

213
Like
0Likes
Like

Posted 24 March 2012 - 08:34 PM

well I have done alot of research on this code but I am still stuck.

### #7MotorBully  Members

144
Like
0Likes
Like

Posted 24 March 2012 - 09:47 PM

It looks like PhysicsEngine in game,about the

collision detection.

Last month,

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

If the thing is ball,you can use my code.

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

This code use the ActionScript in Adobe Flash.

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)
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
{
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
{
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.

### #8MotorBully  Members

144
Like
0Likes
Like

Posted 24 March 2012 - 09:54 PM

Sorry,I think i give an irrelevant answer,and add a new question.But they are looked same.You can refer to it or ignore it.

### #9bd36  Members

118
Like
0Likes
Like

Posted 24 March 2012 - 10:22 PM

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[RIGHT] < rect2[LEFT] ||


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[RIGHT]. Thats out of bounds

### #10phil67rpg  Members

213
Like
0Likes
Like

Posted 24 March 2012 - 10:37 PM

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

### #11slicer4ever  GDNet+

6353
Like
0Likes
Like

Posted 25 March 2012 - 01:13 AM

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[RIGHT] < rect2[LEFT] ||
rect1[LEFT] > rect2[RIGHT] ||
rect1[TOP] < rect2[BOTTOM] ||
rect1[BOTTOM] > rect2[TOP])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}


perhaps it's because your declaration is incorrect?
const int LEFT = 0;
const int TOP = 0;
const int RIGHT = 5;
const int BOTTOM = 3;


to which means when you access rect1[RIGHT], you should be either crashing your program, or getting very bad results, accessing left/top will return the same value at the same location.
i assume you want something like this:
const int LEFT = 0;
const int TOP = 1;
const int RIGHT = 2;
const int BOTTOM = 3;


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

### #12Trienco  Members

2555
Like
0Likes
Like

Posted 25 March 2012 - 01:23 AM

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.

Which would be wrong, because there are plenty of ways for rectangles to intersect without this being true (unless you have known restraints like all rectangles being the same size).

The existing code should be fine by simply testing for a separating axis (for axis oriented rectangles).

My cynical answer to none of the two code blocks being executed would probably be something like "it kind of helps to actually call the function". But apart from that, the code doesn't even make any sense and suggests a strong lack of understanding of basic language features like arrays (why else would the index for top and left be the same and why would the indices be that random in general?)

Which in turn hints at a common case of "I want to make a game but can't waste any time on actually learning how to program in my language of choice". I predict about as much success as an attempt to write a Spanish novel without speaking Spanish and trying to copy/paste from a dictionary.
f@dzhttp://festini.device-zero.de

### #13Postie  Members

1459
Like
0Likes
Like

Posted 25 March 2012 - 04:42 AM

Since you haven't mentioned anything about the array out of bounds error, my money is on the "cynical answer", where the function isn't being called.

Unless... the whole thing is in a try block and the array out of bounds was being handled without any feedback. That would cause collideRectRect() to bail and never set the value of label1.Text.

As a side note, since you're returning true or false from the function anyway why not grab the return value and write that into label1.Text, instead of setting it within the function call?
Currently working on an open world survival RPG - For info check out my Development blog:

### #14phil67rpg  Members

213
Like
0Likes
Like

Posted 25 March 2012 - 03:20 PM

well I reworked my code.I have also done some reserch on arrays and functions.
const int LEFT = 0;
const int TOP = 1;
const int RIGHT = 2;
const int BOTTOM = 3;
int [] rect1 = new int[4] {0,4,1,0};
int [] rect2 = new int[4] {3,3,3,3};
bool collideRectRect(int[] rect1, int[] rect2)
{
if (rect1[2] < rect2[0] ||
rect1[0] > rect2[2] ||
rect1[1] < rect2[3] ||
rect1[3] > rect2[1])
{
label1.Text = "false";
return false;
}
else
{
label1.Text = "true";
return true;
}
}


### #15phil67rpg  Members

213
Like
0Likes
Like

Posted 25 March 2012 - 10:03 PM

I have put this code into a console applicaiton but it still does not work, no output to the screen at all

### #16JustinDaniel  Members

137
Like
0Likes
Like

Posted 25 March 2012 - 10:42 PM

Show us the entire code, Where you call the function too.

### #17willpowered  Members

534
Like
0Likes
Like

Posted 25 March 2012 - 11:24 PM

I have put this code into a console applicaiton but it still does not work, no output to the screen at all

Probably because you're not calling the function anywhere. If this is your whole program, you're going to need a main function (http://en.wikipedia.org/wiki/Main_function) and you'll need to call your collision function from it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.