Having problems with collisions

BlitzMax Forums/BlitzMax Programming/Having problems with collisions

verfum(Posted 2008) [#1]
Well, I think it's the collision, here is some code demonstrating colliding boxes or slots in this case, now the inventory type creates it's own box or slot and also the mouse cursor has it's own slot, the slot type handles the slot drawing and also collition detection, the problem is in the inventory Draw it's asking if the slots have collided and if so DrawText at the m_slot.m_x now I was assuming it would draw it at the Inventory's slot x and y but it's drawing the text on both? Which is strange considering I am using composition here, i.e. m_slot:CSlot;
Thanks for any possible help here :)
SuperStrict

Type CSlot
	Field m_x:Int, m_y:Int;
	Field m_red:Int, m_green:Int, m_blue:Int;
	Field m_mouseSelected:Int = False;
	
	Function Create:CSlot(x:Int, y:int, red:Int, green:Int, blue:Int)
		Local slot:CSlot = New CSlot;
		slot.m_x = x;
		slot.m_y = y;
		slot.m_red = red;
		slot.m_green = green;
		slot.m_blue = blue;
		object_list.AddLast(slot);
		return slot;
	End Function
	
	Method Draw()
	
		if m_mouseSelected
		
			m_x = MouseX();
			m_y = MouseY();
			
			SetColor(200, 0, 0);
			DrawRect(Self.m_x, Self.m_y, 30, 30);
			
			DrawText(Self.m_x, Self.m_x, Self.m_y+30);
			DrawText(Self.m_y, Self.m_x+30, Self.m_y+30);
		Else
			SetColor(150, 150, 150);
			DrawRect(Self.m_x, Self.m_y, 30, 30);
			
			DrawText(Self.m_x, Self.m_x, Self.m_y+30);
			DrawText(Self.m_y, Self.m_x+30, Self.m_y+30);
		endif
		
	End Method
	
	Method collide:int()
		For Local slot:CSlot = eachin object_list
			If slot <> Self Then
				If RectsOverlap(Self.m_x, Self.m_y, 30, 30, slot.m_x, slot.m_y, 30, 30)
					return True;
				End If
			End If
		Next
	End Method
	
	Method RectsOverlap:int(x0:Int, y0:Int, w0:Int, h0:Int, x2:Int, y2:Int, w2:Int, h2:Int)
		If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
		If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
		Return True
	End Method
	
End Type

Type CInventory
	Field m_x:Int, m_y:Int;
	Field m_slot:CSlot;
	
	Function Create:CInventory(x:Int, y:int)
		Local inv:CInventory = New CInventory;
		inv.m_x = x;
		inv.m_y = y;
		inv.createSlot();
		object_list.AddLast(inv);
		return inv;
	End Function
	
	Method createSlot()
		Self.m_slot = CSlot.Create(m_x, m_y, 150, 150, 150);
		Self.m_slot.m_mouseSelected = False;
		object_list.AddLast(Self.m_slot);
	End Method
	
	Method Draw()
	
		For Self.m_slot:CSlot = eachin object_list
			Self.m_slot.Draw()	
		Next

		For Self.m_slot = eachin object_list
			if Self.m_slot.collide()
					DrawText("collided", Self.m_slot.m_x, Self.m_slot.m_y-25);
			endif
		Next

	End Method
	
End Type

Type CMouse
	Field m_x:Int, m_y:Int;
	Field m_slot:CSlot;
	
	Function Create:CMouse()
		Local mouse:CMouse = New CMouse;
		mouse.createSlot();
		object_list.AddLast(mouse);
		return mouse;
	End Function
	
	Method createSlot()
		Self.m_slot = CSlot.Create(m_x, m_y, 200, 0, 0);
		Self.m_slot.m_mouseSelected = True;
		object_list.AddLast(m_slot);
	End Method
	
	Method Draw()
		
		For Self.m_slot = EachIn object_list
			Self.m_slot.Draw();
		Next

	End Method
	
End Type

Graphics(800, 600);

Global object_list:TList = New TList;

Local inventory:CInventory = CInventory.Create(50, 50);

Local mouse:CMouse = CMouse.Create();

While Not KeyHit(KEY_ESCAPE)
	Cls

	SortList(object_list);
	inventory.Draw();
	mouse.Draw();
	
	Flip
Wend
End



verfum(Posted 2008) [#2]
Oh no! It was really silly, I shouldn't of had the for loop in the Inventory Draw, the second one.
D'OH! I hate silly errors!.


verfum(Posted 2008) [#3]
Hmm, no that's still not right, it's drawing on the bloody mouse slot! and not the inventory slot?! Something is a miss.


verfum(Posted 2008) [#4]
Ah, it was ALL of the For Loops, learn something everyday! I feel like a blundering novice sometimes.


Arowx(Posted 2008) [#5]
See it's good to talk! ;o)

I find the same thing once I have to explain a problem to someone else it's sometimes easier to see the wood for the trees!

I think I'll try your approach of posting on issues, as this indie games development lark is a lonely job!