Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Jumping - What did I do wrong?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Esper S.   Members   -  Reputation: 124

Like
0Likes
Like

Posted 16 July 2013 - 02:23 PM

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



Sponsor:

#2 SillyCow   Members   -  Reputation: 899

Like
1Likes
Like

Posted 16 July 2013 - 03:00 PM

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.


My new android game : Enemies of the Crown

My previous android game : Killer Bees


#3 Esper S.   Members   -  Reputation: 124

Like
0Likes
Like

Posted 16 July 2013 - 03:51 PM

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.



#4 BLM768   Members   -  Reputation: 295

Like
2Likes
Like

Posted 16 July 2013 - 04:14 PM

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



#5 lomateron   Members   -  Reputation: 363

Like
1Likes
Like

Posted 16 July 2013 - 04:17 PM

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



#6 Esper S.   Members   -  Reputation: 124

Like
0Likes
Like

Posted 16 July 2013 - 04:53 PM

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)



#7 Muzzy A   Members   -  Reputation: 693

Like
0Likes
Like

Posted 16 July 2013 - 05:02 PM

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, 16 July 2013 - 05:04 PM.


#8 Esper S.   Members   -  Reputation: 124

Like
0Likes
Like

Posted 16 July 2013 - 05:11 PM

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



#9 lomateron   Members   -  Reputation: 363

Like
2Likes
Like

Posted 16 July 2013 - 07:27 PM

if(m_pos.y != 0)inAir = true;  <------this kind of code can be transformed

 

inAir = (m_pos.!= 0) ;  <----to this



#10 Muzzy A   Members   -  Reputation: 693

Like
1Likes
Like

Posted 16 July 2013 - 11:28 PM

@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, 16 July 2013 - 11:29 PM.


#11 Migi0027   Crossbones+   -  Reputation: 2129

Like
0Likes
Like

Posted 17 July 2013 - 02:56 PM

if(m_pos.y != 0)inAir = true;  <------this kind of code can be transformed

 

inAir = (m_pos.!= 0) ;  <----to this

 

I like that! huh.png


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS