Sign in to follow this  

direct x and collsion detection

This topic is 3295 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

well I have some code for collision detection, but I need to make a bounding box and am unsure of how to start, here is some code if((tank2.x==tank.x)&&(tank2.y==tank.y)) { { collision.x=tank.x; collision.y=tank.y; //create vector to update sprite position D3DXVECTOR3 positions3((float)collision.x, (float)collision.y,0.0); //draw the sprite sprite_handler->Draw( collision_image[0], NULL, NULL, &positions3, D3DCOLOR_XRGB(255,255,255)); }

Share this post


Link to post
Share on other sites
here is some bounding box code, is there any mistakes in this code, or any addtional information do I need to include


struct
{
int left;
int top;
int right;
int bottom;
} RECTANGLE;

bool IntersectRect(const RECTANGLE *r1, const RECTANGLE *r2)
{
return (r2->left > r1->right||r2->right>left||r2->top > r1->bottom||r2->bottom>top);

}

Share this post


Link to post
Share on other sites
You want to add a ! outside the parenthesis. And your struct is declared wrong, it should be.
struct Rectangle
{
int left;
int top;
int right;
int bottom;
};


Now for the smaller things that the compiler won't care about. I changed RECTANGLE from being all capitols, because we save that for constants. Also are you sure you want to use ints and not floats (I don't know your game)?

This doesn't matter too much, but most of the time you see people pass to constant reference instead of constant pointer. References get implemented as pointers by the compiler so it's mostly the same, but you get to use the dot operator with references which is nicer. And you also don't have use (*) to get at things which is nice.

edit: ack screwed up one thing

[Edited by - Treb on December 4, 2008 11:09:46 PM]

Share this post


Link to post
Share on other sites
struct RECTANGLE
{
int left;
int top;
int right;
int bottom;
};

bool IntersectRect(const RECTANGLE * r1, const RECTANGLE * r2)
{
return ! ( r2->left > r1->right
|| r2->right < r1->left
|| r2->top > r1->bottom
|| r2->bottom < r1->top
);
}

well this code compiles but I am unsure of how to implement it.

Share this post


Link to post
Share on other sites
So now inside your tank you are going to want to have a Rectangle. This should be the bounding box. So top would be the uppermost value at which the tank will collide, left is the leftmost and so on.

Then you would test like this
if(IntersectRect( tank.rect, tank2.rect ) ){//must have rect member variable

Share this post


Link to post
Share on other sites

This topic is 3295 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this