# Making Something Jump

This topic is 3727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

sounds simple, but i'm having trouble. right now i have something like:
void movey(int interval)
{
float k; int h = 0;
for (int i=0;i<m_nCount;i++)
{
int j=m_pObjectList[i]->m_nObjectType;

//3 is the enumerated type that coicides with the HERO object

if (j==3)
{
k=m_pObjectList[i]->m_structLocation.y;

while ( h <= interval )
{
if ( h < interval/2)
{
m_pObjectList[i]->m_structLocation.y += 5;
}else{
m_pObjectList[i]->m_structLocation.y -= 5;
}
h++;
}
}
}
} 
and it doesn't work, and i don't know why. i know it's not optimal, so, yeah, any help you guys could give me would be awesome.

##### Share on other sites
Do you get eroor's of some kind? If you do, then could you post them?

##### Share on other sites
I've got some mean comments here, please don't take it the wrong way. My intention is simply to correct some bad habits. :)
So, what is k? What is h? You should prefer more descriptive names - otherwise code will be puzzling you a few months later. And why not use an enum instead of a magical number (j == 3)? Also, what's with the m_ and p prefixes? Doesn't your IDE tell you that it's a member variabele and a pointer already? As for pointers, are you familiar with the STL - the standard template library, with it's convenient container classes such as std::vector?

Anyway, it doesn't really look efficient, iterating through a list of game objects only to update the hero's vertical position. Personally, I'd do that inside the hero's own update function, since I'd be updating all of my game objects anyway, so this isn't really a special case that needs to be handled separately. Depends on the game though, but if you really need to handle your hero seperately, why not store a reference to it directly, instead of having to look it up in a list?

As for why the jumping doesn't work, 'it doesn't work' is vague. What exactly is it supposed to do, and what exactly is - and isn't - it doing? Did you step through the code with a debugger already to see where things could possibly go wrong?

Another note: it looks like your jumping code affects the position of the hero directly. This results in a rather unconvincing look: at some point, the hero will immediatly start falling with exactly the same speed. For a more natural look, use a velocity value, and modify that one over time, while modifying the position with the velocity value.

Oh, and this forum supports a source tag, which allows easier code formatting and provides basic syntax highlighting. Makes posts containing code easier to read. :)

##### Share on other sites
Quote:
 Original post by cky83and it doesn't work, and i don't know why.

Let's step back for a while and talk about program flow.

Your program begins at main (or WinMain for unwrapped Windows applications), and runs straight down until it encounters a function call, a loop, or a conditional branch (if statement). If it encounters a function call, flow transfers to the top of that function and runs just like in main until it encounters a return statement or the end of the function.

In all likelihood, you have something like this:
int main(){  ...  while(someCondition)  {    // stuff    render();    // more stuff  }  return 0;}

This causes your rendering code to be executed once per loop iteration. Now, if your function movey is called inside that loop, your rendering code won't get called until the function is finished. What's the problem with that? Your function movey contains a loop which runs over the span of your "jump", without exiting once to let render get called.

You need a way to set the object's state to "jumping", and have it go through all the motions in steps over several iterations of your game loop, but without going off into another function and never coming back. One way to do that is to make your function progress in steps:
int movey(int ObjectIndex, int currentStep, int interval){  if(currentStep < interval / 2)  {    m_pObjectList[ObjectIndex]->m_structLocation.y += 5;  } else {    m_pObjectList[ObjectIndex]->m_structLocation.y -= 5;  }  return ++currentIndex;}

Use it something like this:
int main(){  ...  int jump_step;  ...  while(someCondition)  {    ...    if(object_is_jumping)    {      jump_step = movey(objIndex, jump_step, interval);    }    ...  }}

##### Share on other sites
Well one problem is that you're doing it in a while loop. What's gonna happen is its gonna jump and you'll have no idea because there's no rendering of the jump going on, you only want to update the player's location once per frame.

##### Share on other sites
i used to have it looking like this:

void movey(int time){	float k; int t = g_cTimer.time();	for (int i=0;i<m_nCount;i++)	{		int j=m_pObjectList[i]->m_nObjectType;		//3 is the enumerated type that coicides with the HERO object		if (j==3)		{			k=m_pObjectList[i]->m_structLocation.y;			while(!g_cTimer.elapsed(t, time){				if(!g_cTimer.elapsed(t, time/2)  ){					m_pObjectList[i]->m_structLocation.y += 1;				}else{					m_pObjectList[i]->m_structLocation.y -= 1;				}			}//while		}//if	}//for}

where g_cTimer.time() returns the current time, and g_cTimer.elapsed returns TRUE if the amount of time designated by int time has elapsed since t, and returns FALSE if it hasn't.

this ended up making the character disspear, tho.

Quote:
 Original post by Dancin_FoolWell one problem is that you're doing it in a while loop. What's gonna happen is its gonna jump and you'll have no idea because there's no rendering of the jump going on, you only want to update the player's location once per frame.

how would i do that?

##### Share on other sites
Sigh. This is something that has been bothering me lately. Someone comes asking for help, they get a bunch of replies, one of which contains the answer and the OP for some unknown reason decides to ignore it completely, reply to the latest reply and ask for something that was already answered.

##### Share on other sites
[quote]Original post by Oluseyi
Quote:
 int movey(int ObjectIndex, int currentStep, int interval){ if(currentStep < interval / 2) { m_pObjectList[ObjectIndex]->m_structLocation.y += 5; } else { m_pObjectList[ObjectIndex]->m_structLocation.y -= 5; } return ++currentIndex;}Use it something like this:int main(){ ... int jump_step; ... while(someCondition) { ... if(object_is_jumping) { jump_step = movey(objIndex, jump_step, interval); } ... }}

ok, i see what's going on there.

i've got a few questios, tho.

1) what is ++currentIndex reffering to?

2) in the while(someCondition), what would someCondition be?

i have a switch case in main that says
case VK_SPACE: g_cObjectManager.movey(200);

where ObjectManager handles things like the character moving and jumping.

the switch case is not located inside a while loop, it gets called anytime the space is pressed.

3) in this example, what would be the case to see if object_is_jumping is true?

##### Share on other sites
ok, i think i've done something like that, here's what i have now:

void ObjectManager::jump(int time){	int step = 0;	int t = g_cTimer.time();   	while(!g_cTimer.elapsed(t, time)){		step = jump(step, 10);	}//while}int ObjectManager::jump(int current, int interval){	if (current < interval/2){		m_pObjectList[m_nHeroIndex]->m_structLocation.y += 5;	}else{		m_pObjectList[m_nHeroIndex]->m_structLocation.y -= 5;	}	return ++current;}

and what will happen is that after time amount of time has passed, the character will dissappear.

NOTE: Timer.time() gets the current time, and Time.elapsed(int, int) will return TRUE if the time between t and time has elapesed, and FALSE if it has not.

also, like i said above, jump(int time) will only get called if the space bar is pressed, due to the switch statement in main.

##### Share on other sites
Quote:
 Original post by cky83void ObjectManager::jump(int time){int ObjectManager::jump(int current, int interval){

these two methods are the same name, one is for time while the other is input values for current and interval? if they are the same method you need: jump(int time, int) when using jump(time, ). Only reason I ask they are different is one is void and the other is returns a int.

abrev. names are fine but if you dont know what they are then make them descriptive. bigDogTailWags(). smallDogTailWags(). jumpTimeStart(), jumpTimStp(). This helps other people help you...

##### Share on other sites
Your game architecture is completely flawed.

Your game runs in one continuous loop. All updates to the objects, polling of input and rendering (graphics, audio, force feedback, etc) are done within this one single loop. Now, as part of any individual action, a small loop may run, but that loop must terminate as quickly as possible, so as not to stall all the other activities in the game loop.

Because we want to localize "physical" updates, what we do is set reminders for ourselves that we intend to update certain attributes in certain ways. Flags, basically. They're particularly important for input handling, as they allow us to appear to handle multiple inputs at the same time. Example:
while(gameRunning){  ...  // this is where we obtain and process the input, but note that we   // don't update the game objects  GetKeyboardState(keys);  for(int i = 0; i < NUM_KEYS; ++i)  {    switch(keys[i])    {    case VK_SPACE:      if(!isJumping)      // we don't allow jumping while jumping,         isJumping = true; // and all we do is set a flag      break;    ...    }  }  ...  // here, we update the game object(s) by checking the various flags set earlier  if(isJumping)  {    // jumping code needs to operate in steps, and then as the     // final step clear the flag    step = jump(step, interval);    if(step >= interval)      isJumping = false;  }  ...}

##### Share on other sites
Quote:
 Original post by Oluseyi // here, we update the game object(s) by checking the various flags set earlier if(isJumping) { // jumping code needs to operate in steps, and then as the // final step clear the flag step = jump(step, interval); if(step >= interval) isJumping = false; } ...}[/code]

ok, i think i'm starting to understand, but wouldn't that only run once? wouldn't you need it in a loop of some kind so it keeps changing step until it's more than interval?

##### Share on other sites
honestly, the types of questions and counter-questions you are asking make me think that you are really only at a stage where you NEED to be making the mistakes you are making... but also for YOU to figure out the answers (learn why you don't put your hand on the stove etc). If at this stage you don't get an understanding within yourself of why something like this doesn't work then you are setting yourself up to be a continuous help needer because you don't really get what is and isn't actually going-on and won't have a grasp on things. Really, you are better-off just copy/pasting or using someone elses already polished code if this is the route you are heading.

but as someone else stated earlier, perhaps if you had more meaning in your question and code and described what is and isn't happening then we might be able to help more... but as it stands it really just looks like another "write my code for me" question. perhaps you could re-do your question, particularly since you have had a chance to make modifications from the original post and since you actually have multiple questions which you are spanning over different counter-questions. clear + concise = win. list your issues etc.

either way good luck.

##### Share on other sites
Quote:
 Original post by cky83ok, i think i'm starting to understand, but wouldn't that only run once? wouldn't you need it in a loop of some kind so it keeps changing step until it's more than interval?

while(gameRunning){  ...  if(isJumping)  {    ...  }  ...}

As you can see, it is inside a loop. Pay attention.

##### Share on other sites
Quote:
 Original post by nbhonestly, the types of questions and counter-questions you are asking make me think that you are really only at a stage where you NEED to be making the mistakes you are making... but also for YOU to figure out the answers (learn why you don't put your hand on the stove etc). If at this stage you don't get an understanding within yourself of why something like this doesn't work then you are setting yourself up to be a continuous help needer because you don't really get what is and isn't actually going-on and won't have a grasp on things. Really, you are better-off just copy/pasting or using someone elses already polished code if this is the route you are heading.but as someone else stated earlier, perhaps if you had more meaning in your question and code and described what is and isn't happening then we might be able to help more... but as it stands it really just looks like another "write my code for me" question. perhaps you could re-do your question, particularly since you have had a chance to make modifications from the original post and since you actually have multiple questions which you are spanning over different counter-questions. clear + concise = win. list your issues etc.either way good luck.

well, i don't know if this helps or not, but before i even made this thread, i was able to get the guy to jump if i held down the space bar, but i'd like to have the guy jump if i just tapped the space bar.

don't know if that's what you guys are trying to show me how to do, but if it is, i guess i'm just not getting it. i'll just go to my proff tomorrow and ask him for some advice since he's the one who created most of the code i'm working with. i was just trying to get something done over the weekend, and was having trouble.

and i'm not trying to have you guys write it for me, i was looking for an explanation more than anything, and i guess i got the explanation, but i'm just not understanding it fully.

##### Share on other sites
sounds like maybe u have issues elsewhere in your code too... like in your 'interval' setting in the original post, or 'time' in your last code showing. figure out a way to breakpoint it, or even better have it display somewhere constantly as the program runs (like textout to the top left corner or to a TLabel etc).

i get the feeling you aren't storing the values you think you are and so the hero will only go up when you press the spacebar as that's the only time your if condition will ever be true and all other times your variables get trashed to values you don't mean them to. it's a bit hard to tell looking only at that piece of code because you seem to be using variables outside the scope of that function, plus the naming convention is giving me a headache to try to decipher.

it seems that what you are trying to accomplish is something like

NowTime = ComputersCTimeJumpingForTime = 1000(milliseconds) //or whatever time you want or is sent to the setjumpingstate function etcJumpUntilTime = NowTime + JumpingForTime //you want to store this variable so you can keep re-testing below each time the movement code is called in the game loop...JumpTimeElapsed = JumpUntilTime - NowTimeif JumpTimeElapsed <= (JumpingForTime / 2) then hero.y += 5else hero.y -= 5

yes no?

##### Share on other sites
I think his problem is a lack of understanding of object lifetimes. For instance, the isJumping flag that I used in my earlier example will reset each iteration if you declare it within the while loop, which would break the code. It's obvious to the more experienced among us that the solution is to move the declaration of isJumping outside the loop.

##### Share on other sites
haha oh dear, i just looked at what i wrote and guess what... i messed that up. let's have another (better?) stab at it. keep in mind i code in delphi not C so this is dodgy and hackish and probably mixes Delphi and C and laziness conventions etc just to get the point across as best as i can manage. round 2 :

var CurrentCTimevar JumpStartTimevar JumpDurationvar JumpingBoolvar GameRunningBoolfunction SetJumping(IncomingValForJumpDuration) {  JumpStartTime = CurrentCTime  JumpDuration = IncomingValForJumpDuration  JumpingBool = 1}function MyGameLoopage {var JumpTimeElapsedvar CurrentCTime = CTime()  if (KeyDown = vk_Space) and NOT(JumpingBool) { SetJumping() }  if (KeyDown = vk_Escape) { GameRunningBool = 0 }  JumpTimeElapsed = CurrentCTime - JumpStartTime  if JumpTimeElapsed <= (JumpDuration / 2) then hero.y += 5  else hero.y -= 5  if hero.y <= 0 {    hero.y = 0    JumpingBool = 0  }}function MAIN_PROGRAM_FUNCTION {  //init cool stuff  JumpingBool = 0  GameRunningBool = 1  While (GameRunningBool) {    ProcessMessages()  //don't completely kidnap the cpu    MyGameLoopage()  //run a game cycle  }  //renderer cleanup code etc}

actually, considering what i said before about it's better for you to figure this out yourself etc etc etc... i think i've basically gone against what i said and given you pretty much exactly how i would do it right now. i wonder how close that code is to being compilable (probably not very). meh, what can i say, i screwed the first example and now i'm bored - so enjoy.

you then have other things to possibly consider like velocities etc instead of the hero (and whatever else you code) jumping in a constant up then down 8-bit-feeling manner as opposed to a fluid more realistic manner. but that's probably not something you really care about right now. it eliminates the whole "JumpingBool" toggle and adds a whole new world of complexities that make it better, but requires more code and opportunity to screw up and all just for something that seems to be meant to only be really basic.

you've mentioned your professor. what IS this all for? high school? (god forbid) uni? is it a homework assignment?