ImageRectCollide and MoveMouse()

Monkey Forums/Monkey Programming/ImageRectCollide and MoveMouse()

rIKmAN(Posted 2013) [#1]
Just going through my folders of old Blitz code and have come across some stuff that I have tried to convert to Monkey.

However, the code uses MoveMouse() and ImageRectCollide(), which obviously are not in Monkey.

I know I can use a simple Rect->Rect check for simple collision, but aside from the Frameworks is there still no implementation of proper image based collisions in Monkey?

Also I realise MoveMouse() is not needed on iOS/Android etc, but is there any workaround or function I could use in its place for Windows?


Nobuyuki(Posted 2013) [#2]
To determine if the mouse moved:

[monkeycode]
Field MouseMoved:Bool, LastMouseX:Float, LastMouseY:Float

Method OnUpdate:Int()
MouseMoved = False
If MouseX() <> LastMouseX Or MouseY() <> LastMouseY Then MouseMoved = True
LastMouseX = MouseX(); LastMouseY = MouseY()

Return 0
End Method
[/monkeycode]

To determine if rects overlap:

[monkeycode]
Class rect
int x
int y
int width
int height

Method New(x:Float, y:Float, width:Float, height:Float)
Self.x = x; Self.y = y; Self.width = width; Self.height = height
End Method
End Class

Function valueInRange:Bool(value:Float, min:Float, max:Float)
Return (value >= min) And (value <= max)
End Function

Function rectsOverlap:Bool(rect A, rect B)
Local xOverlap:Bool = valueInRange(A.x, B.x, B.x + B.width) Or
valueInRange(B.x, A.x, A.x + A.width)

Local yOverlap:Bool = valueInRange(A.y, B.y, B.y + B.height) Or
valueInRange(B.y, A.y, A.y + B.height)

Return xOverlap And yOverlap
End Function
[/monkeycode]


There is no function for checking images mask for overlap or collision in Mojo. However, you can use this bounding box function to serve as a first-pass for writing your own using Monkey's pixel methods. However, I'd recommend instead considering a system where you create and handle your own masks manually and import them into arrays, if faster pixel-perfect precision is what you're looking for. In many cases, though, a bounding box or series of them (or a circle) does suffice. There are a number of overlap functions for various shapes on this forum, so consider what you're looking for in your game and proceed accordingly.

edit: jeez, what happened to pretty syntax highlighting?


rIKmAN(Posted 2013) [#3]
Hey Noboyuki, thanks a lot for the reply.

I forgot to update this thread, but I already coded workarounds for these and a few others a couple of hours after posting, but thanks you for taking the time to help. :)