Sign in to follow this  
Esper S.

Jumping - What did I do wrong?

Recommended Posts

Esper S.    124

I know it would be easy to use the windows timer, implement gravity and acceleration, and etc. but just as a test I tried making a primitive "jumping function". On run, the computer freezes; my guess is that I'm doing a useless loop of some sort. Here is the code; can anyone help figure out the problem?

void Character::Jump(bool x){
 bool inAir;
 bool rising;
 bool falling;

 if(m_pos.y != 0)inAir = true;
 if(x)inAir = true;
 if(inAir){
  if(m_pos.y == 10 && !falling){
   rising = false;
   falling = true;
  }
  if(rising)m_pos.y += 0.5;
  if(falling)m_pos.y -= 0.5;
  if(m_pos.y == 0){
   inAir = false;
  }
 }
}

I've tried many ways, and this is my latest... I can't see why it shouldn't work unsure.png

Share this post


Link to post
Share on other sites
SillyCow    1461

1. This code can't loop by itself. 

2. If your computer freezes, it's more likely that you have corrupted your memory than an endless loop. (Unless you mean that your game freezes).

 

Since there is not enough code to analyze this, my suggestions are:

1. Use a debugger, and break excution (see where your computer is stuck).

2. If a debugger is unavailable, add some printf's and print the character ID + the character state. You should be able to figure out what's wrong. (Dump it into a log file)

3. Post more code, so someone can figure out your problem.

 

You should really try suggestions 1 & 2 first, unless you want to wait for someone to figure out the problem.

Share this post


Link to post
Share on other sites
Esper S.    124

Oh, I thought I laid it out to be understandable enough sad.png Well the whole thing is simply a jump function to execute on a camera. It is safe to assume that all other translations/rotations are already coded, so lets just focus on the 'jump' function. bool x is made true when the user presses a key (whichever one you want tongue.png ), and Jump(bool) is run every frame. In case it's the m_pos part that's worrying you, m_pos is a D3DXVECTOR3. Now, with all that out the way, I just want to restate that this is not in any way how I would code a jumping function; this is just something fun I wish to try out to see if it works. The goal is to make basic code that uses no timers, no acceleration, and no gravity: a basic jump function biggrin.png

 

Ah, one more thing: I am using DirectX10, if it helps. But since this is just practice code, the DirectX/OpenGL version used doesn't matter; only making it work with the constraints.

Share this post


Link to post
Share on other sites
BLM768    325

Here's a hint: when the player presses a button, inAir is set to true, but what are the values of rising and falling?

Share this post


Link to post
Share on other sites
lomateron    491

Using all that "if" makes things longer to create and organize

 

remember that bool is a byte so it can have a value from 0 to 255.

 

it will be better if you use just one variable -unsigned char- that is one byte too and when the value of this char is

0 it means it is falling

1 it means it is in air

2 it means its falling

3 it means it is in air and falling

etc..

 

I don't know well what are you doing but you get the idea

it will be easier this way

Share this post


Link to post
Share on other sites
Esper S.    124

Here's a hint: when the player presses a button, inAir is set to true, but what are the values of rising and falling?

     I'm such an idiot!laugh.png I should have defined the three bools in the header for this kind of operation. Also, BLM768 was spot on. Since the player will rise on button press, both rising and inAir should become 'true'. Furthermore, since rising and falling are (pretty much) opposites, only one was required. I believe this is what Iomateron was getting at. Here is the finished code for a basic jump biggrin.png - who knows, someone might find it helpful:

//remember to declare "bool inAir, rising;" in your header. On initialization, just set them to false;
void Character::Jump(bool x){
 if(m_pos.y != 0)inAir = true;
 if(x){
  inAir = true;
  rising = true;
 }
 if(inAir){
  if(m_pos.y >= 10)rising = false;
   if(rising)m_pos.y += 0.5;
   if(!rising)m_pos.y -= 0.5;
  if(m_pos.y == 0)inAir = false;
 }

Nice work guys! (y)

Share this post


Link to post
Share on other sites
Muzzy A    737

You should initialize all of your bools, if not they'll just have garbage that will most likely be non-zero, which is always true. Just set them all to false.

 

Your first check is likely to always be true, given that you've got a velocity applied somewhere. You might want to make it " m_pos.y > 0"

if(m_pos.y != 0)inAir = true;

Same with your max 'y' check, for one, you don't even need the '!falling' in there, because you haven't even touched it yet.

if( m_pos.y == 10 )
 
// Should be
if( m_pos.y >= 10 )

Again this check is useless, you've only assigned to 'rising' once and you set it to false when you did that.

if(rising)

 

Basically everything here that you're doing is wrong or a bad way of doing it.

 

Those booleans that you have should be static, or preferably a data member in your moving object.

 

If you do that then you may find this to be a bit easier than you think.

Edited by Muzzy A

Share this post


Link to post
Share on other sites
Muzzy A    737

@Muzzy A
  The code has been remade and re-posted  smile.png (see comment right before yours)

You still have this

if(m_pos.y != 0);
 
// Should be
if(m_pos.y <= 0);

Though you're only adding or subtracting '0.5', in an actual game/project time is a factor and '0.00001 != 0.0', just a heads up. It will only work in your case.

 

 

I had to stop myself from writing a tutorial :P

Edited by Muzzy A

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this