Help me fix my collision code

Monkey Forums/Monkey Beginners/Help me fix my collision code

Impmaster(Posted 2014) [#1]
Hi all. Once again, it's me, Impmaster. Now, this is hopefully the last problem I'll have to bother you guys with on this project.

I have a main character, and I have fish that swim around the ocean. Now, to do collision, what I had before was that I checked if the x and y coordinates of the player were inside the fish. However, this meant that the player could go through fish as long as he didn't touch his top left corner on them. I was thinking that I could fix this problem by setting up 4 points, one at each corner of the character, but the character rotates, so I don't know how I would do this.

Please help me and tell me how to do better collision in this engine.


You can see my project at : https://www.dropbox.com/sh/fgbijlzvmi2f3z7/bk8taWXSHZ


Shinkiro1(Posted 2014) [#2]
Without looking into your code, it seems this could help you:
http://devmag.org.za/2009/04/13/basic-collision-detection-in-2d-part-1/


therevills(Posted 2014) [#3]
What about a simple rectangle check?
Function RectsOverlap:Int(x0:Float, y0:Float, w0:Float, h0:Float, x2:Float, y2:Float, w2:Float, h2:Float)
	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



Gerry Quinn(Posted 2014) [#4]
What Shinkiro says. It sounds to me like a circle-collision test would suit you. Just test the distance between the centre of the player and the centre of each fish.

If you want to get fancy, you can take their shapes into account, but you may not need this.

If the player is moving fast relative to the fish and will go a long distance during each update, you can do several 'mini-updates' in each real update, testing for collisions at each one, But if you need this, stuff is probably moving too fast anyway.


Impmaster(Posted 2014) [#5]
Alright guys, this works for me. I had never even heard of circular collision be4 :/. I had however, used the rectangle method that therevills suggested in my last game, but i thought there was perhaps something like that written in the software already. Apparently not :/.

Thanks for the link, shinkiro.