Thread: checking if a rect is another

  1. #1
    Registered User canine's Avatar
    Join Date
    Sep 2001

    checking if a rect is another

    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

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    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:
    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;

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Join Date
    Jan 2002
    Or you could alternatively test using the API fn PtInRect.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Probably simple problem w/ RECT members
    By fusikon in forum Windows Programming
    Replies: 2
    Last Post: 01-17-2003, 02:01 PM