4 Replies - 1377 Views - Last Post: 19 October 2011 - 05:56 AM Rate Topic: -----

#1 RUAg4mer  Icon User is offline

  • New D.I.C Head

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

DirectX Game using C++

Posted 15 October 2011 - 03:16 PM

Well I'm back. My game is coming along quite nicely. User Interface is set up. Sprites are looking good.

However I kind of dove right into making the game. It wasn't really planned out at all, as I just found myself bored and started throwing things together.

That being said I need to backtrack a little.

1. Moving sprites:
Currently I'm moving my character sprite to the right like this: Does this seem right? Or should position play no part in my movement, meaning I move the sprite with just velocity acceleration and time.
g_sprite_i->m_fPosition_x -= g_sprite_i->m_fVelocity_x;



2. Time:
How can I include time as a reference for moving the sprite?
I don't have any code to currently attempt that.

3. Gravity:
I set gravity to push down if the character is less than 0 on the y axis. He always is so gravity is always active. So how can I now make the character jump and fall down due to gravity.

I set up gravity....and it was all wrong. I pretty much faked the hell out of it using a bunch of current positions of the sprite. Like if he was less than zero push down with 8N. But if I make him jump with 8N he stays in the air. Cuz the gravity is equal to his jump. If I set gravity one higher than jump speed he doesn't jump at all. If I set gravity one lower he jumps, and can keep going up if the button is held down.

I have backtracked so far I lost the gravity I had working so I can't drop in how I was doing it. But it wasn't working 100 percent properly so I'll avoid it all together.

What's a good way to set it up.

This is an extremely basic 2D sprite game like mario/megaman where gravity can't just push me to the ground. I need to be able to jump again once I'm on a platform.

Is This A Good Question/Topic? 0
  • +

Replies To: DirectX Game using C++

#2 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: DirectX Game using C++

Posted 15 October 2011 - 08:16 PM

For movement, you can alter the velocity with acceleration before you do the movement? Looks good enough to me.
Timestamps? I haven't used them, probably don't need them unless you it's imperative for some fancy algorithm you're writing, though you should get someone else's opinion on that.

Gravity? Add in gravity to your players velocity every tick/interval. Gravity is a negative acceleration. Have you're player keep a boolean isJumping/isDoubleJumping/etc, so that your player cannot infinite-jump. When player jumps, set isJumping/etc to true. If player lands on a platform, set isJumping/etc to false

//arbitrary numbers

double gravity = 1.5;
//Example of jump code
boolean doJump, isJumping;

if(doJump && !isJumping)
playerYVelocity = -10;

//example of player movement
playerXVelocity += playerXAcceleration - friction;
playerXPosition += playerXVelocity;

playerYVelocity += playerYAcceleration - gravity;
playerYPosition += playerYVelocity;


Was This Post Helpful? 0
  • +
  • -

#3 RUAg4mer  Icon User is offline

  • New D.I.C Head

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

Re: DirectX Game using C++

Posted 16 October 2011 - 06:04 PM

//arbitrary numbers

double gravity = 1.5;
//Example of jump code
boolean doJump, isJumping;

if(doJump && !isJumping)
playerYVelocity = -10;

//example of player movement
playerXVelocity += playerXAcceleration - friction;
playerXPosition += playerXVelocity;

playerYVelocity += playerYAcceleration - gravity;
playerYPosition += playerYVelocity;


[/quote]


Thanks for your reply. I'm making good progress I think. Got the sprite going up and dropping down but he still stays in the air as long as jump button is held.

This is the second time I've heard of the jump flag. I have a way to set up the boolean jump. I already have flags for collide so a flag for isJumping will be easy to start.

But how's the function look? I would put that if the position of the character is less than 500 (500 on the y axis is the ground), but that would result in a jumpy sprite whenever he's on a platform, and not the ground.

So back to the bool isJumping. whats that function look like?

I'm accustomed to checking for pixels below the player in FLASH but not sure how to do that with the way the collision is done. Perhaps I should post what I have for collision and then jumping. IT's below.


COLLISION DETECTION WITH THE GROUND
 int nCount1 = 0;
    int nCount2 = 0;
    int size = g_SpriteList.size();

    for( g_sprite_i = g_SpriteList.begin(); g_sprite_i != g_SpriteList.end(); ++g_sprite_i )
    {
        ++nCount1; // Next sprite to process

        for( g_sprite_j = g_SpriteList.begin(); g_sprite_j != g_SpriteList.end(); ++g_sprite_j )
        {
            // Don't check g_sprite_i against sprites that it's already been checked against
            while( nCount2 != nCount1 )
            {
                ++g_sprite_j; // Don't check sprites that've already been checked.
                ++nCount2;    // Skip ahead...
            }

            // Don't bother checking the last sprite...
            if( nCount2 == size )
                break;

            if( g_sprite_i->m_bCollide == true &&
                g_sprite_j->m_bCollide == true &&
                g_sprite_i->m_bActive  == true &&
                g_sprite_j->m_bActive  == true)
            {
                if( SpriteCollisionTest( *g_sprite_i, *g_sprite_j ) )
                {
					
					if( !lstrcmp(g_sprite_i->m_chType, "doctor") && !lstrcmp(g_sprite_j->m_chType, "ground") )
                    { 
						
						 if (g_sprite_i->m_fPosition_y + g_sprite_i->m_nHeight <= g_sprite_j->m_fPosition_y + g_sprite_j->m_nHeight)
					   {
							g_sprite_i->m_fPosition_y -= 20;

						
					   }
						   if (g_sprite_i->m_fPosition_y  >= g_sprite_j->m_fPosition_y + g_sprite_j->m_nHeight)
					   {
						   g_sprite_i->m_fPosition_y += 20;
					   }
						
					}




TO JUMP
if( KEYDOWN(buffer, DIK_SPACE) )
            {
				  //g_sprite_i->m_fPosition_y += (gravity/NumFrames);
                  g_sprite_i->m_fPosition_y -= g_sprite_i->m_fVelocity_y * 7;
				  
					//g_sprite_i->m_fVelocity_y += JUMP_SPEED;
				
			}



GRAVITY
float gravity = 2.0;

if ( g_sprite_i->m_fPosition_y < 500)
			{
				g_sprite_i->m_fPosition_y += gravity + 2;
			}



The gravity is working, horribly, and I can see why. So I need a better way to set it up. Perhaps that bool isJumping!
Was This Post Helpful? 0
  • +
  • -

#4 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: DirectX Game using C++

Posted 16 October 2011 - 07:13 PM

Well, I would wrap your sprites up into an object that had position/velocity/accel as member variables, and the isJumping boolean flag. Your objects would have an update function which would handle the movement based on velocity. Your Jump function would call a method of your object, say jump() which would set the y velocity to go up, and the isJumping flag to true. Now if you try to call jump() again and isJumping is true, do nothing.

example
class Object
{
  private static double gravity = 2;
  private g_Sprite sprite;
  private double xPosition, yPosition;
  private double xVelocity, yVelocity;
  private boolean isJumping;

  public void update()
  {
    xPosition += xVelocity;
    yPosition += yVelocity + gravity; // Gravity works it's magic
  } 
  public void jump()
  {
    // if already jumping have to wait for object/player to land 
    // and have his isJumping flag reset by colliding with other object/ground
    if(!isJumping) 
    {
      yVelocity = -7;
      isJumping = true;
    }
  }
}
//Collision detection
if(Object Player collides with ground)
{ 
  player.isJumping = false;
}


Was This Post Helpful? 0
  • +
  • -

#5 newn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 115
  • Joined: 22-April 09

Re: DirectX Game using C++

Posted 19 October 2011 - 05:56 AM

You might consider looking at Box2D, they have pretty cool stuff for physics. Although I'm programming physics myself for my game too... That's basically logical understanding of physics (or actual knowledge of physics + less logical understanding of physics), and I'm going for the former myself. A bit hard, but fun and pays off well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1