In 2D games there are two position points that are used to place your sprite or drawing on the screen. They are known as the x axis and the y axis. In this example we will call them
object.x ' x axis
object.y ' y axis
We will use two variables called velocity_x and velocity_y to move the object on the screen.
velocity.x ' x axis movement increment
velocity.y ' y axis movement increment
We must be able to determine the direction and speed of the object moving on the screen. To do this we will use two variables called angle and speed.
angle ' direction of object in degrees (0-360)
speed ' The speed at witch the object moves
Using the Liberty BASIC commands cos() and sin() we will convert our angle value to give us another value that we will call the scale_x and scale_y.
scale.x ' cos() of angle
scale.y ' sin() of angle
Since angle values can not be used directly with cos() and sin() we must first convert the angle to a radian value. We do this like so.
angle=angle/57.29577951
This gives us our radian value witch we can use with cos() and sin() to determine our scale values. The scale values will move the object in the direction we have chosen using our angle value.
scale.x=cos(angle)
scale.y=sin(angle)
Once we have our scale values we can now combine them with our speed value to get our velocity values
velocity.x=speed*scale.x
velocity.y=speed*scale.y
Remember that with each new direction the velocity values must be recalculated before moving the object.
Our code so far.
'2D Game Physics - Part Iobject.x=400object.y=300
angle=45
r.angle=angle/57.29577951
speed=5
scale.x=cos(r.angle)
scale.y=sin(r.angle)
velocity.x=speed*scale.x
velocity.y=speed*scale.y
Drawing the object
Now we will draw our object on the screen. For this example I will use a simple line to represent our object. By using the velocity values I can determine the line position and length. Below is an updated version of the code above.
'2D Game Physics - Part Iobject.x=400object.y=300
angle=45
r.angle=angle/57.29577951
speed=5
scale.x=cos(r.angle)
scale.y=sin(r.angle)
velocity.x=speed*scale.x
velocity.y=speed*scale.y
nomainwin
WindowWidth=800
WindowHeight=600
open "2D Game Physics - Part I"for graphics as #main
#main "trapclose [quit]"
#main "color red;size 5;down"
#main "line ";object.x;" ";object.y;" ";object.x+(velocity.x*2);" ";object.y-(velocity.y*2)
wait
[quit]
close #main
end
Moving the object
We will now move our object on the screen in the direction we have chosen (45 degrees) until it reaches the edge of our window.
'2D Game Physics - Part Iobject.x=400object.y=300
angle=45
r.angle=angle/57.29577951
speed=5
scale.x=cos(r.angle)
scale.y=sin(r.angle)
velocity.x=speed*scale.x
velocity.y=speed*scale.y
nomainwin
WindowWidth=800
WindowHeight=600
open "2D Game Physics - Part I"for graphics_nsb as #main
#main "trapclose [quit]"
#main "color red;size 5;down"
#main "line ";object.x;" ";object.y;" ";object.x+(velocity.x*2);" ";object.y-(velocity.y*2)
timer 100,[move]
wait
[move]object.x=object.x+velocity.x'increase object.x valueobject.y=object.y-velocity.y'increase object.y value'check for edge of windowifobject.y<10thenobject.y=10ifobject.y>WindowHeight-20thenobject.y=WindowHeight-20ifobject.x>WindowWidth-15thenobject.x=WindowWidth-15ifobject.x<10thenobject.x=10'draw object
#main "cls"
#main "line ";object.x;" ";object.y;" ";object.x+(velocity.x*2);" ";object.y-(velocity.y*2)
wait
[quit]
close #main
end
Controlling the object
Let's now add some basic movement control to our object. In this example we will use the arrow keys on your keyboard to change the angle of the object witch will change it's direction. I have also created a sub called setangle witch can be used to change the velocity values for each new direction we want to move in.
'2D Game Physics - Part Iobject.x=400object.y=300
angle=45
speed=10call setangle velocity.x,velocity.y,angle,speed
nomainwin
WindowWidth=800
WindowHeight=600
open "2D Game Physics - Part I"for graphics_nsb as #main
#main "trapclose [quit]"
#main "when characterInput [getkey]"
#main "color red;size 5;down"
#main "line ";object.x;" ";object.y;" ";object.x+(velocity.x*2);" ";object.y-(velocity.y*2)
timer 100,[move]
wait
[getkey]
key$ = Inkey$
if len(key$)<2thenelseifright$(key$,1)=chr$(_VK_UP)thenif moving=0then
moving=1else
moving=0endifendififright$(key$,1)=chr$(_VK_RIGHT)then
angle=angle-5if angle<0then angle=360call setangle velocity.x,velocity.y,angle,speed
endififright$(key$,1)=chr$(_VK_LEFT)then
angle=angle+5if angle>360then angle=0call setangle velocity.x,velocity.y,angle,speed
endifendif
wait
[move]
#main "setfocus"
scan
if moving=1thenobject.x=object.x+velocity.x'increase object.x valueobject.y=object.y-velocity.y'increase object.y value'check for edge of windowifobject.y<10thenobject.y=10ifobject.y>WindowHeight-40thenobject.y=WindowHeight-40ifobject.x>WindowWidth-15thenobject.x=WindowWidth-15ifobject.x<10thenobject.x=10endif'draw object
#main "cls"
#main "line ";object.x;" ";object.y;" ";object.x+(velocity.x*2);" ";object.y-(velocity.y*2)
wait
[quit]
close #main
endsub setangle byref velocity.x, byref velocity.y,angle,speed
r.angle=angle/57.29577951
scale.x=cos(r.angle)
scale.y=sin(r.angle)
velocity.x=speed*scale.x
velocity.y=speed*scale.yendsub
I hope you find this example helpful in your game programming and I hope to add more parts as I finish them.
2D Game Physics - Part I
Ben Jimenez-2010Table of Contents
Angle & Velocity
You can easily add basic physics to your games using these examples below. Original examples found at http://www.rodedev.com/tutorials/gamephysics/ .In 2D games there are two position points that are used to place your sprite or drawing on the screen. They are known as the x axis and the y axis. In this example we will call them
object.x ' x axis
object.y ' y axis
We will use two variables called velocity_x and velocity_y to move the object on the screen.
velocity.x ' x axis movement increment
velocity.y ' y axis movement increment
We must be able to determine the direction and speed of the object moving on the screen. To do this we will use two variables called angle and speed.
angle ' direction of object in degrees (0-360)
speed ' The speed at witch the object moves
Using the Liberty BASIC commands cos() and sin() we will convert our angle value to give us another value that we will call the scale_x and scale_y.
scale.x ' cos() of angle
scale.y ' sin() of angle
Since angle values can not be used directly with cos() and sin() we must first convert the angle to a radian value. We do this like so.
angle=angle/57.29577951
This gives us our radian value witch we can use with cos() and sin() to determine our scale values. The scale values will move the object in the direction we have chosen using our angle value.
scale.x=cos(angle)
scale.y=sin(angle)
Once we have our scale values we can now combine them with our speed value to get our velocity values
velocity.x=speed*scale.x
velocity.y=speed*scale.y
Remember that with each new direction the velocity values must be recalculated before moving the object.
Our code so far.
Drawing the object
Now we will draw our object on the screen. For this example I will use a simple line to represent our object. By using the velocity values I can determine the line position and length. Below is an updated version of the code above.Moving the object
We will now move our object on the screen in the direction we have chosen (45 degrees) until it reaches the edge of our window.Controlling the object
Let's now add some basic movement control to our object. In this example we will use the arrow keys on your keyboard to change the angle of the object witch will change it's direction. I have also created a sub called setangle witch can be used to change the velocity values for each new direction we want to move in.I hope you find this example helpful in your game programming and I hope to add more parts as I finish them.