•      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!

### #ActualEndemoniada

Posted 12 April 2013 - 11:02 PM

Hi guys,

I'm still really not sure about how to implement jumping and gravity:

float3 position(0,0,0;
float3 velocity(0,0,0);

// this sums the forces
float3 sumForces()
{
float gravity=-4.0f;
return float3(0,gravity,0);
}

// this gets called every frame
void update(float dt)
{
// mass = 1 so acceleration = forces
float3 acceleration=sumForces();

position += velocity*dt;
velocity += acceleration*dt;
}

// this gets called when controller input changed
void onInput()
{
if(button_pressed){
// add jump but to what ?
forces.y=10.0f;
}
}


What do I do when the button is pressed for a jump ? I think I should add a positive y-value to the forces but where ? It has to go away after a second or two.

And my velocity y-value will continually decrease because of gravity so I have to clamp that to a lower limit, but what should that be ? Shouldn't my guy go faster and faster the deeper he falls ?

Thanks.

### #2Endemoniada

Posted 12 April 2013 - 11:00 PM

Hi guys,

I'm still really not sure about how to implement jumping and gravity:

float3 position(0,0,0;
float3 velocity(0,0,0);

// this sums the forces
float3 sumForces()
{
float gravity=-4.0f;
return float3(0,gravity,0);
}

// this gets called every frame
void update(float dt)
{
// mass = 1 so acceleration = forces
float3 acceleration=sumForces();

position += velocity*dt;
velocity += acceleration*dt;
}

// this gets called when controller input changed
void onInput()
{
if(button_pressed){
// add jump but to what ?
forces.y=10.0f;
}
}


What do I do when the button is pressed for a jump ? I think I should add it to the forces but where ? It has to go away after a second or two.

And my velocity y-value will continually decrease because of gravity so I have to clamp that to a lower limit, but what should that be ? Shouldn't my guy go faster and faster the deeper he falls ?

Thanks.

### #1Endemoniada

Posted 12 April 2013 - 08:04 PM

Hi guys,

I'm still really not sure about something. I'm using a simple 'vehicle' model:

force = truncate (sum_of_forces, max_force);
acceleration = force; (since mass = 1)
velocity = truncate (velocity + acceleration, max_speed);
position = position + velocity;

What I don't fully get is handling it for each frame. Do I maintain the acceleration and velocity in actual values or in units per second, like this (notice the difference in the calculations):

void updateFrame(float t)
{
// in units per second
acceleration += truncate(forces,max_force);
velocity += acceleration;
position += velocity*t;

// or in actual units
acceleration += truncate(forces,max_force)*t;
velocity += acceleration*t;
position += velocity;
}


For basic movement I can't see a difference but now I'm adding jumping and gravity and am a little confused.

Thanks.

PARTNERS