Advertisement

Making a frog jump...

Started by June 14, 2000 03:52 PM
19 comments, last by compfanatic 24 years, 5 months ago
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

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).
Advertisement
I fail to see what this question is doing in the DirectX/OpenGL... forum so I''m moving it.

- WitchLord

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
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 fallfrog.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
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.

#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-
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------
Advertisement
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
+-----------------------------
Those who dance are considered insane by those who cannot hear the music.
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

I looooove programming! Even at my young age.
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 Gravityif (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.
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
I looooove programming! Even at my young age.

This topic is closed to new replies.

Advertisement