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

  • Context: Undergrad 
  • Thread starter Thread starter newjerseyrunner
  • Start date Start date
  • Tags Tags
    Figure Function Reverse
Click For Summary
SUMMARY

The discussion centers on reversing a 2.5D projection function in programming, specifically the function getYPosition and its inverse. The user seeks to derive the distance from a camera based on pixel height and vertical angle. The equation y = k - (k/d) + (k/d) * ((h + v * d) / 128) is identified as a standard linear equation that can be manipulated to solve for distance d. The final solution is encapsulated in the function yPositionToDist, which accurately calculates the distance based on height and pixel position.

PREREQUISITES
  • Understanding of 2.5D graphics projection techniques
  • Familiarity with linear equations and algebraic manipulation
  • Knowledge of C++ programming and function implementation
  • Experience with rendering concepts in game development
NEXT STEPS
  • Study linear algebra applications in computer graphics
  • Learn about 2.5D rendering techniques in game engines
  • Explore the use of Wolfram Alpha for solving complex equations
  • Investigate the implementation of camera systems in 3D environments
USEFUL FOR

Game developers, graphics programmers, and anyone involved in rendering techniques and camera distance calculations in 2.5D environments will benefit from this discussion.

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   Reactions: 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   Reactions: jedishrfu

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
6K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
7
Views
6K
  • · Replies 16 ·
Replies
16
Views
3K