Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Gravity, jump, physics : any advices?  (Read 3871 times)

0 Members and 1 Guest are viewing this topic.

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Gravity, jump, physics : any advices?
« on: February 01, 2014, 04:30:55 pm »
Hi everybody,
I am interested into the creation of a 2D volley ball game, which as to be as realistic as possible.
In first, i want to create a prototype which allow to :
- display 2 circleshapes ( 1 ball and 1 player )
- player shape is located on the bottom on the screen and we can just make him moving left or right, and jumping by a realistic way( i mean with gravity )
- ball shape spawn in top of screen and fall with gravity; it has also to rebound with player, walls and ground

What I already have:
- player shape which move left or right, and a basic jump implemented:
( i want a real jump so i check on the internet to find equations, i found something like
y=y0 -(1/2*g*t*t) + v*t )

- every collisions test
- rebound against walls

I also have a formula to calculate rebound between two circles but i haven't implemented it at the moment;

So my questions are :
- is it best to use " myObject.move(mySpeed) " or " myObject.setPosition(x,y) " ?
- by trying to implement a realistic jump, i did this :

if(inAir)
               speed.y+=k*grav*clock.getElapsedTime().asSeconds();

where k is a fixed constant,
grav is equal to 9.81

But I have some difficulties with modiying speed.y when the player is on the ground and want to jump...
For example, when do i have to do a clock.restart() ?

Thank you for advance,
Have a good day ! :)

Kulade

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Gravity, jump, physics : any advices?
« Reply #1 on: February 01, 2014, 10:22:10 pm »
Well, I'm not sure if you want to build the physics up by yourself, but if you don't, you should check out the Box2D library. A lot of people use it with SFML. Give the documentation a thorough look, make sure you understand the necessary concepts, and you should have a somewhat simple way to handle all the physics.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: Gravity, jump, physics : any advices?
« Reply #2 on: February 02, 2014, 12:18:57 am »
Read this: http://gafferongames.com/game-physics/integration-basics/ and the article that follows it. Basically u want to restart the clock every frame and pass the dt to the physics solver.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Gravity, jump, physics : any advices?
« Reply #3 on: February 02, 2014, 06:28:15 am »
Best way is to have a position, which is altered by velocity, which is altered by acceleration. You jump, and you add -jumpAcceleration to the player. This will have the player accelerate up. At the same time, you apply a constant +gravityAccleration, all the time (9.8m/s/s or 32ft/s/s. jump accleration is negative because negative is up and positive is down). Thus, your character will launch up because their y velocity will go from 0 to -jumpAcceleration. They will continue to move up each frame at a slower and slower rate each frame because gravity is constantly being added to their velocity (making them move up less because up is negative y). Eventually their velocity will be 0 and they will then begin to fall as gravity keeps adding positive y acceleration. They will then hit the ground, which will prevent them from moving any more in the positive y direction. This allows you to completely ignore time in your calculations. The flow of time should be done in the main loop section of your game. http://gafferongames.com/game-physics/fix-your-timestep/ That way you can ensure that all physics simulations in your game are both on the same time frame and they are all flowing at the same rate and you can guarantee that rate.

This way of handling jumping and gravity will give you easy and true movement. You will see a nice parabolic graph of velocity, which is exactly how objects move in real life as they go up and down through the air.

MaxdeoxiS

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Gravity, jump, physics : any advices?
« Reply #4 on: February 02, 2014, 03:25:41 pm »
Hmm, interesting way of doing this, Azaral. I'm going to try to implement this. Thank you both eigenbom and Kulade, i'll read this article just after; and i'm don't want to use Box2D for this project at the moment ^^
I'll come back later if I have any problems, see u!