• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
codingnoobie

Player Acceleration

6 posts in this topic

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.[img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img]
0

Share this post


Link to post
Share on other sites
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 [u]added[/u] 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:

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

Of course, you need to handle collision with the ground otherwise your character will speed up downwards forever. [url="https://docs.google.com/viewer?a=v&q=cache:JimCj3Qwx78J:www.sofa-framework.org/docs/vr09-time-integration.pdf+euler+physics+velocity+integration&hl=en&gl=nz&pid=bl&srcid=ADGEESgu_1WIgg0CSi1Y-sRPjJ8sm7eWxbqIM-R2F7B2VyxtEIO2c0HBcD-bRPwj6AtxaiA-QeTJm5ojxh51mIDwkjLwfXDOd0pEG9wXrLNKoGs9xZ1IUDbDaElRt-njany0qlAUMTho&sig=AHIEtbRR5fQ93R5RyD2AmDbvP_Qy-9k7dw"]You may find this useful, too.[/url] Edited by Bacterius
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:
[CODE]
heroYspeed += gravity * (1.0 / 60.0);
heroY += heroYspeed * (1.0 / 60.0);

if (heroY>=440){
heroYspeed=0; // crude ground collision detection :P
}
[/CODE]

Does it make more sense now?
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
@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. [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0