출처 : [[http://www.rodedev.com/tutorials/gamephysics/game_physics.swf]]
====== Velocity ======
// set initial angle and speed
var angle = 30;
var speed = 20;
// calcuate x, y scales (this will give the x/y equivalent of the angle)
var scale_x = cos(angle); // the x scale is the sin of the angle
var scale_y = sin(angle); // the y scale is the sin of the angle
var velocity_x = (speed * scale_x); // speed times scal_x is our x velocity
var velocity_y = (speed * scale_y); // speed times scale_y is our y velocity
// each frame call game loop to set object's position(move it along the predefined angle)
function GameLoop()
{
movingObject.x = movingObject.x + velocity_x;
movingObject.y = movingObject.y + velocity_y;
}//end game loop
======Acceleration======
// set initial angle and speed, acceleration
var angle = 30;
movingObject.speed = 20;
movingObject.acceleration = 2;
// calcuate x, y scales (this will give the x/y equivalent of the angle)
var scale_x = cos(angle); // the x scale is the sin of the angle
var scale_y = sin(angle); // the y scale is the sin of the angle
// each frame call game loop to set object's position(move it along the predefined angle)
function GameLoop()
{
// continously add acceleration to speed
movingObject.speed = movingObject.speed + movingObject.acceleration;
// speed times scale_x, scale_y is our x/y velocity
movingObject.velocity_x = (movingObject.speed * scale_x);
movingObject.velocity_y = (movingObject.speed * scale_y);
movingObject.x = movingObject.x + movingObject.velocity_x;
movingObject.y = movingObject.y + movingObject.velocity_y;
}//end game loop
======Gravity======
// set gravity constant
var gravity = 3;
var moving = true;
// each frame call game loop to set object's position
function GameLoop()
{
if( moving == true ) {
velocity_y = velocity_y - gravity;
// if object has not hit the ground add y velocity(may be a negative velocity)
if( movingObject.y - velocity_y > ground.y ) {
movingObject.y = movingObject.y + velocity_y;
}
movingObject.x = movingObject.x + velocity_x;
}
}
======Throwing/Passing======
오브젝트를 던지거나 패스하는 경우,
* x 축 속도 : 이동할 거리 / 희망 도착 시간
* x velocity is equal to the distance an object needs to travel to reach its destination divided by the amout of time you want the object to take to reach its destination.
* x velocity = distance / time \\ distance = x velocity * time \\ time = distance / x velocity
* y 축은 중력에 대해서 고려해줘야 한다.
* y velocity = time * gravity * 0.5f
코드 예제
function GameLoop()
{
if( thrown == true && moving == false ) {
moving = true;
// get distance between two guys
var distance = guy2.x - guy1.x;
// claculate x and y velocities
velocity_y = time * gravity * 0.5;
velocity_x = distance / time;
}
// ....
if( moving == true ) {
ball.x = ball.x + velocity_x;
ball.y = ball.y + velocity_y;
velocity_y = velocity_y - gravity;
}
}
======Distance & Timing======
{{:language:2d-game-physics:2d-physics-101-1.jpg|}}
======Frame Rate & Deviance======
{{:language:2d-game-physics:2d-physics-101-2.jpg|}}