General information
Category:
Coding, syntax & commands
Title:
Detecting object collisions
Hi
I'm creating an app where the user can move shape objects but I want the moved object 'detect' other object's borders so the movement is restricted to the free available area. The code belows is from an example from Gérald Santerre here in the downloads section.
It's working ok, but how can I detect collisions between objects?
Any ideas?
Thanks in advance
Héctor L.
**************************************************
DEFINE CLASS _mover AS shape
Height = 6
Width = 100
MousePointer = 5
Name = "_mover"
PROCEDURE RightClick
LOCAL oParent
oParent = This.Parent
DEFINE POPUP sizing SHORTCUT FROM mrow(),mcol()
DEFINE BAR 1 OF sizing PROMPT "Max Width"
DEFINE BAR 2 OF sizing PROMPT "Max Height"
DEFINE BAR 3 OF sizing PROMPT "Bring to Front"
DEFINE BAR 4 OF sizing PROMPT "Send to Back"
ON SELECTION BAR 3 OF sizing =oParent.ZOrder(0)
ON SELECTION BAR 4 OF sizing =oParent.ZOrder(1)
ACTIVATE POPUP sizing
RELEASE POPUP sizing
ENDPROC
PROCEDURE MouseMove
LPARAMETERS nButton, nShift, nXCoord, nYCoord
LOCAL nFreeZone, nLeft, nTop, oRef
nFreeZone = This.Parent.nFreeZone
IF This.Parent.Mover_flag = .T. AND This.MousePointer = 5
nLeft = (This.Parent.OldLeft - (This.Parent.OldX - nXCoord))
nTop = (This.Parent.OldTop - (This.Parent.OldY - nYCoord))
*set the Left position
IF nLeft < Thisform.Width - This.Parent.Width - nFreeZone
IF nLeft > nFreeZone
This.Parent.Left = nLeft
ELSE
This.Parent.Left = nFreeZone
ENDIF
ELSE
This.Parent.Left = Thisform.Width - This.Parent.Width - nFreeZone
ENDIF
*set the top position
IF nTop < Thisform.Height - This.Parent.Height - nFreeZone
IF nTop > nFreeZone
This.Parent.Top = nTop
ELSE
This.Parent.Top = nFreeZone
ENDIF
ELSE
This.Parent.Top = Thisform.Height - This.Parent.Height- nFreeZone
ENDIF
ENDIF
ENDPROC
PROCEDURE MouseLeave
LPARAMETERS nButton, nShift, nXCoord, nYCoord
This.Parent.Mover_flag = .F.
ENDPROC
PROCEDURE MouseUp
LPARAMETERS nButton, nShift, nXCoord, nYCoord
This.Parent.Mover_flag = .F.
ENDPROC
PROCEDURE Init
This.Top = 0
This.Left = 0
This.Width = This.Parent.Width
This.Anchor = 10
ENDPROC
PROCEDURE MouseDown
LPARAMETERS nButton, nShift, nXCoord, nYCoord
This.Parent.OldX = nXCoord
This.Parent.OldY = nYCoord
This.Parent.OldLeft = This.Parent.Left
This.Parent.OldTop = This.Parent.Top
This.Parent.Mover_flag = .T.
This.Parent.edtEditArea.SetFocus()
ENDPROC
ENDDEFINE
*
*-- EndDefine: _mover
**************************************************
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only