Thread: Collision Detection with DirectX9

  1. #1
    Registered User
    Join Date
    Mar 2011

    Collision Detection with DirectX9

    Hi all,

    I am writing a simple little shooter game (similar to asteroids). I am currently working on the upgrades for my space ship and one of these is a laser. I ave successfully implemented a laser which works nicely. However, the collision detection has not been working.

    Below is the code:

    Initialized as

    GameObject *laser;
    InitD3DX::d3dspt->Draw(laser->sprite, NULL, laser->centre, laser->position, D3DCOLOR_ARGB(255, 255,255,255));
    	if (myXBox360Controller->IsButtonPressed(XINPUT_GAMEPAD_X)&& saucer->alive)
    	this->laser->position->x = saucer->position->x + saucer->spriteWidth - 8;
    	this->laser->position->y = saucer->position->y + saucer->spriteHeight/2;
    		this->laser->position->x = 2000; 
    		this->laser->position->y = 2000;
    			for (std::vector<GameObject*>::iterator it = myEnemys.begin(); it!=myEnemys.end(); it++)
    			object1 = *it;
    			// If enemy is hit register hit and play sound.
    			if (this->laser->CollisionDetection(this->laser,object1) && this->laser->alive && object1->alive)
    				object1->alive = false;
    				object1->count = 0;
    The laser displays but the the enemy ships do not get destroyed when the laser touches them. I get no error messages and am sure I am close but it just doesn't want to work! object1 should refer to the enemy ships as this code is the collision detection for the torpedoes hitting the enemy ships.

    Any ideas? Thanks


  2. #2
    Registered User
    Join Date
    Aug 2010
    How on earth can we know what is wrong with collision detection, if you didn't provide ANY code related to detecting collisions?!

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    DirectX specifically does not handle collision detection. The D3DX library does offer some support from D3DXBoxBoundProbe() and other more exact and thus more expensive mesh/tri collision detection functions.

    I suggest looking into open source libraries like Bullet for more accurate and more thorough collision detection and response.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Sorry, there is some code related to collision detection. As this program is being modified, and the collision detection function was already there, I largely ignored it as everything else seemed to work okay.

    Below is the code for the collision detection

    bool GameObject::CollisionDetection(const GameObject *gameObject1, const GameObject *gameObject2) const
    	bool collision = false;
    	RECT rect1;
    	RECT rect2;
    	// Create two bounding rectangles.
    	SetRect(&rect1, (int)gameObject1->position->x, (int)gameObject1->position->y, gameObject1->spriteWidth, gameObject1->spriteHeight);
    	SetRect(&rect2, (int)gameObject2->position->x, (int)gameObject2->position->y, gameObject2->spriteWidth, gameObject2->spriteHeight);
    	// Are the sprites x co-ordinates within range of one another.
    	if ((rect1.left >= rect2.left && rect1.left <= rect2.left+gameObject2->spriteWidth) ||
    		(rect1.left+gameObject1->spriteWidth >= rect2.left && rect1.left+gameObject1->spriteWidth <= rect2.left+gameObject2->spriteWidth))
    		// Now check y co-ordinate values.
    		if (( >= && <=>spriteHeight) ||
    			(>spriteHeight >= &&>spriteHeight <=>spriteHeight))
    				return true;
    	return collision;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 03:25 PM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  3. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM