Collision detection with rotated rectangles?

BlitzMax Forums/BlitzMax Beginners Area/Collision detection with rotated rectangles?

ReconditePhreak(Posted 2011) [#1]
I've been reading over the following collision detection tutorial http://www.2dgamecreators.com/tutorials/gameprogramming/collision/T1%20Collision2.html.

One of the things I'm still unsure about is how to use the built in collision detection system to detect the collision of rotated rectangles (as an example).

Someone correct me if I'm wrong, but from what I've seen there's a global transform that's tracked underneath, so when something is drawn to the screen, said transform is applied. In this way, you can call SetRotation, and then everything you draw will be rotated by 30 degrees (as an example).

But since it's transient (the rotation can be reset to something else immediately after it's drawn), how do you get the built in colliderect, and family, to detect that properly? I may draw two different rectangles using two different transforms, but I would still want their collision to be detected properly.

Also:

- is there a coordinate type defined in the standard library? I'm not seeing it.

- Does blitzmax have a way to use do drawing operations to interim buffers rather than directly to the main graphics buffer?

Last edited 2011


Zeke(Posted 2011) [#2]
SuperStrict 

Graphics 800,600

AutoMidHandle True

Global staticrot:Float=0
Global mouserot:Float=0

Global img_static:TImage=CreateImage(128,128)
Global img_mouse:TImage=CreateImage(128,128)

Global m_scale:Float=1.5

DrawRect 0,0,128,128
GrabImage(img_static,0,0)
GrabImage(img_mouse,0,0)

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	Cls
	Local mx:Int=MouseX()
	Local my:Int=MouseY()
	
	SetRotation staticrot
	SetScale 1,1
	SetColor 255,255,255
	DrawImage img_static,300,300

	SetRotation mouserot
	SetScale m_scale,m_scale
	SetColor 0,200,0
	
	If ImagesCollide2(img_static,300,300,0,staticrot,1,1,img_mouse,mx,my,0,mouserot,m_scale,m_scale) Then
		SetColor 255,0,0
	EndIf
	
	DrawImage img_mouse, mx,my

	Flip

	staticrot:+1 Mod 360
	mouserot:+2 Mod 360
Wend



ReconditePhreak(Posted 2011) [#3]
ok, so what I've gathered from that example is that the only way to arbitrarily be able to do collision detection on rectangles using the built in API is to draw it to the back buffer somewhere, copy that into an image buffer, and then see if those images collide.

One thing to note, I tested and it doesn't look like GrabImage respects the current transformations.

One other question, is there a way to rotate about a specific point rather than the origin of the image?

Last edited 2011


matibee(Posted 2011) [#4]
I have to ask.. do you *really*, **really**, need to do collision detection against rotated rectangles?...

Have you tried, (and have your beta testers) played your game whilst using circle vs circle.

There are very few extreme circumstances where more than that is required.


ReconditePhreak(Posted 2011) [#5]
This is not directly applicable to anything I'm doing, I learn by poking and prodding.