Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++ Help] Jumping.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Game Development
View previous topic :: View next topic  
Author Message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Wed Sep 16, 2009 8:39 am    Post subject: [C++ Help] Jumping. Reply with quote

Heya.

I'm currently trying to code a game together for Linux, and so far it's going well.
I'm currently on the state of coding the jumping part. (Yes, it's a platform game)

So my problem is, whenever I jump (SDLK_SPACE) it just flys to the top of the screen.
Anybody know what to do?

jumpSpeed is 20 when initialized.
Code:
void Player::handle_input()
{
   //If key was pressed
   if (event.type == SDL_KEYDOWN)
   {
      //adjust velocity
      switch (event.key.keysym.sym)
      {
         //case SDLK_UP: yVel -= 1; break; //PLR_HEIGHT / 2; break; //removed all this so it wouldn't be walking up and down as well.
         //case SDLK_DOWN: yVel += 1; break; //PLR_HEIGHT / 2; break;
         case SDLK_LEFT: xVel -= 1; break; //PLR_WIDTH / 2; break;
         case SDLK_RIGHT: xVel += 1; break;//PLR_WIDTH / 2; break;
         case SDLK_SPACE: jump(); break;
      }
   }
   //If key released
   else if (event.type == SDL_KEYUP)
   {
      //adjust velocity
      switch (event.key.keysym.sym)
      {
         //case SDLK_UP: yVel += 1; break;//PLR_HEIGHT / 2; break;
         //case SDLK_DOWN: yVel -= 1; break;//PLR_HEIGHT / 2; break;
         case SDLK_LEFT: xVel += 1; break;//PLR_WIDTH / 2; break;
         case SDLK_RIGHT: xVel -= 1; break;//PLR_WIDTH / 2; break;
      }
   }
}

void Player::move()
{
   //move left or right
   x += xVel;

   //If dot is too far to the left or right
   if ( (x < 0) || (x + PLR_WIDTH > SCREEN_WIDTH) )
   {
      //move back
      x -= xVel;
   }

   //Move up or down
   y += yVel;

   //if player is too far up or down
   if ( (y < 0) || (y + PLR_HEIGHT > SCREEN_HEIGHT) )
   {
      //move back
      y -= yVel;
   }
}

void Player::jump()
{
   int origY = y;
   yVel -= jumpSpeed;
   if (y <= origY-100)
   {
      yVel -= -30;
   }
   if (y >= origY)
   {
      yVel -= 0;
   }
}


All credits to Lazy Foo's Productions however, as I've learned a lot there.
Back to top
View user's profile Send private message
Cryoma
Member of the Year
Reputation: 198

Joined: 14 Jan 2009
Posts: 1819

PostPosted: Wed Sep 16, 2009 10:16 am    Post subject: Reply with quote

Long time no see, Moller.

Alright, I'm no pro at C++, but have you tried changing 100 to something lower?
It seems to me like the character will fly upwards at increments of 100 pixels per frame.
This means when you press spacebar, (and assuming your fps is about 30) the character will bolt upwards 3000 pixels in like, a second.
You also have it set to kill the jump velocity when Player reaches 100 pixels higher than he originally was.

I think.

Like I said, I'm no pro at C++.

Edit: You said jumpspeed is 20, my bad.
Back to top
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Wed Sep 16, 2009 10:24 am    Post subject: Reply with quote

Actually, the jump one should look like this
Code:
void Player::jump()
{
   int origY = y;
   yVel -= jumpSpeed;
   if (y <= origY-100) //origY is 420, as it's the Y amount where the platform is
   {
      jumpSpeed = -30;
   }
   if (y >= origY)
   {
      jumpSpeed = 0;
   }
}


The other one was just when I was testing it out.

Cryoma
I tried messing with it, unfortunately no luck. But thank you for responding anyway.
But you're right that I've set it to kill the jump part when it reaches 100 pixels above the player. I think the problem could be that yVel keeps being an amount higher than 0, and therefore keeps dragging the player up.

Take note, that the x and y isn't like in normal x y.
(0,0) is the upper left corner, and (800,800) (if the screen were that big) would be bottom right corner.
Back to top
View user's profile Send private message
Cryoma
Member of the Year
Reputation: 198

Joined: 14 Jan 2009
Posts: 1819

PostPosted: Wed Sep 16, 2009 10:46 am    Post subject: Reply with quote

Møller wrote:
Actually, the jump one should look like this
Code:
void Player::jump()
{
   int origY = y;
   yVel -= jumpSpeed;
   if (y <= origY-100) //origY is 420, as it's the Y amount where the platform is
   {
      jumpSpeed = -30;
   }
   if (y >= origY)
   {
      jumpSpeed = 0;
   }
}


The other one was just when I was testing it out.


That makes a lot more sense now.

Code:
   yVel -= jumpSpeed;
Doesn't smell right to me.
Could you use yVel = -jumpSpeed; instead?

Quote:
Take note, that the x and y isn't like in normal x y.
(0,0) is the upper left corner, and (800,800) (if the screen were that big) would be bottom right corner.
Yeah, I'm pretty sure all coding languages work that way.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Sep 16, 2009 10:53 am    Post subject: Reply with quote

Every time the function is called (I assume each logic tick), you are setting 'origY' to 'y'. This means the first condition will always be true, and therefore you will continue to fly up according to your co-ordinate system...

... which you really should be abstracting with the use of transformation matrices and a custom camera class.

Personally, I'd implement a 'gravity' function that gets called on all entities each tick, that tries to move them closer to the bottom of the screen with a constant acceleration. That way, when you jump, all you have to do is call a function that sets the players 'y' velocity up, and the engine takes care of the rest.
Back to top
View user's profile Send private message
Cryoma
Member of the Year
Reputation: 198

Joined: 14 Jan 2009
Posts: 1819

PostPosted: Wed Sep 16, 2009 11:13 am    Post subject: Reply with quote

Møller wrote:
Actually, the jump one should look like this
Code:
void Player::jump()
{
   int origY = y;
   yVel -= jumpSpeed;
   if (y <= origY-100) //origY is 420, as it's the Y amount where the platform is
   {
      jumpSpeed = -30;
   }
   if (y >= origY)
   {
      jumpSpeed = 0;
   }
}


The other one was just when I was testing it out.


Gah why didn't I see this, you're not checking variables right.
Code:
if (y <== origY-100) //origY is 420, as it's the Y amount where the platform is
   {
      jumpSpeed = -30;
   }
   if (y >== origY)
   {
      jumpSpeed = 0;
   }


A single = sets the variable, double =='s compares the variable.
Back to top
View user's profile Send private message
gogodr
I post too much
Reputation: 125

Joined: 19 Dec 2006
Posts: 2041

PostPosted: Wed Sep 16, 2009 11:23 am    Post subject: Reply with quote

int origY = y;
yVel -= jumpSpeed;
if (y <= origY-100) //origY is 420, as it's the Y amount where the platform is
{
jumpSpeed = -30;
}
if (y >= origY)
{
jumpSpeed = 0;

-------------

as Athaem says I would use a gravity system too and its not really hard to do.


Code:
void Player::jump()
{

int Vel = 100;
float Ac = 0;
int origY = y

do{
float fpsCalc=(float)frames*calcVal;

y = y + Vel + Ac;
Ac = Ac - 10/fpsCalc; // using Frames per seconds because it will load on every frame and is an estimate to make the acceleration influence on the Velocity of -10m/s^2 so instead of seconds I'm using frames
}
while(y => origY);


}





----------------------

and to this I would also add a collision condition also it stops falling if it collides with something in Y


Last edited by gogodr on Wed Sep 16, 2009 11:27 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Robotex
Master Cheater
Reputation: 0

Joined: 05 Sep 2006
Posts: 378
Location: The pizza country!

PostPosted: Wed Sep 16, 2009 11:25 am    Post subject: Reply with quote

Cryoma wrote:

A single = sets the variable, double =='s compares the variable.

Actually >= and <= are right for comparing in C++

_________________

ASM/C++ Coder
Project Speranza lead developer
Back to top
View user's profile Send private message
Cryoma
Member of the Year
Reputation: 198

Joined: 14 Jan 2009
Posts: 1819

PostPosted: Wed Sep 16, 2009 11:29 am    Post subject: Reply with quote

Robotex wrote:
Cryoma wrote:

A single = sets the variable, double =='s compares the variable.

Actually >= and <= are right for comparing in C++

Oh.
Shut up, Cryoma.
Back to top
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Wed Sep 16, 2009 2:13 pm    Post subject: Reply with quote

gogodr wrote:
int origY = y;
yVel -= jumpSpeed;
if (y <= origY-100) //origY is 420, as it's the Y amount where the platform is
{
jumpSpeed = -30;
}
if (y >= origY)
{
jumpSpeed = 0;

-------------

as Athaem says I would use a gravity system too and its not really hard to do.


Code:
void Player::jump()
{

int Vel = 100;
float Ac = 0;
int origY = y

do{
float fpsCalc=(float)frames*calcVal;

y = y + Vel + Ac;
Ac = Ac - 10/fpsCalc; // using Frames per seconds because it will load on every frame and is an estimate to make the acceleration influence on the Velocity of -10m/s^2 so instead of seconds I'm using frames
}
while(y => origY);


}





----------------------

and to this I would also add a collision condition also it stops falling if it collides with something in Y


Mind explaining where (and how) to define?:
Code:
(float)frames*calcVal;


Athaem wrote:
Every time the function is called (I assume each logic tick), you are setting 'origY' to 'y'. This means the first condition will always be true, and therefore you will continue to fly up according to your co-ordinate system...

... which you really should be abstracting with the use of transformation matrices and a custom camera class.

Personally, I'd implement a 'gravity' function that gets called on all entities each tick, that tries to move them closer to the bottom of the screen with a constant acceleration. That way, when you jump, all you have to do is call a function that sets the players 'y' velocity up, and the engine takes care of the rest.

Even though I defined origY to 420, it didn't work.
I'll try to look at the gravity system.

If you want to see the full source code, you can see it here: PASTEBIN
Back to top
View user's profile Send private message
gogodr
I post too much
Reputation: 125

Joined: 19 Dec 2006
Posts: 2041

PostPosted: Wed Sep 16, 2009 3:40 pm    Post subject: Reply with quote

use this to get the fps

http://www.cprogramming.com/snippets/show.php?tip=5&count=30&page=0

well its just a procedure to have time modifying your acceleration. if you find another thing easier to replace the time than frames you should use that one.
Back to top
View user's profile Send private message MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Sep 16, 2009 5:45 pm    Post subject: Reply with quote

The problem is is that you are trying to do too many things in a single class; you are building an unmaintainable monolith. You need to redesign your system so that you have a common engine base that interacts with a graphics engine and a physics engine. That way you can build up your game based on the common engine base (so your game doesn't directly touch the graphics processing and physics), so from the game programmer point of view you don't care how the other systems work, as far as you're concerned it "just happens".

Your current code looks like you are just programming in "C with classes", when C++ is a lot more than that. If you are having a hard time separating the theory from the language, you could instead do this in C# XNA where a lot of the little things are already done for you.
Back to top
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Wed Sep 16, 2009 11:42 pm    Post subject: Reply with quote

Athaem wrote:
The problem is is that you are trying to do too many things in a single class; you are building an unmaintainable monolith. You need to redesign your system so that you have a common engine base that interacts with a graphics engine and a physics engine. That way you can build up your game based on the common engine base (so your game doesn't directly touch the graphics processing and physics), so from the game programmer point of view you don't care how the other systems work, as far as you're concerned it "just happens".

Your current code looks like you are just programming in "C with classes", when C++ is a lot more than that. If you are having a hard time separating the theory from the language, you could instead do this in C# XNA where a lot of the little things are already done for you.

I must admit I'm still new to C++, but are you recommending that I create more source codes (for example a seperate .cpp file to graphics engine)? And for an example, split up the Player::handle_input?
I will look more into this when I get home from school.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Sep 17, 2009 12:32 am    Post subject: Reply with quote

gogodr wrote:
use this to get the fps

http://www.cprogramming.com/snippets/show.php?tip=5&count=30&page=0

well its just a procedure to have time modifying your acceleration. if you find another thing easier to replace the time than frames you should use that one.


ughgh, GetTickCount() is bad practice as it is not accurate enough, you can run into stupid problems.

this is how you should be handling your timestep.

http://gafferongames.com/game-physics/fix-your-timestep/
Back to top
View user's profile Send private message
gogodr
I post too much
Reputation: 125

Joined: 19 Dec 2006
Posts: 2041

PostPosted: Thu Sep 17, 2009 1:36 am    Post subject: Reply with quote

slovach wrote:
gogodr wrote:
use this to get the fps

http://www.cprogramming.com/snippets/show.php?tip=5&count=30&page=0

well its just a procedure to have time modifying your acceleration. if you find another thing easier to replace the time than frames you should use that one.


ughgh, GetTickCount() is bad practice as it is not accurate enough, you can run into stupid problems.

this is how you should be handling your timestep.

http://gafferongames.com/game-physics/fix-your-timestep/


hmm, thank you very much n.n I'll consider this in my future projects.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Game Development All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites