Issue with SKNode coordinate space

I am creating a game that simulates a to-scale version of the solar system in 2D. It allows the user to build and fly rockets around the solar system. However, I am having an issue with position values in the coordinate spaces of each SKNode.

When I try to change a position value of an object that is very far from the origin (around 1,000,000 coordinate spaces or more away in either the x or y axis), the position value is changed slightly. However, the farther from the orgin an node gets, the more this value gets changed.


This function calculates the next position of a planet, and then updates the position of the planet object:


func updatePos(delta:CGFloat, ct:CGFloat){
        
        let t = ct.truncatingRemainder(dividingBy: T)
        let newPos = getPos(at: t)
        
        prevPos = position
        self.position = newPos
        self.vel = calcVel()
        
        if(self.objectName == "Earth"){
            print("Earth P1: \(newPos.dictionaryRepresentation)")
            print("Earth P2: \(self.position.dictionaryRepresentation)")
        }

    }
}


The output here should print the same value. But once I assign my calculated position value to the position of a planet, the value is changed as shown here:


Earth P1: {

X = "-146973645921.7216";

Y = "6187141860.840587";

}

Earth P2: {

X = "-146973638656";

Y = 6187141632;

}


Earth P1: {

X = "-146973645295.3229";

Y = "6187156989.44586";

}

Earth P2: {

X = "-146973638656";

Y = 6187156992;

}


This issue is happening with the SKCameraNode as well. The farther from the origin the position of the camera gets, the choppier the movement of the camera becomes. It also becomes very noticible the more the camera zooms in, as show here:


https://gfycat.com/HighScarceHorsemouse


I have tried so many different things to fix this issue. I have tried to use SKActions to move the objects around instead. This did not fix the issue.


One thing to note: The camera choppyness only happens when its position values become very large. When the camera is near (0, 0) of the SKScene I have no issues with choppyness. This could possibly be an issue with either the position values of SKNodes, or maybe an issue with the coordinate spaces of the scene and the nodes themselves.


The documentation for an SKScene describes a scene as being "infinitely large," so I feel like a scene of this size shouldn't be causing these problems.


Any insight into what this may be would be extremely helpful.

Replies

You're running into the representation limits of floating point. I'm guessing that you're running this on a device where CGFloat is Float rather than Double, although you could run into the same problem with Double if the distances are huge enough.


The problem is that floating point has a slight representation inaccuracy (or you can call it error, if it results from a calculation) in the mantissa of the value. (The mantissa is the value scaled down to the 0-to-1 range.) This error is multiplied by the exponent, which is large for large distances.


I don't think there's much you can do about this. You're not going to notice the difference if your camera is zoomed to a scale commensurate with solar system scales. You are going to notice if the camera is zoomed in to human-size scales.

Why is it that newPos is much more precise than the SKNode's position value? They are both CGPoints, with CGFloats as their x and y values. Shouldn't they be able to hold floating points to the same precision?