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
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
In a perfect world every dog would have a home and every home would have a dog.
Visit My Web Site, Canine Programming
I use Win32 API
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;
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Or you could alternatively test using the API fn PtInRect.
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.