Asteroids on Oscilloscope

The Joy of Tech - Anything and everything about all things shiny..
Post Reply
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Asteroids on Oscilloscope

Post by Kamineko »

After doing the Christmas message a couple of weeks ago, it was only on the way home I realised I already had a couple of reasonably good DACs available, and I didn't need to faff around with the Arduino's PWMs at all. I should have used my laptop's sound card!

I've always wanted to fiddle with a Vectrex, and surely Laptop + Oscilloscope = Ghetto Vectrex! And the first games were on oscilloscopes anyway, so that's wonderful too.

I wrote a simple game in a couple of days over Christmas. The most vectory vector game of all, the obvious one: a copy of Asteroids! If you've not played Asteroids before, here's the rules. Writing something to output audio data in realtime isn't so hard. Base SDL by itself doesn't really give you any audio primitives except that; you need a library like SDL_mixer if you want a pre-constructed system that can play sound effects and stream music.

The game software renders visible objects into an array of line segments (pairs of float pairs) on an internal screen. At the end of every frame, this list is changed into an array of audio samples compatible with the audio output. When the audio buffer callback happens, it selects the unused previously filled audio output array (it's double buffered) and populates the output buffer with copies of the converted data until its completely filled.

You can either render the raw floating point lines to the screen (these are the green pics below), output the contents of the audio buffer to the screen (this would be quantised to fit into the format of the audio buffer, I'm using unsigned bytes), or, if you're feeling adventurous, just listen to the audio buffer. Each of the screens in game has its own particular pitch due to the way the constructed audio buffer loops through the original line list: the more stuff on screen, the longer the period and the lower the pitch. You can also hear the objects moving through the field, it sounds like phasing.

The next Maker Night... it's time to see if this thing even works. There's a few things that might happen. It might come out backwards, or unbearably flickery, or just a squiggle, or the oscilloscope might blow up, or my laptop might blow up.

Image
The place is not short on oscilloscopes! This isn't even all of them.

What we need is a calibration screen. Something the extends to the border so you can scale the picture properly, and something with readable text so you can tell if the picture's flipped.

Image
There we go. Let's see if my code to render the internal line buffer as stereo audio data works...

Image
NAILED IT.

Those wavy lines in the background are the oscilloscope cursor trying to draw the diagonal lines after drawing the border. The cursor flies off and wobbles about instead of resting suddenly. I think what I'm seeing is inductance, either from the scope itself from moving the cursor or the probes themselves (But wouldn't I have seen it on the christmas message? That thing was crystal clear.). That leaves the laptop itself or the sliced up bit of speaker cable I'm using to connect it to the probes.

Image
Who doesn't want to see their name in lights?

Image
To aid the scope in drawing lines, I'm now sending the cursor slowly down the length of each line (20 locations seemed to work the best). The wobbling cursor is very obvious here, the text sort of looks hand-written. My original plan was to alternate between the endpoints of each line, but that was phenomenally crap. I tried increasing the sample rate, but then the entire image was one hilarious distorted circular mess.

Image
Woooo!

Image
Hey, where'd my asteroids go?

If the buffer isn't large enough to hold all the drawable things, the things that are last on the list won't get drawn. That's only really a problem when complex items like text are on screen as they're composed of a ton of lines. Before I added the slowly-iterate-down-each-line stuff, there was tons of space in the audio buffer for everything.

Image
Bingo! No denying it, that's Asteroids!

You can still see the effect of inductance at the start and end of every object as the cursor leaps off and slows down to draw the next object. The extra lives are just three triangles constructed of three lines in the lower left, and the cursor doesn't even attempt to show them right.

Image
...but there is a lot of stuff missing from the screen. The score and highscore captions were completely illegible and distored the screen horrifically so they're gone now.

The glowing border is there to keep the screen centred. (I got the idea for the border from this guy. Everybody's doing Asteroids, hooray!) The display tends to float around, as if it's trying to keep everything centred. When an asteroid floated off the side of the arena, the whole image would jump. I think that's the result of capacitance. Drawing objects multiple times to make them more distinct was a bad idea: as the image floated around, the repeated object would leave a large glowing trail behind it.

I tried looking for an oscilloscope with some different probes, but no luck. There's probably a more accurate way to set up the scope for measing signals like this, but I didn't look too far into it. Considering how well the Arduino Duo message worked, I think I'm at the limits of my laptop's sound hardware than anything else.

Image

And that's the story of how I tried to coerce a piece of antiquated oceanographic analysis equipment into an action packed intergalactic adventure.
User avatar
Flojomojo
Baaaaaa!
Baaaaaa!
Posts: 2341
Joined: Mon Jul 29, 2002 6:21 am 0
XBL Gamertag: jindofox
PSN: jindofox
Location: Washington DC, USA

Re: Asteroids on Oscilloscope

Post by Flojomojo »

That is very beautiful in a retro green vector way.
User avatar
toxibunny
Baaaaaaa!
Baaaaaaa!
Posts: 5906
Joined: Thu Jun 17, 2004 11:59 pm 0
Location: limb clock land

Re: Asteroids on Oscilloscope

Post by toxibunny »

Drunksteroids.
I made a deviantart page.
'....The baron is a gay old rogue with an eye for the ladies....'
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

It's your soundcard. You need a better soundcard. Neither the scope nor the cable could be operating anywhere near their limits.

The drifting about is because the channels are AC-coupled (yes, with capacitors) and you're losing a bit of low-frequency information.

The ringing looks like a high-order filter to me, so it's probably digital, and it's probably brickwall. It may be something you can switch off or move out of your way, either by looking for "DSP effect" settings or by choosing the correct sample rate (which is probably a multiple of 48kHz), or by chasing all other audio-using software out of your system and resetting the audio driver subsystem to force it to stop resampling your data.

If you can't (or, don't want to!) improve your signal conditions, then there's a lot of fun to be had developing mitigation strategies...

For development without a scope, you could either plug your audio out into your audio in (which will probably compound the problem) and plot out what you record, or you could apply a FIR filter to your pixel data before plotting it.

If you look at the waveforms of each channel individually you'll see a lot of discontinuities. The bigger those discontinuities the bigger the ringing effect that the filter will add. You need to find a way of doing things that can either make those steps smaller, or make those corners less pointy.

I think what you could do is to sling-shot the trace from one line to the next, by back-tracking over the same line quickly to pick up some speed, and by landing on the next line at speed before turning back on itself to draw the line slowly. So you would cover the line three or four times -- once slowing down from the direction of the previous line, once moving over it slowly to define it, and once speeding up in the direction of the next line. Maybe once more in the middle if you come out facing the wrong way.

Basically you try to capture that ringing effect and make it wiggle in the same direction as the line you want to draw so it doesn't show, and when you have to leave that line you're travelling so fast that the trail is very faint. If you don't want to over-shoot, then slow down as you reach the end of the line.

You could also add a couple of integrators into your circuit and then rather than driving the absolute positions out of your soundcard, you would drive the velocities out.
User avatar
random_dave
Baaa!
Baaa!
Posts: 255
Joined: Fri Sep 19, 2008 6:37 am 0
XBL Gamertag: lanki of badger
PSN: random_dave
Location: Sheffield

Re: Asteroids on Oscilloscope

Post by random_dave »

if you could make an asteroids desk clock like that it'd be amazing
User avatar
Dio
Sheepie Whore
Sheepie Whore
Posts: 18887
Joined: Mon May 31, 2004 1:08 pm 0
Location: Newcastle UK

Re: Asteroids on Oscilloscope

Post by Dio »

gumboot wrote:The drifting about is because the channels are AC-coupled (yes, with capacitors) and you're losing a bit of low-frequency information.
Image
This is a good example to use. Note how everything 'decays' towards the centre the longer it's left in a given state and then 'rebounds'.

You can calculate how long the cursor is above 0V while writing "MATTSTEROIDS" and it will give you an idea of how quickly the DC voltages are decaying out of the output capacitors.

To improve things without additional hardware you need to ensure that over a reasonably short time constant the DC component of each channel is 0. The shorter you push that time constant the less wonky the picture will get.
User avatar
Dio
Sheepie Whore
Sheepie Whore
Posts: 18887
Joined: Mon May 31, 2004 1:08 pm 0
Location: Newcastle UK

Re: Asteroids on Oscilloscope

Post by Dio »

Further thinking: one way to do that would be if after drawing each (small) object - say, one of the letters in the text - you move the beam to an offscreen position on the opposite side of the screen. The time it needs to stay there will be short because of the relatively extreme position, and the high slew rate caused by such a dramatic voltage change should keep the beam shift artifact relatively small.
User avatar
gilesgoat
Sheepie Whore
Sheepie Whore
Posts: 27259
Joined: Wed Sep 03, 2003 7:54 am 0
Location: Monfalcone, Italy
Contact:

Re: Asteroids on Oscilloscope

Post by gilesgoat »

3 things by the look of it.

1. you should also find a way to control the Z axis to shut down the beam when you move from one place to another

2. every vector generator I've seen as a way to literally short circuit ( via a cmos bilateral switch ) the integrating capacitor every X time
otherwise you get errors that accumulate causing drift, not sure if you could get "something similar" by periodically outputting some
(min, min ) in your (x,y)

3. it sems also you have some sort of noise present on the signal and/or could maybe be high freq harmonics owing the switching
supply maybe ?
Giles the billy goat
"Baa"
User avatar
moonraider
Baa!
Baa!
Posts: 79
Joined: Sun Apr 14, 2013 4:03 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by moonraider »

Beautiful work, sir. Beautiful.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

gilesgoat wrote:1. you should also find a way to control the Z axis to shut down the beam when you move from one place to another
In scopes of that generation you might expect to find an input in back which you can drive to zero to blank the cursor. With not too much circuitry you can make a black and white TV out of a scope in X-Y mode.

gilesgoat wrote:2. every vector generator I've seen as a way to literally short circuit ( via a cmos bilateral switch ) the integrating capacitor every X time otherwise you get errors that accumulate causing drift, not sure if you could get "something similar" by periodically outputting some (min, min ) in your (x,y)
You mentioned integrators. I was wondering about that. I'd planned to do scope asteroids a few years ago and the guy I was talking to about it told me that the biggest problem was in keeping good calibration in the integrators. The implication, then, was that he expected my DAC outputs to drive gradients rather than ramping through absolute coordinates. That instantly solved something I hadn't got around to asking him (how do I emit continuous lines rather than a series of discrete points) but it made the problem seeem a whole lot harder overall.

Then it turned out that a video capture board was going to be more useful, so I did that instead.

gilesgoat wrote:3. it sems also you have some sort of noise present on the signal and/or could maybe be high freq harmonics owing the switching supply maybe ?
Because I've thus-far assumed that this design is running without integrators, I think it's the same smoothing filter which allows continuous lines to be drawn from absolute coordinates is, at the ends of lines, ringing.

I could rant for hours about my feelings on brickwall filters and that ringing characteristic as compared with properly damped filters in audio applications.

Dio wrote:Further thinking: one way to do that would be if after drawing each (small) object - say, one of the letters in the text - you move the beam to an offscreen position on the opposite side of the screen. The time it needs to stay there will be short because of the relatively extreme position, and the high slew rate caused by such a dramatic voltage change should keep the beam shift artifact relatively small.
You could even use the edges of the screen as a place to manouvre so that you can approach the next line segment at an angle that will disguise the ringing.


Random thought: If you spend all your idle cycles overdrawing the player's ship, then the AC coupling will pull the ship towards the centre of the screen. You get hardware-accelerated panning of the playfield!
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

gumboot wrote:It's your soundcard. You need a better soundcard. Neither the scope nor the cable could be operating anywhere near their limits.
Yeah that makes sense. It's the best possible result because it means there's naught I can do about it, so I can stop worrying about it!
The ringing looks like a high-order filter to me, so it's probably digital, and it's probably brickwall. It may be something you can switch off or move out of your way, either by looking for "DSP effect" settings or by choosing the correct sample rate (which is probably a multiple of 48kHz), or by chasing all other audio-using software out of your system and resetting the audio driver subsystem to force it to stop resampling your data.
That code was running on an Acer Travelmate C300 laptop running whatever default AC97 stuff Linux (Debian Wheezy) gives me. I don't see much in the way of visible options in the KDE settings and poking around sounds like a good way to fruitlessly waste a fortnight. Having working audio in Linux for me is already a major victory.
If you look at the waveforms of each channel individually you'll see a lot of discontinuities. The bigger those discontinuities the bigger the ringing effect that the filter will add. You need to find a way of doing things that can either make those steps smaller, or make those corners less pointy.

I think what you could do is to sling-shot the trace from one line to the next, by back-tracking over the same line quickly to pick up some speed, and by landing on the next line at speed before turning back on itself to draw the line slowly. So you would cover the line three or four times -- once slowing down from the direction of the previous line, once moving over it slowly to define it, and once speeding up in the direction of the next line. Maybe once more in the middle if you come out facing the wrong way.

Basically you try to capture that ringing effect and make it wiggle in the same direction as the line you want to draw so it doesn't show, and when you have to leave that line you're travelling so fast that the trail is very faint. If you don't want to over-shoot, then slow down as you reach the end of the line.
That sounds oddly plausible. All of the lines are stored as individual line segments, and there's no barrier to implementing a sort function that asks the cursor to pass through all the segments with minimal wiggle. It would look like a strange smooth curve shooting around between the on-screen objects linking them all together. (Though the interconnecting mesh would flicker madly as the best non-wiggly solution changed from frame to frame.)

Instead of sorting all the line segments, maybe it would be easier to assume that most game objects are going to be closed polygons (everything except text is) and then you can identify good looking wiggle launch and land positions on each object for the cursor to jump between them nicely.
You could also add a couple of integrators into your circuit and then rather than driving the absolute positions out of your soundcard, you would drive the velocities out.
I'm a complete noob when it comes to analogue electronics, but isn't there the potential for large-ish voltages building up facing into the audio output of my laptop with that arrangement?
random_dave wrote:if you could make an asteroids desk clock like that it'd be amazing
Oscilloscope clock? You got it.
To improve things without additional hardware you need to ensure that over a reasonably short time constant the DC component of each channel is 0. The shorter you push that time constant the less wonky the picture will get.

Further thinking: one way to do that would be if after drawing each (small) object - say, one of the letters in the text - you move the beam to an offscreen position on the opposite side of the screen. The time it needs to stay there will be short because of the relatively extreme position, and the high slew rate caused by such a dramatic voltage change should keep the beam shift artifact relatively small.
If I added a counterbalancing 'thing' to the back end of every object to give them a 0,0 centre of mass it would help with centering everything but you'd get really terrible wobbles as the cursor shot back in from the edge of the screen to the draw whatever object was next. The object-begin wobbles are already pretty bad even drawing between different objects on the same screen.
gilesgoat wrote:3 things by the look of it.

1. you should also find a way to control the Z axis to shut down the beam when you move from one place to another

2. every vector generator I've seen as a way to literally short circuit ( via a cmos bilateral switch ) the integrating capacitor every X time
otherwise you get errors that accumulate causing drift, not sure if you could get "something similar" by periodically outputting some
(min, min ) in your (x,y)

3. it sems also you have some sort of noise present on the signal and/or could maybe be high freq harmonics owing the switching
supply maybe ?
I counted six oscilloscopes in the workshop, none of them seemed to have a Z-intensity input sadly.

That's sort of the purpose of the repeating border.

I can't honestly say I can see noise in the signal. If there were noticeable noise, the border wouldn't appear so smooth and neatly overlap itself.
moonraider wrote:Beautiful work, sir. Beautiful.
Thanks! :) It's... some kinda thing, haha.
gumboot wrote:In scopes of that generation you might expect to find an input in back which you can drive to zero to blank the cursor. With not too much circuitry you can make a black and white TV out of a scope in X-Y mode.
The back. Damn. Didn't even think to look at the back.
Random thought: If you spend all your idle cycles overdrawing the player's ship, then the AC coupling will pull the ship towards the centre of the screen. You get hardware-accelerated panning of the playfield!
Without the border, it does exactly that. As the screen display bounces around, the multiple copies of the ship stack up together into a blurry object fading and heading back into the screen. Looks like a mixture between Tempest and Decaying Orbit.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

Kamineko wrote:
gumboot wrote:You could also add a couple of integrators into your circuit and then rather than driving the absolute positions out of your soundcard, you would drive the velocities out.
I'm a complete noob when it comes to analogue electronics, but isn't there the potential for large-ish voltages building up facing into the audio output of my laptop with that arrangement?
It's safe. An integrator won't significantly exceed the supply-rail voltages to the opamp (it's not a charge pump). However, although I've never built one myself I understand calibration to be a bit of a headache. Maybe not the best starting point for an introduction into analogue electronics.

Or maybe it is. My analogue is weak. Somebody else might want to weigh in on that.
User avatar
Dio
Sheepie Whore
Sheepie Whore
Posts: 18887
Joined: Mon May 31, 2004 1:08 pm 0
Location: Newcastle UK

Re: Asteroids on Oscilloscope

Post by Dio »

Kamineko wrote:If I added a counterbalancing 'thing' to the back end of every object to give them a 0,0 centre of mass it would help with centering everything but you'd get really terrible wobbles as the cursor shot back in from the edge of the screen to the draw whatever object was next. The object-begin wobbles are already pretty bad even drawing between different objects on the same screen.
Have you tried it? I wonder if the wobbles are actually caused by decay of a large DC value being present in the capacitor in the first place...
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Nope. I won't be able to get to a scope again for a while.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

Kamineko wrote:Nope. I won't be able to get to a scope again for a while.
Use the line-in on your soundcard.
Post Reply