Live web version: http://t.novasquirrel.com/test/mode7/
Source code repository (GNU GPL version 3): https://github.com/NovaSquirrel/Mode7Preview
I used it to come up with the following generic formula for affine parameters, useful for games like Cameltry (aka On the Ball) where the background is parallel to the screen.
Code: Select all
// The S-PPU calculates (U, V) coordinates in the map
// screen coordinates using this formula
// U = A * (X + HOFS - X0) + B * (Y + VOFS - Y0) + X0
// V = C * (X + HOFS - X0) + D * (Y + VOFS - Y0) + Y0
// Other input parameters: framecount and scanline
Tau = 2*Math.PI; // radians per turn, about 6.283
// xpos and ypos scroll the map
// xcenter and ycenter control how far the camera is ahead of the player
// theta controls rotation, dist controls distance from the camera,
// and par controls pixel aspect ratio (which for NTSC is 1.143)
// animate it to drive home that this is the center of rotation
xpos = 0;
ypos = 0;
xcenter = 128;
ycenter = 112;
theta = framecount;
dist = 144 * (1 + .5 * Math.sin(framecount * Tau/512));
par = 1.143;
ctheta = Math.cos(theta * Tau/256);
stheta = Math.sin(theta * Tau/256);
m7a = Math.round(ctheta * dist);
m7b = Math.round(stheta * dist / par);
m7c = Math.round(-stheta * dist);
m7d = Math.round(ctheta * dist / par);
m7x = xpos;
m7y = ypos;
m7hofs = xpos-xcenter;
m7vofs = ypos-ycenter;
return [m7a, m7b, m7c, m7d, m7x, m7y, m7hofs, m7vofs];