I How Do You Reverse a 2.5D Projection Function in Programming?

AI Thread Summary
The discussion focuses on reversing a 2.5D projection function in programming, specifically the need to derive distance from a given y-position and height. The original function calculates y-position based on height and distance, but the user struggles to reverse it to find distance. They initially attempted to use Wolfram Alpha for assistance but found it inadequate due to the equation's complexities. Ultimately, they realized that multiplying the equation by distance transformed it into a solvable linear equation. The user successfully developed an algorithm to convert y-position back to distance using a derived formula.
newjerseyrunner
Messages
1,532
Reaction score
637
I have this function that I need to reverse.

int getYPosition(float height, float dist) const {
const float hh = renderer -> viewPortHalfH / dist;
return renderer -> viewPortHalfH - hh + hh * ((height + renderer -> player -> verticalAngle * dist) / 128.0f);
}

float getDistYPosition(float height, int yPosition) const {
//wtf, I can't figure out what this should be
return 1;
}

I can't figure out how to reverse it though, I should be able to take the resulting int, put it through a function with the height value and get back distance. Normally, when I get stuck, I just ask wolfram alpha to solve it for me, but that doesn't seem to be giving me an answer.

y = k - (k/d) + (k/d) * ((h + v * d) / 128) solve d
Is there something with my equation that makes it fundamentally irreversible?

I feel like wolfram alpha is having trouble because the general equation has some quirks that the actual code won't? Like the fact that both renderer -> viewPortHalfH and dist are always positive and non-zero. How would I tell wolfram alpha about this (would it even help?)
 
Mathematics news on Phys.org
Are you trying to implement the Pythagorean distance here?
 
y = k - (k/d) + (k/d) * ((h + v * d) / 128)
Multiply the equation by d and you get a standard linear equation which you can solve for d.
 
  • Like
Likes newjerseyrunner
jedishrfu said:
Are you trying to implement the Pythagorean distance here?
No. I'm not sure what it's called. The first function projects a 2.5D point to a 2D screen, I'm trying to reverse the projection. I don't know x or y position, in fact, I need this distance calculation to figure that out in order to texture it.
Screen Shot 2020-09-22 at 10.28.55 PM.pngIt's for determining distance from a camera across the floor. I knew the distance of the bottom of the wall, and I knew what pixel height it started at. I then need to iterate along each pixel going down and figure out it's distance from the camera.

mfb said:
Multiply the equation by d and you get a standard linear equation which you can solve for d.
That was the step I needed, I was able to figure out the algorithm from there

float yPositionToDist(float height, int pixel) const {
float top = (height - 128.0f) * renderer -> viewPortHalfH;
float bottom = (renderer -> player -> verticalAngle + 128.0f) * renderer -> viewPortHalfH - 128.0f * (float)pixel;
return -top / bottom;
 
  • Like
Likes jedishrfu
Seemingly by some mathematical coincidence, a hexagon of sides 2,2,7,7, 11, and 11 can be inscribed in a circle of radius 7. The other day I saw a math problem on line, which they said came from a Polish Olympiad, where you compute the length x of the 3rd side which is the same as the radius, so that the sides of length 2,x, and 11 are inscribed on the arc of a semi-circle. The law of cosines applied twice gives the answer for x of exactly 7, but the arithmetic is so complex that the...
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...
Back
Top