I'm trying to create a simple program where balls will bounce around on the screen.
The thing is sometimes the balls ricochet as they should and in the correct direction; and then sometimes they don't.
Sometimes balls go through each other.
They sometimes get stuck going in the same direction, and sometimes when two balls collide; one of the two balls would go in the opposite direction as it should, while the other one continues in the same direction as if it had never been touch.
Can you please take a look at my code and see what I'm doing wrong?
The thing is sometimes the balls ricochet as they should and in the correct direction; and then sometimes they don't.
Sometimes balls go through each other.
They sometimes get stuck going in the same direction, and sometimes when two balls collide; one of the two balls would go in the opposite direction as it should, while the other one continues in the same direction as if it had never been touch.
Can you please take a look at my code and see what I'm doing wrong?
Global graphicDepth:Int = 0 Graphics 1024, 768, graphicDepth screenTop = 0 screenLeftEdge = 0 screenBottom = GraphicsHeight() screenRightEdge = GraphicsWidth() 'mask must be set before loading of images SetBlend MASKBLEND SetMaskColor(255, 21, 192) Local ball:TBall SeedRnd MilliSecs() TBall.Create(200, 122, 0, LoadImage("images\airball.png"), "air", "", 30, 8) TBall.Create(50, 702, 0, LoadImage("images\blackball.png"), "black", "", 96, 8) TBall.Create(261, 138, 0, LoadImage("images\blueball.png"), "blue", "", 86, 8) TBall.Create(88, 456, 0, LoadImage("images\blueDarkball.png"), "bluedarkball", "", 112, 8) TBall.Create(486, 712, 0, LoadImage("images\brightgreenball.png"), "brightgreenball", "", 12, 8) TBall.Create(309, 186, 0, LoadImage("images\brownball.png"), "brownball", "", 104, 8) TBall.Create(370, 74, 0, LoadImage("images\cartoonball.png"), "cartoonball", "", 212, 8) SetClsColor 125, 58, 148 While Not KeyDown(KEY_ESCAPE) Cls For ball = EachIn TBall.ballList ball.drawballs ball.updateballs Next Flip Wend Type TBall Global ballList:TList = New TList Field xpos:Int, ypos:Int, zpos:Int, new_x:Int, new_y:Int Field imgBall:TImage Field color:String Field balltype:String Field ballxdirection:String = "right" Field ballydirection:String = "up" Field angle:Float Field speed:Float Function Create:TBall(x:Int, y:Int, z:Int, img:TImage, clr:String, typ:String, angl:Float, spd:Float) Local t:TBall = New TBall t.xpos = x t.ypos = y t.zpos = z t.imgBall = img t.color = clr t.balltype = typ t.angle = angl t.speed = spd balllist.AddLast(t) Return t End Function 'draw balls Method drawballs() DrawImage(imgBall, xpos, ypos, 0) End Method 'update balls movement Method updateballs() CheckBallCollision() xpos:+Cos(angle) * speed ypos:+Sin(angle) * speed If ypos <= 0 Or ypos >= (GraphicsHeight() - 40) Then angle = -angle EndIf If xpos <= 0 Or xpos >= (GraphicsWidth() - 40) Then angle = 180 - angle EndIf End Method Method CheckBallCollision() For Local b:TBall = EachIn ballList If ImagesCollide(b.imgBall, b.xpos, b.ypos, 0, imgball, xpos, ypos, 0) = True 'when collide with itself just continue on If b = Self Continue End If b.angle = -b.angle Exit EndIf Next End Method End Type