View previous topic :: View next topic |
Author |
Message |
NothingToShow Grandmaster Cheater Supreme Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Sep 16, 2009 8:39 am Post subject: [C++ Help] Jumping. |
|
|
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 |
|
|
Cryoma Member of the Year Reputation: 198
Joined: 14 Jan 2009 Posts: 1819
|
Posted: Wed Sep 16, 2009 10:16 am Post subject: |
|
|
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 |
|
|
NothingToShow Grandmaster Cheater Supreme Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Sep 16, 2009 10:24 am Post subject: |
|
|
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 |
|
|
Cryoma Member of the Year Reputation: 198
Joined: 14 Jan 2009 Posts: 1819
|
Posted: Wed Sep 16, 2009 10:46 am Post subject: |
|
|
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.
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 |
|
|
Flyte Peanuts!!!! Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Wed Sep 16, 2009 10:53 am Post subject: |
|
|
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 |
|
|
Cryoma Member of the Year Reputation: 198
Joined: 14 Jan 2009 Posts: 1819
|
Posted: Wed Sep 16, 2009 11:13 am Post subject: |
|
|
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 |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Sep 16, 2009 11:23 am Post subject: |
|
|
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 |
|
|
Robotex Master Cheater Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Wed Sep 16, 2009 11:25 am Post subject: |
|
|
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 |
|
|
Cryoma Member of the Year Reputation: 198
Joined: 14 Jan 2009 Posts: 1819
|
Posted: Wed Sep 16, 2009 11:29 am Post subject: |
|
|
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 |
|
|
NothingToShow Grandmaster Cheater Supreme Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Sep 16, 2009 2:13 pm Post subject: |
|
|
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 |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
|
Back to top |
|
|
Flyte Peanuts!!!! Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Wed Sep 16, 2009 5:45 pm Post subject: |
|
|
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 |
|
|
NothingToShow Grandmaster Cheater Supreme Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Sep 16, 2009 11:42 pm Post subject: |
|
|
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 |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
Back to top |
|
|
gogodr I post too much Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Sep 17, 2009 1:36 am Post subject: |
|
|
hmm, thank you very much n.n I'll consider this in my future projects.
|
|
Back to top |
|
|
|