Jump to content

  • Log In with Google      Sign In   
  • Create Account

Player Acceleration


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 codingnoobie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 09 September 2012 - 08:48 PM

I've been trying to implement a simple acceleration for a character you can control, but for the life of me I cannot quite grasp it. :/

I know the equation, acceleration = velocity/time, gravity = -9.8m/s^2 but how can you implement time?

for vertical movement

int playerY;
int playerYvelocity= gravity * time?
int gravity = -10;

public void update(){
playerY += playerYvelocity;
}

Hmmmmm. Hmmmmm.Posted Image

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9286

Like
2Likes
Like

Posted 09 September 2012 - 09:09 PM

Time is (usually) either a fixed amount each frame (or physics update) or sometimes you actually take into account the real elapsed time (measured by getting the computer's clock at the last update and before updating) and using that as a time delta.

As for the code, that's not quite correct, you should be adding gravity * time to the player's Y velocity - remember it is an acceleration and acceleration is added to velocity as a vector (otherwise there wouldn't be much point in differentiating them). And the usage of time is slightly different but it really depends on what integration method you use, the most basic (and unstable) one is below. So you need to keep track of both the player's position and velocity, and each frame, go:

playerVelocity += gravity * time;
playerPosition += playerVelocity * time; // multiply by time again! google "explicit integration forward Euler" for explanations.

Of course, you need to handle collision with the ground otherwise your character will speed up downwards forever. You may find this useful, too.

Edited by Bacterius, 09 September 2012 - 09:09 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 codingnoobie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 10 September 2012 - 01:05 AM

I looked into it, messed around with delta again, and I set the fps to 60 and I could finally got it working, kind of.

Below is the code that I got working.

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
// unit = ms?
counter += delta;

heroYspeed+=gravity*counter;
heroY+=heroYspeed;

if (heroY>=440){
counter=0;
heroYspeed=0;

Input input= gc.getInput();
if(input.isKeyDown(Input.KEY_UP)){
heroY=0;
}

}


I know I didn't use heroY += heroYspeed * time; but the character disappears when I use that code. It made sense that velocity * time would be distance and I wanted it to work, but I'm tired from wrapping my head around a few concepts, ha, ha.... (I spend some time in khan academy, and exercising my brain :P)

I also want to find out about the relationship between delta, fps and update time. It's vague to me right now.

#4 Bacterius   Crossbones+   -  Reputation: 9286

Like
1Likes
Like

Posted 10 September 2012 - 01:33 AM

If your FPS is locked at 60, try using a constant delta of 1 / 60 seconds, for debugging purposes. Also, what is the purpose of "counter" in your code? I'm probably not explaining it all too well but the velocity and position increments are per-frame, so they need to be moderated by some time delta to make sense, this is what the multiplication by the delta is for.

If you think you need the "counter" so that the player can actually accelerate, then no, it's not needed - that's the beauty of it: the double effect of acceleration on position (velocity += acceleration, and then position += velocity) results in position depending on acceleration squared, naturally producing the parabola you'd expect from an object subject to gravity.

It would look somewhat like this:
heroYspeed += gravity * (1.0 / 60.0);
heroY += heroYspeed * (1.0 / 60.0);

if (heroY>=440){
heroYspeed=0; // crude ground collision detection <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/tongue.png' class='bbc_emoticon' alt=':P' />
}

Does it make more sense now?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 BeerNutts   Crossbones+   -  Reputation: 3007

Like
1Likes
Like

Posted 10 September 2012 - 01:20 PM

What are you using for gravity? If you are using 10, realize that in feet for distance, but you are operating in a virtual world, and displaying in pixels. For simplicity, you can use pixels as the distance, and using 10 would mean your player would increase his VELOCITY by 10 pixels/second. You may need to modify that value to get it to what you would like.

Also, be sure you are storing player velocity and position as a float, or else you will lose a lot of precision.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#6 codingnoobie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 10 September 2012 - 01:35 PM

@Bacterius
I think so.

What you are saying is *time isn't necessary because it's already there as it's updated every set interval, right?

I tested it out, and I'm happy to see it working. :D

@BeerNutts
I wanted to use float but I didn't know how big is 1f in comparison to 1 pixel so, I used integers and doubles, for now. :-P

I plugged this in as gravity:

double gravity=980/360; (converting at 1m=100pixels, and 60 delta = second)


I made the heroYspeed decrease in value when key.space is inputted, so that he jumps with deceleration and falls with acceleration. Posted Image

I want to tweak the values around a bit, but I think I will move on to horizontal movement and friction. Khan academy, maybe?

Edited by codingnoobie, 10 September 2012 - 01:42 PM.


#7 BeerNutts   Crossbones+   -  Reputation: 3007

Like
0Likes
Like

Posted 10 September 2012 - 03:09 PM

When I said float, I just meant not an integer. A double is probably better to use than float, when speaking in C/C++ contexts these days.

You should not be using integers anywhere during these computations. 1f is 1, doesn't matter what it's representing. If you have player position as an integer, then, when you add a partial distance, say 1.9 pix, you're only going to be adding 1 to the position, and the next time you add 1.9 pix, it'll be 1 again, for 2 pixels in 2 frames. Whereas, if you use floats, adding 1.9 to the position, the player will be shown on the screen only moving 1 pixel (since pixels are discrete), but the next frame, the player's position will have moved 1.9+1.9 = 3.8 pixels, and be shown as 3 pixels on the screen. it just gets worse after that.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS