TGC Codebase Backup



Gravity Balls by Anonymous Coder

21st Jul 2006 18:30
Summary

Control balls with mouse pointer as gravity source.



Description

Control balls with mouse pointer as gravity source.
Thanks to NewGuy for his very helpful piece of code in "Strong Gravity."



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem ===INITIALISATION=====================================
ScreenWidth=1024
ScreenHeight=768
ScreenDepth=32
RefreshRate=60

BorderOn=1
NumBalls=5
BallSize=10
BallsSpeed#=0.5
Gravity#=200 `Gravity# = (GravConst#*Mass1#*Mass2#)/Dist#^2


sync on
sync rate RefreshRate
`hide mouse
set display mode ScreenWidth,ScreenHeight,ScreenDepth

rem Random Ball Starting Positions
DIM xPosMem#(NumBalls)
for xPos=1 to NumBalls
xPosMem#(xPos)=rnd(ScreenWidth-(2*BallSize))+BallSize
next xPos

DIM yPosMem#(NumBalls)
for yPos=1 to NumBalls
yPosMem#(yPos)=rnd(ScreenHeight-(2*BallSize))+BallSize
next yPos

rem Random Ball Starting Velocities
DIM xVelMem#(NumBalls)
for xVel=1 to NumBalls
xVelMem#(xVel)=rnd(1000)
next xVel

DIM yVelMem#(NumBalls)
for yVel=1 to NumBalls
yVelMem#(yVel)=rnd(1000)
next yVel

rem Creates Random Pos/Neg Values
DIM RevMix#(NumBalls)
for Mixer=1 to NumBalls
PN=rnd(1) : if PN= 1 then RevMix#(Mixer)=1 else RevMix#(Mixer)=-1
next Mixer


do
rem ===BALLS======================================
FOR Loopy1=1 TO NumBalls
rem Update Ball Positions
xPosMem#(Loopy1)=xPosMem#(Loopy1)+(((xVelMem#(Loopy1))/(1000/BallsSpeed#))*RevMix#(Loopy1))
yPosMem#(Loopy1)=yPosMem#(Loopy1)+(((yVelMem#(Loopy1))/(1000/BallsSpeed#))*RevMix#(Loopy1))

rem Draw Balls
ink RGB(255,255,255),0
circle xPosMem#(Loopy1),yPosMem#(Loopy1),BallSize

rem Collisions with Screen
if xPosMem#(Loopy1)>(ScreenWidth-(BallSize)) or xPosMem#(Loopy1)<(BallSize) then (xVelMem#(Loopy1))=-(xVelMem#(Loopy1))
if yPosMem#(Loopy1)>(ScreenHeight-(BallSize)) or yPosMem#(Loopy1)<(Ballsize) then (yVelMem#(Loopy1))=-(yVelMem#(Loopy1))
NEXT Loopy1

rem ===LINE BORDER====================================
if BorderOn=1
ink RGB(255,255,255),0
line 0,0,Screenwidth,0
line 0,0,0,ScreenHeight
line ScreenWidth-1,0,ScreenWidth-1,ScreenHeight-1
line 0,ScreenHeight-1,Screenwidth-1,ScreenHeight-1
endif

rem ===GRAVITY==============================================
FOR Loopy2=1 TO NumBalls
if mouseclick()=2 then GravOn=1 else GravOn=0

Dist#= sqrt( (mousex()-xPosMem#(Loopy2))^2 + (mousey()-yPosMem#(Loopy2))^2 )

X# = newXvalue(0,atanfull(xPosMem#(Loopy2)-mousex(),yPosMem#(Loopy2)-mousey())+180,Gravity#)
Y# = newZvalue(0,atanfull(xPosMem#(Loopy2)-mousex(),yPosMem#(Loopy2)-mousey())+180,Gravity#)

if GravOn=1 then (xVelMem#(Loopy2))=(xVelMem#(Loopy2))+(X#)*RevMix#(Loopy2)
if GravOn=1 then (yVelMem#(Loopy2))=(yVelMem#(Loopy2))+(Y#)*RevMix#(Loopy2)

`if xVelMem#(Loopy2)> 2 then xVelMem#(Loopy2)=2
`if yVelMem#(Loopy2)> 2 then yVelMem#(Loopy2)=2

NEXT Loopy2

sync
cls
loop