Here is another Circle to Rectangle collision example. Like the other I found this in the C++ language on a stackoverflow page. I could not get it working becourse it had a weird character in it ^ but I got help from the Monkey Community(beginners forum) Within a day the solution was posted :) It turned out to be the Pow Command in Monkey.
I was watching a game video of an old Amiga Game called Superfrog and there were swinging balls on chains in it. With the circlerectcollide function you can see if the big ball hits the ractangular area ontop of the player. Also I can use it for other games that have circles and rectangular area's interacting.
I probably will look for more collision code and convert that to monkey if I can.
Import mojo
Global cx:Int=200
Global cy:Int=200
Global cr:Int=75
Global rw:Int=30
Global rh:Int=40
Class MyGame Extends App
Method OnCreate()
SetUpdateRate(60)
End Method
Method OnUpdate()
End Method
Method OnRender()
Cls 0,0,0
SetColor 255,255,255
DrawText "Move the rectangle in the circle to see collision.",0,0
Local coll:Bool=circlerectcollide(cx,cy,cr,MouseX(),MouseY(),rw,rh)
If coll=True
DrawText "Collision",2,16
End If
DrawRect MouseX()-rw/2,MouseY()-rh/2,rw,rh
DrawCircle cx,cy,cr
End Method
End Class
Function circlerectcollide:Bool(cx:Int,cy:Int,cr:Int, rx:Int,ry:Int,rw:Int,rh:Int)
Local circledistancex = Abs(cx - rx)
Local circledistancey = Abs(cy - ry)
If (circledistancex > (rw/2 + cr)) Then Return False
If (circledistancey > (rh/2 + cr)) Then Return False
If (circledistancex <= (rw/2)) Then Return True
If (circledistancey <= (rh/2)) Then Return True
Local cornerdistancesq = Pow(circledistancex - rw / 2, 2) + Pow(circledistancey - rh / 2, 2)
Return (cornerdistancesq <= Pow(cr,2))
End Function
Function Main()
New MyGame()
End Function
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.