Detect overlap of rectangle and moving and rotating texture

Hi all,

I want to detect whether a moving rectangle is inside another static rectangular area or not.

The moving rectangle is a texture displayed with Screen(‘DrawTexture’,…) and it also rotates. The static rectangle is created using Screen(‘FillRect’',…), but it could also be a texture if that would make things easier.

Initially, I thought I could detect the overlap by using ClipRect, but then I realised that of course the PsychRect of the moving texture itself is not rotated so that does not work. As I understand it, it also would not work to compute the coordinates of the rotated PsychRect and then use ClipRect with those coordinates.

I thought that would be an easy thing to solve, but I seem not to be able to figure it our. Does anybody know if there is a way to do this?

Thank you

Use inpoly, or isinpoly or something like that for the flur corner points. Can’t check right now, but a function with a name something like that should be in the psychoneliners

Thank you. So far I didn’t find anything akin to those functions mentioned, but I now saw the FramePoly/FillPoly options. So, I guess that would be the way to go when implementing the rotation and overlap detection.

I thought that in my case with uniformly coloured objects of fixed size against a black background and only one moving object, the easiest way to detect an overlap would be to count the number of black or non-black pixels after each flip, but as far as I can see the only way to get to these are the (slow?) GetImage or CopyImage options.

I saw the ‘inpolygon’ function in MATLAB, but since Screen(‘DrawTexture’,…) does not return the rotated coordinates of the rect that means I would have to compute the rotated coordinates in addition separately to provide them to the inpolygon function.

The simplest way is to calculate the coordinates of the corners of the two rects. Polygons are more complex. Your situation is simple.

Getting images pixels would be a terribly inefficient way to go. I am sure it would not work at all for what you are wanting.

I have lots of demos on my website, here: Scarfe Lab: PTB

The most relevant to your needs looks to be: Scarfe Lab: Mouse In Square

This tracks one coordinate, the mouse position, and sees if it is inside of a rect. You could build on this by checking the four corners of your texture rect.

Thats my best guess without looking into it in more detail.

If it is proving difficult let me know. There is scope that I might be able to make a demo for the website for this. But my time is rather tight at the moment and I do all the demos for free in my spare time to help the Psychoolbox folk. So it would not be an immediate thing.

Peter

Thank you, Peter. Yes, I ended up doing what you suggested. I rotated the rectangle myself and then used inpolygon.m to detect the overlap. The display of the rectangle is still handled by Screen(‘DrawTexture’,…). With the simple rectangle that I’m currently using, ‘FillPoly’ would work as well, but I might want to show more involved stuff in the future. Therefore, I was wondering whether it was possible to somehow access the coordinates computed by Screen(‘DrawTexture’), but I guess this is not possible. My pixel based idea might be terrible (especially because it would always be one ‘Flip’ behind) but I thought it would have the advantage that it would also work for non-rectangular shapes (e.g., ellipses).

Martin

Screen() doesn’t provide this info for drawn primitives. For simple geometry like yours, doing the 2D rotation and intersection in Matlab is the way most simple and efficient solution.

Very complex shapes and objects, various OpenGL based techniques can be more efficient. E.g., MorphDemo.m with the dotson parameter 3 or 4 for reading back projected vertex positions of complex gemetry, DotDemoStencil.m for bits about graphics based area intersection, but nothing ready made for this.