Help with simple collision

Started by
3 comments, last by maximo1491 13 years, 5 months ago
Basically im trying to do a rectangle collision so if rectangle intersects with blah blah then do this, but im trying to do this between two separate classes so when I run the code it is saying that it does not exist in current context because im guessing that the rectangle im trying to intersect it with is in the other class therefore not knowing where it is, is there a way I can get it to detect the rectangle in the other class?
Advertisement
It is better to have separated procedures to test, like:

bool CollideRectRect(CRectangle, CRectangle);
bool CollideRectTri(CRectangle, CTriangle);
bool CollideRectMesh(CRectangle, CMesh);
bool CollideTriRect(CTriangle, CRectangle);
etc.

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

Ok sorry im new to c# only started it about a month ago, what does that do?
I believe Vilem was giving you examples on how to structure your program

Quote: is there a way I can get it to detect the rectangle in the other class?


You might as well pass them as Parameters like Vilem suggested? Could you post a stripped down version of your code with all the essentials?
Well this is the bit im having the trouble with. Basically when I run it, it is saying the other rectangle in the rectangle does not exist in current context. I've tried putting them both in the same class but the way the rest of my code is laid out I need them as two separate classes. Was just wondering if there is a way I can do this or do I have to restructure the rest of my code?

public class Warrior
{

public void Update(GameTime gameTime)
{

Rectangle warriorRectangle = new Rectangle((int)warriorPosition.X, (int)warriorPosition.Y, warriorTexture.Width, warriorTexture.Height);

warriorHit = false;

if (warriorRectangle.Intersects(enemyWarriorRectangle))
{
warriorHit = true;
}

if (warriorHit)
{
warriorPosition = new Vector2 (0,0);
}
}
}


public class enemyWarrior
{

public void Update(GameTime gameTime)
{

Rectangle enemyWarriorRectangle = new Rectangle((int)enemyWarriorPosition.X, (int)enemyWarriorPosition.Y, enemyWarriorTexture.Width, enemyWarriorTexture.Height);

enemyWarriorHit = false;

if (enemyWarriorRectangle.Intersects(warriorRectangle))
{
enemyWarriorHit = true;
}

if (enemyWarriorHit)
{
enemyWarriorPosition = new Vector2(0, 200);
}
}
}

[Edited by - maximo1491 on November 30, 2010 5:11:25 AM]

This topic is closed to new replies.

Advertisement