I have a character moving around the screen and the vars holding its position are x and y and widdth and hieght and I have rectangles it cant move in that its positons is held in a rect how can I keep the character from going into these rectangles
Printable View
I have a character moving around the screen and the vars holding its position are x and y and widdth and hieght and I have rectangles it cant move in that its positons is held in a rect how can I keep the character from going into these rectangles
Are both the player (character) and the obstacles rectangles?
Then do a "look ahead", before you actually do the move, make a temporary move and check if the two rectangles intersect (overlap). If they do, don't perform the move.
Assuming (x, y) is the top left corner of the rectangle, this checks if two rects overlap:
Code:if(((R1.x + R1.width) >= R2.x) && (R1.x <= (R2.x + R2.width)))
{
if(((R1.y + R1.height) >= R2.y) && (R1.y <= (R2.y + R2.height)))
{
return true;
}
}
return false;
Or you could alternatively test using the API fn PtInRect.