Checking if a point is inside a rotating view

Z

Ziphold

Guest
Hello again!
I'm currently implementing culling in my game and everything works fine until I start rotating the view.
This is the simple portion of code that controls it.
Code:
lastPointInView = point_in_rectangle(lastPointX,lastPointY,view_xview,view_yview,view_xview+view_wview,view_yview+view_hview);    
if !lastPointInView {
    lastPointIndex ++;
    pathAddRandomPoint();
}
Now, because the view is rotating, that wouldn't always work because view_wview and view_hview are always the same, independent of the room view. So sometimes, the code would take effect even when the point is in view, and sometimes it wouldn't when the point is out of it. (Which is opposite of what I want)

I would be thankful if anyone posted their idea of how I could solve this.
In advance, thank you.
 
J

jaydee

Guest
Apply the rotation transform to the lastPoint vector before doing your point_in_rectangle test.

Code:
// Assuming here that the origin of your rotation is the centre of the view:
var OriginX = view_xview + view_wview / 2;
var OriginY = view_yview + view_hview / 2;

// Find position of LastPoint relative to Origin
var OffsetX = lastPointX - OriginX;
var OffsetY = lastPointY - OriginY;

// Apply rotation
OffsetX *= cosd(view_angle);
OffsetY *= sind(view_angle);

// Transform back into world coordinates
lastPointX = OriginX + OffsetX;
lastPointY = OriginY + OffsetY;
Could be condensed or made into a script, but I wanted each step to be clear. Might need to double check the math, but I think it should all be right.

*Note* Whether this would work or not depends on what exactly lastPoint is! In the case this doesn't work, there are two things you should try:

1) In my code, replace view_angle with -view_angle.
2) Instead of applying the rotation transform to your x and y boundaries instead of lastPoint.

If after all 3 this isn't working, I'll need more information to help you.
 
Last edited by a moderator:

NightFrost

Member
Shouldn't the rotation be
Code:
OffsetX = OffsetX * dcos(view_angle) - OffsetY * dsin(view_angle);
OffsetY = OffsetY * dcos(view_angle) + OffsetX * dsin(view_angle);
 
W

Wraithious

Guest
You could use point_in_circle(,,,,,,) with the radius being 1/2 the width of the narrowest side of the view, OR if you need the whole rectangle to be considered an easy way to do it would be to have an object with a blank sprite the size of your view, with the not precise box checked in the sprite editor, mimic your view's x,y, origin and image angle and just check for a collision

a third option is to use point_in_triangle(,,,,,,,) twice making sure each triangle is of the right triangle type to form a rectangle, and then use length_direction(,,,) and point_direction(,,,) to update the points of the triangles to match the view's rotation.
 
Last edited by a moderator:
Z

Ziphold

Guest
Sorry for the late answer, guys. Anyways, I used each method and found out there is more to it than what I expected. So instead of deactivating points outside the view, I constantly deactivate 20th point in the list (which is always outside the view).
I'm very thankful for the answers, however!
 
Top