collision code

Started by
15 comments, last by WillTice 12 years, 1 month ago

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;
}
}




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

, 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.
Advertisement

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
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?
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
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;
}
}
I have put this code into a console applicaiton but it still does not work, no output to the screen at all
Show us the entire code, Where you call the function too.

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.

This topic is closed to new replies.

Advertisement