Collision help, please
BlitzMax Forums/BlitzMax Beginners Area/Collision help, please
| ||
I wonder if someone would be so kind as to tell me what code to add to this small program, so I can delete the rectangle I hit with my Right_Mouse_Button.
SetGraphicsDriver GLMax2DDriver()
Global number_of_Drects = 10
Type rect
Field x#,y#,size#
Field rojo#,verde#,azul#
'Global rectnumber=0
Global dadas:TList
Method draw()
SetColor(rojo,verde,azul)
DrawRect x,y,size,size
End Method
Method move()
x=x+(size/8)
If x > 800
x=0
EndIf
End Method
Method Compare(other:Object)
If rect(other).size < size Return -1
Return 1
End Method
Function create(cm)
If dadas = Null dadas = CreateList()
socas:rect = New rect
If cm=1
socas.x = MouseX() ; socas.y = MouseY()
Else
socas.x = Rand(5,795) ; socas.y = Rand(5,595)
EndIf
socas.size = Rand(4,20)
socas.rojo = Rand(80,255) ; socas.verde = Rand(80,255) ; socas.azul = Rand(80,255)
dadas.addlast(socas)
End Function
End Type
For n = 1 To number_of_Drects
rect.create(0)
Next
SortList(rect.dadas,False)
'ClearList(rect.dadas)
a=CountList(rect.dadas)
Graphics 800,600
Repeat
Cls
For t:rect = EachIn rect.dadas
t.draw
t.move
Next
DrawText a,20,80
Flip
If MouseHit(1)
rect.create(1)
SortList(rect.dadas,False)
a=CountList(rect.dadas)
EndIf
If KeyHit(key_space)
ClearList(rect.dadas)
a=CountList(rect.dadas)
EndIf
Until KeyHit(key_escape) Or AppTerminate()
Excuse my ugly coding, but I'm trying to understand Bmax. |
| ||
| Hi Madmax, What you could do is hide the mouse pointer and draw your own mouse pointer then check for collisions between it and your flying squares. (Also you could choose not to hide the mouse pointer and draw a 1*1 pixel image at mouse coordinates then again check for collisions). |
| ||
| Thanks for your answer Benyboy, another thing how do I get the colour from a pixel on the screen (opposite of plot)? |
| ||
| I don't know how to get the color from a pixel, but what you could do maybe is have less different colors at creation then keep track of them in a field and test with a condition if it matches the color you want ? |
| ||
First of all, if you're testing collisions only with rectangles, you don't need to plot anything extra on the screen. Just check the MouseX() and MouseY() coordinates with the bounding points of the rectangle.
'Function to see if a given point is within any rectangles
'MX and MY are the mouse x and y coordinates
Function CheckRectCollision:rect(MX:int, MY:int)
If rect.dadas
For Local CurrentRect:rect = EachIn rect.dadas
If MX > CurrentRect.x and MX < CurrentRect.x + CurrentRect.size ..
and MY > CurrentRect.y and MY < CurrentRect.y + CurrentRect.size
Return CurrentRect
End If
Next
End If
Return Null
End Function
'You could call this function like this
Local TestRect:rect = CheckRectCollision(Mousex(),MouseY())
If TestRect Then ProcessRect(TestRect) 'Some function to deal with the rectangle collided with
As for getting the color of a point on screen, you need to first transfer the part of the screen you wish to test to a pixmap, and test that. Color:Int = ReadPixel(GrabPixmap(X,Y,1,1),0,0) Blue = Color & $FF Color = Color shr 8 Green = Color & $FF Color = Color shr 8 Red = Color & $FF Color = Color shr 8 Alpha = Color & $FF |
| ||
| Thanks to all for your answers. |