-
Gravity
Hi..
I am trying to make a basic side scroller game and I am wondering how I should implement gravity.
I wrote something and it works but i am wondering if there is another or better way to do it.
vel_y, tmp_y, y, x, h, and w belong to the object that's subject to gravity.
Code:
if(vel_y < MAX_SPD) vel_y += G;
tmp_y += vel_y;
for(i = 0; i < 15; i++ ){
if((tmp_y+h > blocks[i].y) && (y+h < blocks[i].y + blocks[i].h) && (x > blocks[i].x) && (x < blocks[i].x + blocks[i].w)){
tmp_y = y;
vel_y = G;
y = blocks[i].y - h;
break;
}
y = tmp_y;
}
Also i would be grateful if you could recommend some books.
-
Gravity is not simply a unit change of velocity, so should not just be added to a velocity. It is an acceleration (i.e. a continuous - ongoing - change of velocity over a time period) associated with a force that attracts two bodies to each other.
Look up "Newton's Law of gravitation" to better understand what gravity is, and how to represent its effects on objects. Look up "kinematic equations" to understand what the results are when an acceleration is applied to a moving object.
Coding up actual equations to handle such things is trivial.