4 Replies - 3251 Views - Last Post: 13 October 2011 - 12:25 PM Rate Topic: -----

#1 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

DirectX 9 collision c++

Posted 09 October 2011 - 07:53 PM

Does anyone have any source code that lands one cube on top of the other using DirectX 9 and written in C++:

Pretty much I need to see how collision detection works with DirectX 9. I have a sprite and then I have a platform that I want him to jump up onto. Simple platformer game. If anyone has source code that shows what I'm trying to do that would be great.

This is a game I'm making on my own time. Not homework.

My main guy, "The doctor", is drawn like this:

 CSprite sprite;
    int i = 0;

    sprite.zeroSpriteValues();
    sprite.m_nID                = 555;
    strcpy( sprite.m_chType, "doctor" );
    strcpy( sprite.m_chSpriteTextureName, "doctor.jpg" );
    sprite.m_nWidth             = 50;
    sprite.m_nHeight            = 50;
    sprite.m_fPosition_x        = 0;
    sprite.m_fPosition_y        = 500;
    //sprite.m_bAutoAnimate       = false; // We'll control the animation ourself through incFrame() and decFrame()
    //sprite.m_nFrameRateModifier = -2;
    //sprite.m_nCurrentFrame      = 14;  // Point forward until further notice!
    sprite.m_nFrameWidth        = 50;
    sprite.m_nFrameHeight       = 50;
    //sprite.m_nFramesAcross      = 8;
	// sprite.m_nWidthScaling      = 50; // Shrink the sprite's width
	// sprite.m_nHeightScaling     = 50; // Shrink the sprite's height
    sprite.m_bModifyCollision   = true;
    sprite.m_nCollisionRight    = -5;  // Reduce sprite 's collision area on the right side
    sprite.m_nCollisionLeft     = -15; // Reduce sprite 's collision area on the left side
    sprite.m_nCollisionBottom   = -50; // Reduce sprite 's collision area on the bottom
    sprite.m_nCollisionTop      = -20; // Reduce sprite 's collision area on the top
    sprite.loadAnimationString( 0, "0",
        CSprite::MAINTAIN_LAST_FRAME );

    g_SpriteList.push_back(sprite);



and the platform I want him to land on is drawn like this:

sprite.zeroSpriteValues();
    sprite.m_nID                = 555;
    strcpy( sprite.m_chType, "brickWall" );
    strcpy( sprite.m_chSpriteTextureName, "brick.jpg" );
    sprite.m_nWidth             = 500;
    sprite.m_nHeight            = 50;
    sprite.m_fPosition_x        = 300;
    sprite.m_fPosition_y        = 475;
    sprite.m_nFrameWidth        = 500;
    sprite.m_nFrameHeight       = 50;
	sprite.m_bModifyCollision   = true;
    sprite.m_nCollisionRight    = -5;  // Reduce sprite 's collision area on the right side
    sprite.m_nCollisionLeft     = -15; // Reduce sprite 's collision area on the left side
    sprite.m_nCollisionBottom   = -50; // Reduce sprite 's collision area on the bottom
    sprite.m_nCollisionTop      = -20; // Reduce sprite 's collision area on the top
    sprite.loadAnimationString( 0, "0",
        CSprite::MAINTAIN_LAST_FRAME );

    g_SpriteList.push_back(sprite);



Question: How can I detect collision between the doctor and the wall, so the doctor will stop falling through platform when he jumps up there?

This post has been edited by RUAg4mer: 09 October 2011 - 07:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: DirectX 9 collision c++

#2 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

Re: DirectX 9 collision c++

Posted 10 October 2011 - 09:04 AM

Hello,

Collision in the case of sprites though sounds like a challenge is fairly simple, on the persumption that your sprites have the main X & Y coordinates at the top left of the sprite (quite a common instance) then you just need to compair the X and Y position of the first sprite to the X/Y of the seconds keeping in mind the width and height of the sprite so basic logic to find if there is a collision:

we'll refer to the two sprites as Sp1 and Sp2
if( Sp1.x + Sp1.Width < Sp2.x ) // collision not possible
 return false;

if( Sp1.y + Sp1.Height < Sp2.y ) // collision not possible
return false;

if( Sp1.x > Sp2.x + Sp2.Width ) // collision not possible
return false;

if( Sp1.y > Sp2.y + Sp2.Height ) // collision not possible
return false;

return true; // collision detected




for the responce you just need to track the object back to just before the collision happened, there are a few tutorials on google about this, the best one to look at is the search 'Polygon collision detection'.

Hope that helps you :).

This post has been edited by Kain6622: 10 October 2011 - 09:18 AM

Was This Post Helpful? 2
  • +
  • -

#3 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

Re: DirectX 9 collision c++

Posted 10 October 2011 - 09:33 PM

View PostKain6622, on 10 October 2011 - 09:04 AM, said:

Hello,

Collision in the case of sprites though sounds like a challenge is fairly simple, on the persumption that your sprites have the main X & Y coordinates at the top left of the sprite (quite a common instance) then you just need to compair the X and Y position of the first sprite to the X/Y of the seconds keeping in mind the width and height of the sprite so basic logic to find if there is a collision:

we'll refer to the two sprites as Sp1 and Sp2
if( Sp1.x + Sp1.Width < Sp2.x ) // collision not possible
 return false;

if( Sp1.y + Sp1.Height < Sp2.y ) // collision not possible
return false;

if( Sp1.x > Sp2.x + Sp2.Width ) // collision not possible
return false;

if( Sp1.y > Sp2.y + Sp2.Height ) // collision not possible
return false;

return true; // collision detected




for the responce you just need to track the object back to just before the collision happened, there are a few tutorials on google about this, the best one to look at is the search 'Polygon collision detection'.

Hope that helps you :).



Thanks for the input. Including the width of the jpg's has kept them from overlapping as much. I have collision and response from both the left and the right of my enemy. And I have a background now too. Game is looking good. Onto the next issue.

Whats the best way to simulate some gravity directly onto the main character sprite? So that he falls after I make him jump. Instead he stays in the air because of the BUTTONDOWN technique. As long as I hold the spacebar he stays in the air.
Was This Post Helpful? 0
  • +
  • -

#4 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 18-March 10

Re: DirectX 9 collision c++

Posted 11 October 2011 - 05:18 AM

The best way to simulate gravity for your main character sprite and the collision detection used is to have a boolean flag for your character for it's jump state, but default when the player just on the ground the flag can be set to false, then when the user presses the jump button you set that flag to true until the player hits the ground again, it's easier to explain with a form of pseudo-code:

1: if the player collides with the ground 
22:      set jump flag to false
3: if the user presses the jump key and the jump flag is false
4:      set jump flag to true    
6: if jump flag is false
7:      don't apply gravity
8: if jump flag is true
9:      apply jumping effect 



the basic logic here is that if the player is not jumping (jump flag = false) and the user presses the jump button, the jumping code will be executed and the jump flag is set to true... this prevents the player floating if the button is held down as the jumping code is only executed if and only if the button is down AND the jump flag is false... there is a small problem that does come from this but it is very dependant on how your jumping physics code is coded, what you want to have is apply a force velocity to the player when they jump which gets incremented while jump is set to true but have a constant gravity value which is taken off the original force each frame...so

JumpingForce = 5
constant Gravity = 0.5

when isJumping is true
PlayerSprite.y += JumpingForce
JumpingForce -= Gravity

when isJumping is false
reset the Force back to the original value

the above is only a simple example and you would need to tweak the assigned values till you get the effect you desire. I'm not sure how well I explained that but if you have anything you don't understand I'll be willing to explain further.
Was This Post Helpful? 0
  • +
  • -

#5 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

Re: DirectX 9 collision c++

Posted 13 October 2011 - 12:25 PM

Ughh.. I never thought I'd be into graphics or art but drawing scenes, sprites, and meshes is sooo much easier than setting up the physics.

I went back and set up the velocity, acceleration, and ultimately the moving from left to right properly. So I'm sure I have everything I need to I just don't know how to lay it out.

I added a boolean to my sprite header for 'Jump' and set it to true.
Then back in my winMain I dropped that into the sprite struct.

So now what's the bool Jump() function that returns true look like?

Seems likely that's the only function I'm missing to make this work. Like I said I have the variables in place I just don't know how to set up the function.

Thank you for your help so far and I'm trying my best to utilize your advice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1