[WIP] 3D starfield Demo

Hi everyone! Here is a quick and dirty demo I put together to see if I can make a space flight game kinda like Elite.
I am super rusty at trig and floating point maths are slow.

Controlls:
A: Speed up
B: Slow down
Left & Right: Roll on Z axis
Up & Down: Pitch on Y?

https://github.com/ankorite/Arduboy-Starfield-Demo

3 Likes

Can confirm that it works and that up and down are pitch.

Also I recommend fixed point maths. There’s some very nice resouces if you know where to look.

2 Likes

Very nice demo :slight_smile:

There are some glitches with random lines right?

Yeah, when it starts there are some issues with the first round of stars. I believe I can easy fix it by setting up the old x and y positions initially like I do when they exit the screen.

Also thanks! Glad you like it. Those resorces listed above are very helpful, next step is getting rid of floats and making a sin/cos lookup table using real numbers.

I’ve done this once before, so here’s some advice:
You only need to store one quadrant’s worth of angles.
If you’re using 8-bit binary radians (brads) then you only need to store 64 angles.
Then you can do this:

constexpr inline AngleResult AngleLookup(uint_fast8_t index)
{
        // This table will need to be in progmem,
        // so you'll need a progmem lookup here too.
        // The progmem lookup might invalidate the constexprs.
	return (index == 64) ? 1 : AngleTable[index & (0x3F)];
}

constexpr AngleResult Sin(uint8_t brads)
{
	const uint8_t quadrant = ((brads & 0xC0) >> 6);
	const uint8_t index = ((brads & 0x3F) >> 0);
	switch (quadrant)
	{
	case 0:	return AngleLookup(index);
	case 1:	return AngleLookup(64 - index);
	case 2:	return -AngleLookup(index);
	case 3:	return -AngleLookup(64 - index);
	default: return 0; // Theoretically unreachable code
	}
}

constexpr AngleResult Cos(uint8_t brads)
{
	const uint8_t quadrant = ((brads & 0xC0) >> 6);
	const uint8_t index = ((brads & 0x3F) >> 0);
	switch (quadrant)
	{
	case 0:	return AngleLookup(64 - index);
	case 1:	return -AngleLookup(index);
	case 2:	return -AngleLookup(64 - index);
	case 3:	return AngleLookup(index);
	default: return 0; // Theoretically unreachable code
	}
}

This code only works with unsigned numbers though, I haven’t developed a signed version.
Signed numbers use more program space anyway (because there has to be extra code for checking status register flags and things).

1 Like

Thanks! That is very clever way to do it. Looks like I have a bit of reading to do this weekend. I really appreciate the help.

1 Like

Oh, and in case you’re wondering, AngleResult has 1 sign bit, 1 integer bit and 14 fractional bits. I tried several sizes and found that to be the best balance for what I was doing. If it weren’t for 1 and -1 being possibilities I could have made it all fractional.

Also I missed this resource because it’s hidden on the second page of the search I did.

(I also found a few corpses, the Mafia has been busy.)

3 Likes

This is awesome, I wonder if it could be combined with the teapot demo and get even closer to a starfox clone :slight_smile:

2 Likes