public static int BulletCollisionCheck(Templates.Projectiles.Bullet bulletx)
{
bulletpoints[0] = (int)bulletx.Position.X;
bulletpoints[1] = (int)bulletx.Position.Y;
bulletpoints[2] = (int)bulletx.Position.X + bulletx.Image.Width;
bulletpoints[3] = (int)bulletx.Position.Y + bulletx.Image.Height;
foreach (int point in bulletpoints)
{
if (point > (int)Player1.Position.X
&& point < (int)Player1.Position.X + (int)Game1.CharacterWidth
&& point > (int)Player1.Position.Y
&& point < (int)Player1.Position.Y + (int)Game1.CharacterHeight)
{
errorscreen.Show("player hit", false);
break;
}
}
return 0;
}
5 replies to this topic
#1
Posted 05 January 2012 - 11:44 PM
In the game I'm working on I'm trying to detect when a bullet hits the player. To do this I decided to get the position of each of the corners of the bullet image. Then check each of the points to see if they where between each corner of the player image. My player, however, can still walk through bullets. I've probably just been at this too long and will feel stupid for asking, but does anyone see anything wrong with this method?
|
|
|
#2
Posted 05 January 2012 - 11:49 PM
sonar87 said:
My player, however, can still walk through bullets.
A question from me here, Are you using XNA or normal C#?
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
#3
Posted 05 January 2012 - 11:50 PM
The problem is that "point" is actually only half the information of a point. Either X or Y.
You gonna want a class that holds BOTH x and y, and use that object to do the if-test like so:
You gonna want a class that holds BOTH x and y, and use that object to do the if-test like so:
if (point[B][SIZE=4].x[/SIZE][/B] > (int)Player1.Position.X && point.[SIZE=4][B]x[/B][/SIZE] < (int)Player1.Position.X + (int)Game1.CharacterWidth && point.[B][SIZE=4]y[/SIZE][/B] > (int)Player1.Position.Y * && point.[B][SIZE=4]y[/SIZE][/B] < (int)Player1.Position.Y + (int)Game1.CharacterHeight)
#4
Posted 05 January 2012 - 11:54 PM
wim DC said:
The problem is that "point" is actually only half the information of a point. Either X or Y.
You gonna want a class that holds BOTH x and y, and use that object to do the if-test like so:
You gonna want a class that holds BOTH x and y, and use that object to do the if-test like so:
if (point[B][SIZE=4].x[/SIZE][/B] > (int)Player1.Position.X && point.[SIZE=4][B]x[/B][/SIZE] < (int)Player1.Position.X + (int)Game1.CharacterWidth && point.[B][SIZE=4]y[/SIZE][/B] > (int)Player1.Position.Y * && point.[B][SIZE=4]y[/SIZE][/B] < (int)Player1.Position.Y + (int)Game1.CharacterHeight)
His bulletpoints array is holding the X and Y of the bullet, Isn't the foreach loop letting him skip the point.X and point.Y ?
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
#5
Posted 06 January 2012 - 12:01 AM
His foreach is fine, but when the "point" in the foreach has a X-value, then this if should be done:
With the current code it is not possible with a foreach, or else you'll make it ugly(/ier) by doing:
if (point > (int)Player1.Position.X && point < (int)Player1.Position.X + (int)Game1.CharacterWidth)However, if "point" contains a Y-value, it should check:
if( point.y > (int)Player1.Position.Y && point.y < (int)Player1.Position.Y + (int)Game1.CharacterHeight)
With the current code it is not possible with a foreach, or else you'll make it ugly(/ier) by doing:
boolean x = true;
foreach (int point in bulletpoints)
{
if (x && point > (int)Player1.Position.X && point < (int)Player1.Position.X + (int)Game1.CharacterWidth)
{
x = !x;
errorscreen.Show("player hit", false);
break;
}
if (!x && point > (int)Player1.Position.Y && point < (int)Player1.Position.Y + (int)Game1.CharacterHeight
{
x = !x;
errorscreen.Show("player hit", false);
break;
}
}
#6
Posted 06 January 2012 - 12:27 AM
alright guys, I figured it out. Was thinking too much about how to compare the points to bother giving a thought to what a point even is I guess. Thanks.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









