Jump to content

Photo

Looking for a toroidal angle function


  • Please log in to reply
3 replies to this topic

#1 Moosh

Moosh

    Tiny Little Questmaker

  • ZC Developers

Posted 11 July 2017 - 08:24 AM

Probably not the right term, but whatever. I'm not the best with math. :P

 

kj9qrjo.png

 

So here's the deal. I've got a screen where the X and Y coordinates wrap at the edges, like the surface of a torus. On it are two points, one I want to move to the other's position taking the shortest path. If both points are close to the edge, one will wrap around, following something like the blue path in this example image. I've taken to the internet searching for existing functions to use, but all I've been able to find is one that finds the distance between the two points. Finding the angles is apparently either so obvious that nobody's asking or so horrible that nobody's answering. So I figured I'd put out a request if anyone more mathematically inclined wants to help out. Otherwise I'll probably just end up doing something hackish and inefficient.



#2 grayswandir

grayswandir

    semi-genius

  • ZC Developers

Posted 11 July 2017 - 08:39 AM

I think this should work?
int TorusAngle(int x1, int y1, int x2, int y2) {
  // Add/Subtract screen sizes to point 2 until it is "closest" to point 1.
  while (x2 - x1 < -128) x2 += 256;
  while (x2 - x1 > 128) x2 -= 256;
  while (y2 - y1 < -88) y2 += 176;
  while (y2 - y1 > 88) y2 -= 176;
  // Get normal angle.
  return Angle(x1, y1, x2, y2);
}

  • Moosh likes this

#3 Moosh

Moosh

    Tiny Little Questmaker

  • ZC Developers

Posted 12 July 2017 - 12:30 AM

Works great! The problem was indeed way simpler than I was making it out to be. :approve:



#4 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 12 July 2017 - 04:19 AM

Added this to std_functions_maths.zh.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users