IntersectRect() not working as expected?

This is a discussion on IntersectRect() not working as expected? within the Windows Programming forums, part of the Platform Specific Boards category; I think this is more of a Win32 related question than a game question. This is a simple solution for ...

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    IntersectRect() not working as expected?

    I think this is more of a Win32 related question than a game question. This is a simple solution for now in my game loop. It checks for collision. Here, I have two objects on the screen (PC and NPC). If they collide from (x1,y1) up to (x2,y2), a message should appear in the console saying that the collection was found. The member variables (x1,y1,x2,y2) are floats and they are casted to LONG since that is what RECT expects. It's not the fastest solution, but I just want to see that it works. Apparently, nothing is being outputed to the console. They are first initialized with a 64x64 tile, and the PC and NPC is first started off with coordinates -32, 32, 32, -32 (x1,y1,x2,y2). The grid I am using is a cartesian grid with origin in the middle of the screen (0,0). So, these tiles are 32 all around the origin. So if they start off with the same coordinates, how come there isn't any collision message?

    Code:
    void GetCollision(void)
    {
    	RECT rPC;
    	rPC.left =		(LONG) PC.x1;
    	rPC.top =		(LONG) PC.y1;
    	rPC.right =	(LONG) PC.x2;
    	rPC.bottom =	(LONG) PC.y2;
    
    	RECT rNPC;
    	rNPC.left =	(LONG) NPC.x1;
    	rNPC.top =	(LONG) NPC.y1;
    	rNPC.right =	(LONG) NPC.x2;
    	rNPC.bottom =	(LONG) NPC.y2; 
    
    	RECT rect;
    
    	if (IntersectRect(&rect, &rPC, &rNPC))
    		std::cout << "Got it." << std::endl;
    }
    Last edited by dxfoo; 09-05-2006 at 05:43 PM.

  2. #2
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I got it working, however I don't understand why it's working? I replaced the bottom-right coordinates with the size of the sprite added onto its x1,y1 coordinates. It's working as expected now.

    Code:
    void GetCollision(void)
    {
    	RECT rPC;
    	rPC.left =		(LONG) PC.x1;
    	rPC.top =		(LONG) PC.y1;
    	rPC.right =		(LONG) PC.x1+64;//PC.x2;
    	rPC.bottom =	(LONG) PC.y1+64;//PC.y2;
    
    	RECT rNPC;
    	rNPC.left =		(LONG) NPC.x1;
    	rNPC.top =		(LONG) NPC.y1;
    	rNPC.right =	(LONG) NPC.x1+64;//NPC.x2;
    	rNPC.bottom =	(LONG) NPC.y1+64;//NPC.y2; 
    
    	RECT rect; 
    
    	if (IntersectRect(&rect, &rPC, &rNPC)) 
    		std::cout << "Got it." << std::endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Porting a shared library from Linux to Windows
    By DARKGuy in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2007, 02:45 PM
  2. Simple vector class problem, please help
    By fidodidohk in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2007, 10:13 AM
  3. Vector not working!!
    By aaroroge in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2005, 06:17 PM
  4. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21