benjamin805
Jun 29, 2010
=2D Game Physics - Part I= //Ben Jimenez-2010// [[toc]] ---- ==2D Game Physics== You can easily add basic physics to your games using these examples below. Original examples found at [[@http://www.rodedev.com/tutorials/gamephysics/]] . The examples there are very good,but can confuse you. I will try to give examples that I think are easier to understand. 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 [[http://en.wikipedia.org/wiki/Cos%28x%29|cos()]] and [[http://en.wikipedia.org/wiki/Sin%28x%29|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 [[http://en.wikipedia.org/wiki/Radian|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. [[code format="vbnet"]] '2D Game Physics - Part I object.x=400 object.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 [[code]] ==Moving the object== Now we will create our game loop that we will use to actually move the object on the screen. There are several ways to do this and you must decide how far you want the object to move in any given direction. For example if you are driving a car you should be able to move until you hit something or if you are throwing a ball it may only go for a short distance.For this example we will allow the object to move until it reaches the edge of our graphic window. Below is an updated version of the code above. [[code format="vbnet"]] '2D Game Physics - Part I object.x=400 object.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 [[code]] ---- ---- ---- ---- [[code format="vbnet"]] [[code]] ==Third Part Title== Text here. [[code format="vbnet"]] 'code here [[code]]