jump algorithm

Started by
33 comments, last by phil67rpg 4 years, 6 months ago

here is my stubbed out code


#include<iostream>

using namespace std;
 
int main()
{ 
	float g = -1.0f;
	float Vy = 5.0f;
	float Vx = 3.0f;
	float time = 0.0f;
	float Po = 0.0f;
	float positionY = 0.0f;
	float positionX = 0.0f;

	cout << "Time: ";
	cin >> time;
	cout << endl;

	for (int t = 0; t < time; t++)
	{
		positionY = 0.5f*g*(t*t) + Vy * t + Po;
		cout << positionY << endl;
	}
	
	cout << endl;

	for (int t = 0; t < time; t++)
	{
		positionX = Vx * t + Po;
		cout << positionX << endl;
	}

	system("pause");
	return 0;
}

 

Advertisement

Looks right to me.

I am not sure how you intend to apply this to your game though (or how the video suggests you do that)?

What you have is the closed-form equation that describes the motion of your entity jumping under gravity from its initial state (initial position and initial velocity).

Depending on your game you may be able to use that approach but often games will use numerical integration for this instead. I.e Track the current physical properties of the entity (e.g. current position & current velocity) and integrate them with respect to the frame's time delta to attain the new updated position for that frame.

A full-blown physics library would do all that for you (along with collisions, joints, etc), but lots of games don't need or want a full-blown physics engine so they just code the basic motion equations using (for example) Euler integration as it is dead simple to code and understand, although I typically will use velocity Verlet integration as it is more accurate (and still pretty simple).

This looks like a decent source for more info: http://lolengine.net/blog/2011/12/14/understanding-motion-in-games

 

Thanks for sharing @dmatter ! :) 

Programmer and 3D Artist

 well I have put this code into my game but for some reason it does not jump, can someone please explain to me why this does not work, it looks like it should work.


void spritejump()
{
	for (int t = 0; t < time; t++)
	{
		positionY = 0.5f*g*(t*t) + Vy * t + Po;
		cout << positionY << endl;
	}

	for (int t = 0; t < time; t++)
	{
		positionX = Vx * t + Po;
//		cout << positionX << endl;
	}
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		spritejump();
		break;

 

C'mon, phil, how many times do people have to explain this stuff to you. Make a small movement, draw, make a small movement, draw. If you make all your movements at once in a single for loop it wont work.

This reminds on when i learned programming as a child from the C64 manuals. There was an example moving a sprite within a loop, and it took me weeks to figure out i can just change the position every frame based on key press, NOT using a loop, haha :)

Here is a snippet comparing closed from solution to integration for free fall under gravity:


const float timestep = 1.0f / 60.0f;
const float start = 0;
float p = start; // position
float v = 0; // velocity
float a = -10.0f; // constant gravity acceleration
for (float t = 0; t <= 2; t += timestep)
{
	v += a * timestep;
	p += v * timestep;
    cout << "integrated position: " << p << " velocity: " << v << endl;                 

	float pCF = start + 0.5f * a * t*t;
	float vCf = a * t;                     
	cout << "closed form position: " << pCF << " velocity: " << vCF << endl;
}

The numbers should closely match, aside from some small integration error. (Otherwise i did something wrong)

Closed form would be more accurate, but it's useless for what you want to do.

You want to use integration, likely doing it once each frame with a fixed timestep to advance time for the 'small movement'.

And to make a jump you would add an impulse to velocity, somehow like this:


	GameLoop ()
	{
	if (KeyUP && onGround) sprite.velocity -= 5;
	const float dt = 1/60;
	IntegratePhysics(dt); //  sprite.velocity += gravity*dt; sprite.position += sprite.velocity * dt;
	Draw();
	}
	

well joe I implemented your code but it still does not jump.


void spritejump()
{
	const float timestep = 1.0f / 60.0f;
	const float start = 0;
	float p = start; // position
	float v = 0; // velocity
	float a = -10.0f; // constant gravity acceleration
	for (float t = 0; t <= 2; t += timestep)
	{
		v += a * timestep;
		p += v * timestep;
		cout << "integrated position: " << p << " velocity: " << v << endl;
		drawSprite(p);
		float pCF = start + 0.5f * a * t*t;
		float vCF = a * t;
		cout << "closed form position: " << pCF << " velocity: " << vCF << endl;
		drawSprite(pCF);
	}
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		spritejump();
		break;

 

23 hours ago, JTippetts said:

C'mon, phil, how many times do people have to explain this stuff to you. Make a small movement, draw, make a small movement, draw. If you make all your movements at once in a single for loop it wont work.

How many times do we have to repeat this for you?
Fix your game loop, Phil, or stop wasting people's time.

I am sorry I am going to do some research on game loops. 

It's important to understand that you must draw a complete picture every time the hardware refreshes the screen. Such a picture is then called a frame. You create motion by drawing objects at a slightly different position each frame. Make sure you get this concept down before you move on. Then find some examples that implement it correctly and work from there. Good luck!

This topic is closed to new replies.

Advertisement