int Collision(CHARACTER* object1, CHARACTER* object2) { D3DXVECTOR3 pos1 = object1->GetPosition(); D3DXVECTOR3 pos2 = object2->GetPosition();
RECT a, b, *des; a.left = pos1.x; a.right = pos1.x + object1->width; a.top = pos1.y; a.bottom = pos1.y + object1->height;
b.left = pos2.x; b.right = pos2.x + object2->height; b.top = pos2.; b.bottom = y2 + object2->height;
return IntersectRect(des, a, b); }
Hi there. Please use code tags from now on, it's much prettier, and makes your code a lot easier to read.
Why not try out radial collision detection? Should look something like this in your case:
#include <math.h>
int Collision(CHARACTER* object1, CHARACTER* object2)
{
D3DXVECTOR3 pos1 = object1->GetPosition();
D3DXVECTOR3 pos2 = object2->GetPosition();
int xDistance = pos1.x - pos2.x; // Get relative distances
int yDistance = pos1.y - pos2.y; // between the objects
// Assuming your objects are pretty square shaped
int combinedRadius = object1->width + object2->width;
xDistance *= xDistance; // This should give you xDistance^2
yDistance *= yDistance; // and yDistance^2
// And finally, some pythagoras
return (sqrt(xDistance + yDistance) <= combinedRadius);
}
I just came up with this solution, and while writing this code down, I realized this is probably going to be a lot slower than your original example. It has some pros and cons though. Collision detection between an alligator and a flag pole wouldn't generate very good results with this algorithm. Though, collision detection between a small shuttle craft and a huge planet would benefit from this algorithm since you will actually test against the planets circle, and not the square around it, which make huge differences on large objects!
This might also help you with a new way of attacking your problem, so all might not be lost :)
Good luck on your collisions!
Emil Olofsson