Archived

This topic is now archived and is closed to further replies.

compfanatic

Making a frog jump...

Recommended Posts

I am in the process of creating my first game, and it is a frog game. I am trying to figure out how to make the frog jump. Here is my idea, but I have not tried it, and I definitely doubt it works: if (KEY_DOWN(VK_UP)) { frog.y+=8; // have the frog increase y-coords frog.y-=8; // then have the frog''s y-coords decrease } This isn''t all of the code that would be involved in making it jump, but I would like to know how you would do it. Thanks! -Alex

Share this post


Link to post
Share on other sites
Have you had any physics classes ?

Jumping is all about physics........
                
#define INIT_VELOCITY 20

#define GRAVITY_FACTOR 1

#define MAX_VELOCITY_DOWN -90

#define RUN_RATE 8

#define FLOOR_HEIGHT 480 // if bottom of screen 480



if (jump key pressed)
{
playerVelocity = INIT_VELOCITY;
do{
if (key over is pressed)
{
player.x += RUN_RATE;
}
player.y -= player.velocity;
player.velocity -= GRAVITY_FACTOR;
}
while(playerHeight >= FLOORHEIGHT);
}






Edited by - ncsu121978 on June 14, 2000 5:10:44 PM

Share this post


Link to post
Share on other sites
now instead of doing to do while loop like a have it in the above example, for a game you would do it like this

if the jump key is pressed then set the player velocity to the initvelocity like i have it

then in the update fram loop of your game


if (playerHeight > FLOORHEIGHT)
{
player.y -= player.velocity;
player.velocity -= GRAVITY_FACTOR;
}




"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
if you increase then init_velocity then he will jump higher and jump up faster

if you increase the gravity factor he will jump lower and fall faster

this two values work hand in hand so you can mess with both of them until you reach the jump height and jump speed you want

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
Do you mean that he jumps in the air but then not back down ?
If that is the case then make sure you have all your signs right (all the pluses and minus)..........it should work fine......as long as when you
#define FLOOR_HEIGHT 480

if 480 is at the bottom of your screen ?
answer me some questions
what compiler are you using ?
what language ?
what graphics library ?


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
Ok, I am using Visual Studio 5, DirectX, C++. The bottom of my screen IS 480. I noticed that you defined MAX_VELOCITY_DOWN but did not use it, and when you say "player.velocity," which velocity are you talking about? y-velocity, or x-velociy?

Thanks Again!!!
-Alex

Share this post


Link to post
Share on other sites
Why don't you try using a sine wave. Then when his y position is < or = to the minumum value you stop the jump.
    
frog_state=JUMP;
if(frog_x<min)
frog_state=WALK;

or something like that.

Edited by - Julio on June 15, 2000 3:15:12 PM

Share this post


Link to post
Share on other sites
the player.velocity is the y-velocity

max_velocity_down would be used if you are falling from a great hieght ........if he continues to fall unimpeeded then his fall rate will get faster and faster......this would just put a cap on how fast he can fall
[sourec]
if (player.velocity < MAX_VELOCITY_DOWN)
player.velocity = MAX_VELOCITY_DOWN)
[/source]
the reason why you say player,velocity less then MAX_VEL_DOWN is because MAX_VEL_DOWN is a negative number



"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
compfanatic, maybe you should learn C a little better, as :
if (KEY_DOWN(VK_UP))
{
frog.y+=8; // have the frog increase y-coords

frog.y-=8; // then have the frog''s y-coords decrease
}


would not change frog.y. No Physics needed to see that Im just thinking maybe a simpler project would take less effort and put you on your way a little faster.(no offensive, just a thought).

Share this post


Link to post
Share on other sites
Actually it does change the value, for .000000001th of a second that is, then it goes right back, you might try to Sleep(100); in the middle of that.

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

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Share this post


Link to post
Share on other sites
Compfanatic, you might try something like this:

if(KEY_DOWN(VK_UP) && !frog.Jumping) // test if frog is already jumping
{
frog.yvelocity -= 8;
}

Then every frame update the frog''s position:

frog.yvelocity += GRAVITY_CONSTANT; // add gravity to fall
frog.y += frog.yvelocity; // move frog

But there''s still a problem.. if you do this the frog will jump, then fall off the screen. You need to add some sort of collision detection to prevent the frog from falling thru the ground or keep track of the frog''s starting position. Collision detection is more difficult and can implemented in different ways, so keeping track of the frog''s starting position is probably easier. Just add another line of code next to the ''frog.yvelocity -=8;'' that says something like ''frog.startY = frog.y;''. Then in the part that updates the frog''s position every frame add something like ''if (frog.y >= frog.startY){ frog.y = frog.startY; frog.yvelocity = 0; frog.jumping = false}'' and you''ll need to stop the gravity from pushing the frog off the screen. Hope that helps some.

Alex

Share this post


Link to post
Share on other sites

#define INIT_VELOCITY 8
#define MAX_VELOCITY -20
#define GRAVITY 1

if (jump key pressed)
{
if (!player.jump)
{
player.velocity=INIT_VELOCITY;
player.jump=1;
}
player.y-=player.velocity;
if (player.velocity>MAX_VELOCITY)
player.velocity-=GRAVITY;
}

if (player on ground)
player.jump=0;


This piece of code will work fine.
If the player presses UP, the code will check if jumping should be initialized (start with the correct velocity). Then, the player''s y-coordinate is updated, as is the velocity (which cannot be smaller than MAX_VELOCITY).
As long as player.velocity is positive, the player will go up. When it becomes negative, the player will fall down until something stops it from falling down

-my 2 cents-

Share this post


Link to post
Share on other sites
quote:

Actually it does change the value, for .000000001th of a second that is, then it goes right back, you might try to Sleep(100); in the middle of that.



that might work but then u would have a frog that jumped like
(F = Frog path)


/ F F F F
/ F F
/ F F
/ F F
/---------------------------------------------------





+-----------------------------
Come, help me program my 3d game, i just picked up a new copy of Learn C++ in 21 days!
--Unknown Newbie
+-----------------------------

Share this post


Link to post
Share on other sites
Ok, I figured out what I am supposed to do, sort of.

--- If the player press a button, the frog jumps.
--- At a certain point the frog''s velocity decreases.
--- At a certain point the frog''s velocity is reversed so the frog begins to land.

The problem is, I am having trouble declaring the "certain points."

For example:#define INIT_VELOCITY 8
#define MAX_VELOCITY -20
#define GRAVITY 1

if (jump key pressed)
{
if (!player.jump)
{
player.velocity=INIT_VELOCITY;
player.jump=1;
}
player.y-=player.velocity;
if (player.velocity>MAX_VELOCITY)
player.velocity-=GRAVITY;
}

if (player on ground)
player.jump=0;


I am not sure how I would declare the "if player is on the ground" part. Would I do something like "if collision with ground?"


Thanks!
-Alex

Share this post


Link to post
Share on other sites
You don''t need to know the "certain point". Gravity always acts upon an object. If it has some sort of vertical component (IE, parallel to the direction of gravity), than gravity will attempt to decrease the velocity by a given ammount per second. So on earth, where gravity is ~9.8 m/s^2, you would be going 9.8m/s after 1 second of falling from rest (v = at). I am sure you already knew this. I am not trying to patronize.

So really what you want is this (and I am going to change it around so the ground is 0, and the frog is jumping up towards like 100 or something, gravity will be positive, but it will be subtracted):

    
#define GROUND 0
#define GRAVITY 10 // Approximately Earth Gravity


if (KEY_DOWN(VK_UP)) /* Why would you change the X coords if he is jumping up, wouldn''t you only want to change the Y? Is the frog going straight up, or over to the side also? */
{
frog.velocityY = 100;

/* You should really have a timer here, you will get better results, have it update the position every .1 seconds, it will be smooth at 10fps that way, or .5 for 20fps, or whatever you want... */

while (frog.y >= GROUND)
{
sleep(50) /* Sleep for 50 MS (IE, update 20 times per second) */

frog.y = frog.y + frog.velocityY*0.05; /* The .05 is because that is the fraction of a second we are using... */

frog.velocityY = frog.velocityY - GRAVITY*0.05; /* See equation above, all I am doing is decreasing the velocity, do it after the incrementing the position, be ready for next time around. This ensures it all works the first .05 seconds. */

}
}


See? Eventually the velocity will be negative, and then when you add the velocity to frog.y, it will start going towards the groung, eventually hitting it, where the loop will stop. I don''t know if this is what you want, cause it might hinder other movement, but hey, it does work (and model a frog, or ball or whatever) well. I think. I haven''t compiled it to see. Hope this helps.

Share this post


Link to post
Share on other sites
Thanks a lot!

Now I understand the concepts of it.

Now, what if the frog is standing on a lilly pad? The ground wouldn''t be 0, so would I need some sort of collision detection?

Thanks again!!!

-Alex

Share this post


Link to post
Share on other sites
So make the ground height a variable. You don''t really need collision detection, just make sure the frog stops going down when his height matches the lillypads height. Sure, this isn''t an elegant collision detection system, but if you wrap it up in a couple classes (or just one) and use something like frog->groundHeight and stuff, it will be fine. And bugs in your implementation will be easily seen, cause the frog will start going through the ground, or into heaven... Hope that helps.

Share this post


Link to post
Share on other sites