How do I get the "depth = -y" effect in a rotatable view?

Baldi

Member
This is probably basic trigonometry but I have tried and failed. I would like to know "how far up" a point is in the view, even though "up" is no longer -y, since the view may be rotated.
Thank you for any help or resources, unfortunately the ones I found did not help me since I did not understand the solution or how to implement it in GML. Here is one example, in case it helps explain the question.

Thanks!
 

Nidoking

Member
It sounds like what you've got is a bunch of instances in the standard coordinate system, and you want to rotate only the view while keeping the same coordinate system, and you want to know just the y value of each instance in the rotated view. If that's correct, then while I'm sure there's a really simple way to do this with matrices (and I implore someone who knows them better than I do to respond with it), I think we can handle this with trigonometry.

First, I assume that the origin that the view rotates around is not at 0,0. So let's shift everything so that it is. Assuming that you're calling this from each instance, and if not, replace the variables accordingly:
GML:
var newX = x - origin_x;
var newY = y - origin_y;
Now everything is centered with the point of view rotation at the origin. Next, we need to convert this point to polar coordinates. You can use trig for that, or GML functions:
GML:
var r = point_distance(0, 0, newX, newY);
var theta = point_direction(0, 0, newX, newY);
In polar coordinates, it's super easy to translate the coordinate space with a rotation.
GML:
theta -= view_angle;
And now we just need the y coordinate of the new point.
GML:
var rotatedY = lengthdir_y(r, theta);
That should get you what you need. There may be some need for (90 - theta) conversions, depending on how you're calculating your draw angles, but I hope this idea gets you to the solution you want.
 

Yal

šŸ§ *penguin noises*
GMC Elder
How about...
  • Start from the center of the view.
  • Move VIEW HEIGHT * 0.5 steps in the ROTATION DIRECTION + 180. (Use lengthdir_x and lengthdir_y to go from length / direction to x / y).
  • This point is the bottom of the view.
  • Make a vector in the ROTATION DIRECTION of length VIEW HEIGHT. This is the UP VECTOR.
  • For every thing you wanna depth sort, create a vector between the view's bottom we computed before, and its current real room position. This is the POSITION VECTOR.
  • Compute the dot product between the UP VECTOR and the POSITION VECTOR. This gives you a number that's bigger, the bigger the parallel component of the position vector with respect to the up vector is. Or in layman's terms, the more "up" something is with respect to the view, the bigger this number is. (If it's facing downwards, the number is negative).
  • Depth sort using this number, by just sticking a minus sign in front of it.
For vectors, you can just use an array of size 2. The dot product is a[0]*b[0] + a[1]*b[1], so it's simple enough to compute.
 

Baldi

Member
Thank you very much for your replies! I tried both ways and ended up sticking to what Nidoking said, unfortunately I must have made a mistake that I could not find when trying Yal's approach, but it is working great now. Thank you!
 
Top