TGC Codebase Backup



Swinging pendulum by Ianx

12th Jan 2004 16:19
Summary

Following on from my "bouncing balls" codebase entry, here's another simulation: A swinging pendulum.



Description

This is a sample for representing a swigning pendulum in Dark Basic. The actual coding is pretty simple. As with most exercises like this, the tricky part is figuring out how to represent the physics in code. In this case we create a small cube, then add a limb for the pendulum's shaft and another limb for the weight. This means we can simply rotate the cube according to the correct formula for a pendulum and the shaft & weight will rotate with it.
The final code is fairly short but does give a good representation of a pendulum - it moves quite realistically. The parameters as set in the sample code will cause the pendulum to swing in a complete circle for a while then gradually slow down and come to rest.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: pendulum
Rem ===============================================================================
rem Simple demonstration of a swinging pendulum
Rem
rem ===============================================================================

rem -------------------------------------------------------------------------------
rem Initialisation and set up constants
sync on
sync rate 50
set ambient light 50

#Constant oSphere 10
#Constant lSphere 2
#Constant mSphere 11

#Constant oCylinder 20
#Constant lCylinder 1
#Constant mCylinder 25

#Constant oPendulum 40
rem ---------------------------------------------------------------------------------

rem ---------------------------------------------------------------------------------
rem Create the model
rem A small box is created and two limbs are added to it, one (a cylinder) representing the shaft of
rem the pendulum, the other representing the weight (a sphere)
make object box oPendulum, 1, 1, 1
make object sphere oSphere, 5, 50, 50
make object cylinder oCylinder, 1

make mesh from object mSphere, oSphere
make mesh from object mCylinder, oCylinder
delete object oSphere
delete object oCylinder

add limb oPendulum, lCylinder, mCylinder
scale limb oPendulum, lCylinder, 100, 5000, 100
offset limb oPendulum, lCylinder, 0, -25, 0
add limb oPendulum, lSphere, mSphere
offset limb oPendulum, lSphere, 0, -50, 0

position camera camera position x(), camera position y()-25, camera position z()-75
rem ---------------------------------------------------------------------------------------

rem ---------------------------------------------------------------------------------------
rem Swing the pendulum
rem zrot = the initial angle eg -90 = 90 degrees ie horizontal ("Angle")
rem zinc = the rate of angle increment. A Rate of 30 is pretty quick ("Angle Velocity")
rem damping = representation of the amount of friction ("Friction")
zrot#=-90
zinc#=30
damping#=.992

rem Simple pendulum algorith is as follows:
rem Angle = Angle + Angle Velocity
rem Angle Velocity = Angle Velocity + (sin(Angle) / length of shaft)
rem Angle Velocity = Angle Velocity * Factor for friction

while abs(zinc#)>0.00001
   zrot# = zrot# + zinc#
   zinc# = zinc# + -1*(sin(zrot#) / 1)
   zinc# = zinc# * damping#
   rotate object 40, 0, 0, zrot#
   rem damping# = damping# * 0.99999 : rem Change the damping rate slightly...makes it look a little more realistic, effectively makes the pendulum stop quicker when it's moving very slowly
   sync
endwhile
rem ---------------------------------------------------------------------------------------

exit prompt "All done", "Pendulum"