Jump to content
  • Advertisement
Sign in to follow this  
maximo1491

Help with simple collision

This topic is 2785 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!