I'm working on some code just now, that requires, a Ball or Circle i should say, to collide off of a few different shapes.
Circle - Circle
Circle - Tri
Circle - Squ
Circle - Star
Circle - flatwall or line.
I am currently working on the following code.
Space Bar, will add another circle, at the moment, I seem to be getting a change in velocity(x,y) from some collisions and I dont want that, I need for the collision to only change the vector the circle is moving along and not its speed.
I also need as stated above to work out how to best detect a collision vs a square, and have the circle reflect at the correct angle, but im not sure where to start with that, not to mention a star lol..
Anyway . thanks in advance for any help.
Circle - Circle
Circle - Tri
Circle - Squ
Circle - Star
Circle - flatwall or line.
I am currently working on the following code.
Graphics3D 500,500,16,2 SetBuffer BackBuffer() Cls SeedRnd MilliSecs() Type Sphere Field x# Field y# Field angle# Field velx# Field vely# Field size% Field id% Field hit Field velocity# Field Colltimer% End Type Global Ball.sphere = New sphere : Delete ball Global ballcounter% For looper=1 To 2 create() Next Repeat Cls update() render() Flip Until KeyHit(1) Function Create() ballcounter=ballcounter+1 Ball.sphere = New sphere Ball\x=Rand(100,400) Ball\y=Rand(100,400) Ball\angle=Rand(-180,180) Ball\size = 30 ball\velx = Sin(ball\angle) ball\vely = Cos(ball\angle) ball\id = ballcounter ball\hit=False ball\colltimer=0 ball\velocity#=3 End Function Function update() If KeyHit(57) Then create() For Ball.sphere = Each sphere If ball\colltimer>0 Then ball\colltimer=ball\colltimer-1 ball\x=ball\x+ball\velx*ball\velocity ball\y=ball\y+ball\vely*ball\velocity If ball\x<0 Or ball\x>500-(ball\size) Then ball\velx=-ball\velx If ball\y<0 Or ball\y>500-(ball\size) Then ball\vely=-ball\vely ;check for collisions. For ball2.sphere = Each sphere If ball2<>ball ;do checks here CollisionDistance# = (ball\size+ball2\size)/2 Local xdist#,ydist#,dist# xdist#=Abs(ball2\x-ball\x) ydist#=Abs(ball2\y-ball\y) distance = Sqr(xdist*xdist+ydist*ydist) DebugLog distance ;Collided or not? If Distance < collisionDistance Then ;Get the Angle from Ball 1 to Ball 2 Normal_Angle#=ATan2(ball2\y-ball\y, ball2\x-ball\x) ;Work out, How much they are overlapping. moveback1#=(collisionDistance-Distance) moveback2#=(collisionDistance-Distance) ;Move Bals Back along the normal Angle by the amount they overlap. ball\x=ball\x + moveback1*Cos(Normal_Angle+180) ball\y=ball\y + moveback1*Sin(Normal_Angle+180) ball2\x=ball2\x + moveback2*Cos(Normal_Angle+180) ball2\y=ball2\y + moveback2*Sin(Normal_Angle+180) ;we now need to bounce them away .. reflection. nX#=Cos(Normal_Angle) nY#=Sin(Normal_Angle) ball\velx = ball\velx - nX ball\vely = ball\vely - ny ball2\velx = ball2\velx + nX ball2\vely = ball2\vely + ny End If End If Next Next End Function Function render() For Ball.sphere = Each sphere Text ball\x,ball\y-5,Str(ball\id),1,1 If ball\hit=True Color 255,0,0 Oval(Ball\x,Ball\y,Ball\size,Ball\size,1) ball\hit=False Else Color 0,255,0 Oval(Ball\x,Ball\y,Ball\size,Ball\size,1) End If Next End Function
Space Bar, will add another circle, at the moment, I seem to be getting a change in velocity(x,y) from some collisions and I dont want that, I need for the collision to only change the vector the circle is moving along and not its speed.
I also need as stated above to work out how to best detect a collision vs a square, and have the circle reflect at the correct angle, but im not sure where to start with that, not to mention a star lol..
Anyway . thanks in advance for any help.