This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| 4rpl:commands:signalgenerator [2021/05/22 14:43] – Karsten75 | 4rpl:commands:signalgenerator [2025/09/03 16:43] (current) – [Sine wave in any direction without gaps] kalli | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ~~NOTOC~~ | + | < |
| - | < | + | < |
| ====== SignalGenerator ====== | ====== SignalGenerator ====== | ||
| - | SignalGenerator(< | + | SignalGenerator(< |
| ===== Description ===== | ===== Description ===== | ||
| Line 13: | Line 14: | ||
| Arguments and type in order: | Arguments and type in order: | ||
| - | * interval: (integer vale) X coordinate | + | * time: (float) Current step in the waveform. |
| - | * frequency: (floating point number) The number | + | * frequency: (float) The frequency dictates the duration |
| - | * phaseShift: (floating point number) An offset | + | * phaseShift: (float) A starting |
| - | * invert: | + | * invert: (0/1) inverts the waveform |
| * signalType: (0 to 6) | * signalType: (0 to 6) | ||
| * 0 = NONE | * 0 = NONE | ||
| Line 22: | Line 23: | ||
| * 2 = SQUARE | * 2 = SQUARE | ||
| * 3 = TRIANGLE | * 3 = TRIANGLE | ||
| - | * 4 = SAW - TOOTH | + | * 4 = SAW-TOOTH |
| * 5 = RANDOM | * 5 = RANDOM | ||
| * 6 = CONSTANT | * 6 = CONSTANT | ||
| - | === Notes === | + | The resulting variable sigValue contains |
| - | == Phase Shift == | + | |
| - | The phase difference, or phase shift as it is also called, of a Sinusoidal Waveform is the angle Φ(Greek letter Phi), in degrees or radians that the waveform has shifted from a certain reference point along the horizontal zero axis. In other words phase shift is the lateral difference between two or more waveforms along a common axis. Sinusoidal waveforms of the same frequency can have a phase difference. | + | |
| - | The phase difference, Φ of an alternating waveform can vary from between 0 to its maximum time period, T of the waveform during one complete cycle and this can be anywhere along the horizontal axis between, Φ = 0 to 2π(radians) or Φ = 0 to 360o depending upon the angular units used. | + | ===== Example ===== |
| + | To create a sine wave that should start at its peak and run for 80 frames until it repeats, we should set the frequency | ||
| - | ===== Examples | + | Play the following example in the editor console to spawn a continuous stream of creeper using the sine wave at the bottom of the map. |
| + | <code 4RPL> | ||
| + | $time:0 | ||
| + | $duration: | ||
| + | $phaseShift: | ||
| + | $invert:0 | ||
| + | $signalType: | ||
| + | |||
| + | SignalGenerator(< | ||
| + | <-time 1 add ->time | ||
| + | |||
| + | #Let's use the result to spawn some creeper in a range of cells: | ||
| + | AddCreeper(< | ||
| + | |||
| + | :Once | ||
| + | 1.0 < | ||
| + | | ||
| + | </ | ||
| + | |||
| + | ===== Patterns | ||
| + | [{{cw4_signalgenerator_1.png|1 - SINE, Pattern repeated every 60 cells}}] \\ | ||
| + | [{{cw4_signalgenerator_2.png|2 - SQUARE, Pattern repeated every 60 cells}}] | ||
| + | [{{cw4_signalgenerator_3.png|3 - TRIANGLE, Pattern repeated every 60 cells}}] \\ | ||
| + | [{{cw4_signalgenerator_4.png|4 - SAW-TOOTH, Pattern repeated every 60 cells}}] | ||
| + | [{{cw4_signalgenerator_5.png|5 - RANDOM, Pattern repeated every 5 cells}}] \\ | ||
| + | |||
| + | ===== Extra Notes ===== | ||
| + | This is how each wave is produced in the game's source code: | ||
| + | ==== Sine wave function ==== | ||
| + | <code C#> | ||
| + | float t = frequency * time + phase; | ||
| + | value = (float)Mathf.Sin(2f * Mathf.PI * t); | ||
| + | </ | ||
| + | ==== Square wave function ==== | ||
| + | <code c#> | ||
| + | value = Mathf.Sign(Mathf.Sin(2f * Mathf.PI * t)); | ||
| + | </ | ||
| + | ==== For triangle wave function ==== | ||
| + | <code c#> | ||
| + | value = 1f - 4f * (float)Mathf.Abs(Mathf.Round(t - 0.25f) - (t - 0.25f)); | ||
| + | </ | ||
| + | ==== Saw-tooth wave function ==== | ||
| + | <code c#> | ||
| + | value = 2f * (t - (float)Mathf.Floor(t + 0.5f)); | ||
| + | </ | ||
| + | ==== Random wave function ==== | ||
| + | The function will output the same number for a given interval. | ||
| + | |||
| + | <code c#> | ||
| + | float interval = 1 / frequency; | ||
| + | int slot = (int)(time / interval) ; | ||
| + | slot = (slot * 1431655781) + (slot * 1183186591) + (slot * 622729787) + (slot * 338294347); | ||
| + | if (slot < 0) slot = -slot; | ||
| + | value = (float)GameSpace.instance.RandDoubleInput(randSeed + slot)*2-1; | ||
| + | </ | ||
| + | |||
| + | ===== Extra examples ===== | ||
| + | ==== Sine Wave Creeper on Terrain | ||
| <code 4rpl> | <code 4rpl> | ||
| # On a map with terrain of 200 in X direction and at least 150 in Z direction (3D coordinates) | # On a map with terrain of 200 in X direction and at least 150 in Z direction (3D coordinates) | ||
| Line 57: | Line 114: | ||
| {{sine_creeper.png? | {{sine_creeper.png? | ||
| - | <=[[4rpl: | + | ==== Make a Unit Move or " |
| + | <code 4RPL> | ||
| + | # Oscillate | ||
| + | $UID:1 | ||
| + | |||
| + | GetUnitMoveCell(< | ||
| + | if (< | ||
| + | SignalGenerator(< | ||
| + | <-time 1 + ->time | ||
| + | < | ||
| + | GetUnitPosition(< | ||
| + | GetExactTerrain(< | ||
| + | SetUnitPosition(< | ||
| + | # SetObjPosition(2 | ||
| + | else | ||
| + | 0 ->time | ||
| + | endIf | ||
| + | |||
| + | :Once | ||
| + | # Parameters for SignalGenerator | ||
| + | 1.0 180 / -> | ||
| + | 0.0 -> | ||
| + | false ->invert # Invert the waveform | ||
| + | 1 -> | ||
| + | 0.5 -> | ||
| + | </ | ||
| + | |||
| + | ==== Sine wave in any direction without gaps ==== | ||
| + | {{ : | ||
| + | |||
| + | <code 4RPL> | ||
| + | 30 30 v2 165 91 v2 15 4.5 0 @getSinusoidCells ->list | ||
| + | <-list 0 do | ||
| + | < | ||
| + | loop | ||
| + | |||
| + | : | ||
| + | ->cell1 | ||
| + | ->cell0 | ||
| + | <-cell1 <-cell0 sub ->dir | ||
| + | <-dir.0 abs <-dir.1 abs max ->length | ||
| + | < | ||
| + | <-cell0 | ||
| + | < | ||
| + | dup <-dirL add | ||
| + | loop | ||
| + | < | ||
| + | |||
| + | : | ||
| + | ->shift # FLOAT between 0 and 1 that will shift the signal from signalgenerator. | ||
| + | -> | ||
| + | ->ampl | ||
| + | ->cell1 | ||
| + | ->cell0 | ||
| + | |||
| + | clearstack | ||
| + | <-cell0 <-cell1 @getSingleLineCells dup -> | ||
| + | < | ||
| + | |||
| + | <-cell1 <-cell0 sub normalize ->dir | ||
| + | <-dir.1 <-dir.0 neg v2 <-ampl mul ->dirP | ||
| + | |||
| + | <-cell0 | ||
| + | < | ||
| + | i <-freq <-shift 0 1 signalgenerator <-dirP mul < | ||
| + | loop | ||
| + | list | ||
| + | |||
| + | </ | ||
| + | |||
| + | < | ||