On the NES, few people would calculate a sine at runtime. There are lots of ways to calculate them, though, and lots of reasons why you might do this rather than a lookup. The naive way of calculating one is probably the
Taylor Series definition of the sine, i.e. you have an infinite series with increasingly smaller terms, and you just evaluate enough terms to reach the accuracy you want.
CORDIC is the name of a commonly used set of trigonometry calculations for CPUs without a hardware multiply, like the 6502.
JoeGtake2 wrote:
Yeah, the target point method is essentially what I'm working with. I can get oscillation in either axis just fine, I just can't get a dependable circular motion that way...or even close, no matter how i populate the starting values or starting velocities (LOTS of trial and error, too). I mean, it's a cool organic sort of movement, but certainly not orbiting 'around' the orbit object ... any tips on better control over this?
To calculate the starting velocity, know that X velocity is 0 at the two horizontal extremes, and Y is 0 at the vertical. So, starting from zero, the velocity at frame T is A*T. The position accumulates each frame, though, so it's a series, (A*1)+(A*2)+(A*3)+...(A*T) = A*(1+2+3+...T) which
reduces to: A*T*(T+1)/2
So, now that you can easily calculate position and velocity in terms of frame, pick a number of frames for your object to travel from the extreme to the centre. For instance, if it takes 5 frames, and acceleration is +3 per frame:
radius = 3 * 5 * (5+1) / 2 = 3 * 5 * 6 / 2 = 45
starting velocity = 3 * 5 = 15
So, in this case, you can place your object on the Y extreme, at (0,45) relative to the target, and give it a starting velocity of (15,0), and it will create a perfect orbit, at least until the target moves. Once the target moves, the circle gets stretched, probably irreversibly without extra corrections.
Here are some ways to do corrections to recover a circle:
1. Clamp your maximum velocity to that calculated starting velocity value, this creates a maximum radius.
2. Encourage motion in one direction only, like a
Coriolis effect. Discourage anti-clockwise movement by preventing out-of-phase acceleration, by using 4 quadrant rules for acceleration instead of just 2 axis rules, e.g.:
- top right - always accelerate down, only accelerate left if X velocity is positive
- bottom right - always accelerate left, only accelerate up if Y velocity is positive
- bottom left - always accelerate up, only accelerate right if X velocity is negative
- top left - always accelerate right, only accelerate down if Y velocity is negative
With rules like this, starting velocity/position doesn't really matter, as it should eventually seek an orbital equilibrium. Play with the acceleration and clamping velocity parameters until you find the right balance. You probably want to test based on a velocity threshold (not simply negative/positive) to be able to accelerate up to a minimum velocity, but there's a tradeoff between accurate phase and narrow velocity range. There's many other tweaks that can be done, but this is a start.