Elastic turning!
Blitz3D Forums/Blitz3D Programming/Elastic turning!
| ||
I have a 2D `ship` similar to that in the game Asteroids. I can move the ship at any given `angle` at a variable `velocity`by using the formula:- deltax# = velocity *cos(angle) deltay# = velocity *sin(angle) The velocity is calculated with acceleration and friction so is not a constant velocity. As in Asteroids I want to turn the ship so there is an `elastic` turning effect ( like a handbrake turn) Any suggestions please? Many thanks. |
| ||
***EDIT***** Sorry, just noticed that you specified 2d... Ignore the post. Have you tried AlignToVector with the rate# parameter set to >=0 and <1 ? |
| ||
The elastic effect only happened if you were holding down the thrust on the original asteroids, correct? Otherwise it just spun? |
| ||
Non working code but something like this? Note that the maximum speed is implied .. = Acceleration / ( 1.0 - Drag ), which is 5.0 with my settings. You may want to play about with this.;initialise Global Acceleration# = .05 Global Drag# = .99 Global TurnSpeed# = .25 Global VelocityX# = 0 Global VelocityY# = 0 Global PositionX# = 0 Global PositionY# = 0 Global Angle# = 0 ;define keys Const KEYleft = 203 Const KEYright = 205 Const KEYthrust = 200 ;main loop While Not KeyDown(1) ;turning Angle = Angle + ( KeyDown( KEYleft ) - KeyDown( KEYright ) ) * TurnSpeed ;moving Thrust# = KeyDown( KEYthrust ) * Acceleration VelocityX = VelocityX * Drag + Thrust * Cos( Angle ) VelocityY = VelocityY * Drag + Thrust * Sin( Angle ) PositionX = PositionX + VelocityX PositionY = PositionY + VelocityY ; ;DRAW SHIP @ pos x,y Wend |
| ||
In my view you need to seperate acceleration and inertia. So you'll have two bodies in a hierarchy. In 3D this would be easy, parent the ship to a pivot. the ship has the user controls, and the pivot is moved in space. then transfer the acceleration of the ship to the pivot in a delayed way, as well as the rotation (AlignToVector, as suggested). Now you can control the amount of inertia by the speed of the forces equalization. |