Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Component Sandbox 2

Component logic testing and debug tool for Barotrauma. · By Austin

Oscillator frequency and output incorrect math

A topic by Matlair created 29 days ago Views: 82 Replies: 5
Viewing posts 1 to 5

Hello Austin,

I've been playing around with Oscillator components in Component Sandbox 2 and there seem to be some bugs / incorrect math regarding their frequency and output.


Frequency: 

Setting the frequency value to "1" should change the oscillator's signal every 1 second. In the sandbox, setting the value to "1" seems to change the signal every 0.5 seconds instead. Setting the frequency to "0.5" changes the signal every 1 second.


Output: 

Pulse - should alternate between "null" and "1", but instead alternates between -1 and 1.

Square - should alternate between "0" and "1", but instead alternates between -1 and 1

Sawtooth and Triangle - they seem to never reach the targeted 0 or 1, instead stopping at 0.0001 and 0.9999 before resetting (not quite sure about this one, but it seemed to behave as described from my testing)


Thank you for the amazing work, the sandbox is a godsend for testing out complex circuits before wiring them up in the game.

thanks for the detailed bug report! I’m glad you’re getting use out of the tool! I’ll be looking into this now, thanks :)

Here’s what the logic looked like prior to this bug: image.png

I’ve updated the signal processing method to behave as you described: image.png

For frequency, I realized I was modulating by a value of 1, meaning the value of x will always be between 0 and 1. image.png

This worked (mathematically) but where it falls short is accuracy. Time in Unity is not inclusive to every possible moment of real time, making the incrementation (and thus calculation) inaccurate. It stutters and doesn’t increment as you would expect, so I had to change how x is calculated.

^ TLDR: the numbers were getting skipped because Time.time is not accurate.

Pulse has been updated to look like this, using delta time instead of application time: image.png

I tested this with a light component, it can toggle the light on/off with every pulse.

This fixes pulse, I am going to continue on the others, even though I believe they’re working now. But I will continue to test.

okay I’ve further updated how the oscillator calculates x to guarantee it hits the correct extremes: image.png

it’s possible to further refactor this but I like simplicity because it helps me return to the project and mentally load the code when needed. I’d like to also note that my implementation is an alternative implementation to Barotrauma and may not function exactly. They use a phase value which I call “x” and modulate the value wherein I am “ping-ponging” a value to determine a position in the wave form.

Barotrauma Oscillator

here’s the calculations for each wave form: image.png

I updated the frequency calculation (which I forgot to do after taking screenshots) so that it isn’t at half herz. With that said, after some testing I think it all works now… let me know if you disagree. I’ll publish the new version soon.

I also found some other bugs/issues while testing that will be included.

Whoa, that's some stellar work, I appreciate the detailed writeup!

All seems good to me, I will test out the new version asap.

Thank you and have a great day!

of course!!! have a great day as well. Sorry if it was too much, I enjoy this kind of interaction and want to show I’m dedicated to help/fix