Circles & Sine Waves: Playing with Pi

A forum to share your demonstrations stacks, fun stacks, games, etc.
Post Reply
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Circles & Sine Waves: Playing with Pi

Post by xAction »

Simple fun math for the whole family.
Circle_simple.oxtStack
(3.45 KiB) Downloaded 22 times
Circle_simple.gif
Circle_simple.gif (133.73 KiB) Viewed 324 times
I know what you're thinking: "Hey that's not a circle!", aha, indeed it is not, but it is a product of pi with an extremely low radius, a whole bunch of other cool shapes are generated with the same exact code and a drag of the scrollbar.

The script without all the UI baggage is simply this:
repeat with t = 0 to (2 * pi) step pi / 180
-- Calculate x and y coordinates
put radius * cos(t) into x
put radius * sin(t) into y

-- Shift the coordinates to center the circle
add centerX to x
add centerY to y

put x*100 & comma & y*100 & cr after tCirclePoints
end repeat
Hours of fun right there.

The IDE comes with an "Oval" Graphic, but with this you could create, lets' say a variety of old school pixelated "Asteroids" or a...
right-angle triangle. It consists of a square with two lines extending from it: one horizontally from the middle of its right side and another vertically from its bottom side. The right-angle triangle is characterized by one angle measuring 90 degrees (the right angle). It is a fundamental geometric shape used in various mathematical and engineering applications.
All that with one function and by changing just one value!

I used this function in the action game to spawn ships at a random point along the circle, around the rect of the card, but not right on top of the player. Maybe it'll come in handy elsewhere.

Action is in the stack script. Enjoy!
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

Sine waves can be fun and useful, especially if you want some kind of variation to movement but totally random positions are too erratic and unpredictable.
SineWave.oxtStack
(5.59 KiB) Downloaded 22 times
SineWave.png
SineWave.png (12 KiB) Viewed 344 times
This is another super simple script, again without all the extra GUI baggage this is the magic:
repeat with x = 0 to width of this card step StepsPerIteration
put x & "," & y+(Amplitude * sin(Oscillations * pi * x / Wavelength)) & cr after tPoints
end repeat
That's it! How fun is that? Too much fun, that's how much.

See the card script for details.
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

We heard you like circles so we put circles in your circles.
Circles_Concentric.oxtStack
(4.59 KiB) Downloaded 22 times
Circles_Concentric.gif
Circles_Concentric.gif (263.01 KiB) Viewed 325 times
This stack demonstrates different properties of a circle such as the aspect ratio to create ovals, or the illusion of 3D, control of the diameter, repetitions, and the engine's glow effects to make nostalgic retro cyber graphics.

Action is in the stack script.
User avatar
richmond62
Posts: 2767
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by richmond62 »

Every time I have ever needed a circle for something other than a 'circle' (i.e. a picture) I have generated a set of points in a scrolling list field and then set the points of a graphic to them.

This goes way back to when I was generating circles and suchlike on a BBC Master computer of mine in 1989 in BBC BASIC.
https://richmondmathewson.owlstown.net/
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

SineWave_animated.oxtStack
(7.72 KiB) Downloaded 22 times
You can animate sine waves
sinewaveanim.gif
sinewaveanim.gif (16.15 KiB) Viewed 332 times
with a simple change to the main loop:
repeat with x = waveStartX to width of this card step StepsPerIteration
put x & "," & y + (Amplitude * sin(Oscillations * pi * x / Wavelength)) & cr after tPoints
end repeat
Calling that from a handler that modifies the waveStartX position creates the appearance of animation.

The math is explained in the card script. But let's have a look at it here:

Consider using the equation of a sine wave:
(y = A \sin(kx + \phi)), where
(A) is the amplitude,
(k) is the wave number (related to wavelength),
(x) is the x-coordinate, and
(\phi) is the phase angle.

To achieve the animation effect, you can adjust the phase angle (\phi) over time.
As (\phi) changes, the entire wave will shift horizontally.
The equation with a time-dependent phase angle becomes:
(y = A \sin(kx - vt)),
where (v) is the velocity.
In my case I didn't use the velocity*time directly in the equation. I controlled the speed outside of the equation by using a global AnimSpeed that can be a value from 0.01 to 1, this value is subtracted from the waveStartX global to tease out the animation. Maybe I should rework that. But hey this works.

What can you do with it?
For example, when you have a side scrolling video game and you have some enemies that zoom in from the sides of the screen, you don't want them just coming straight ahead and getting knocked on the nose by your favorite flavor of photon torpedo. With sin wave you have a plentiful supply of enemy motion options: zig-zag, wavey, extreme jitter, smooth and groovy, and everything in between

Of course just flip your x and y values vertical scrolling.

Ah that reminds me, say you want to make a top down vertical scrolling racing game? Here's your road with A bit of tweaking here and there like sliding the x start position a little left or right and you got curves and turns to avoid!
Now this is pod - racing! Yippee!
User avatar
tperry2x
Posts: 1533
Joined: Tue Dec 21, 2021 9:10 pm
Location: Britain (Previously known as Great Britain)
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by tperry2x »

This is totally beyond me, but I get how it works. I couldn't have come up with it myself.

Very clever indeed.
User avatar
OpenXTalkPaul
Posts: 1574
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by OpenXTalkPaul »

xAction wrote: Mon Feb 26, 2024 11:10 am
What can you do with it?
For example, when you have a side scrolling video game and you have some enemies that zoom in from the sides of the screen, you don't want them just coming straight ahead and getting knocked on the nose by your favorite flavor of photon torpedo. With sin wave you have a plentiful supply of enemy motion options: zig-zag, wavey, extreme jitter, smooth and groovy, and everything in between
Wow, more great stuff, thanks!
There's lots you can do with this sort of thing, like generate sine sound samples :-D !
In fact I was recently looking at a stack that draws wave samples but used all straight lines to do it (so everything looked like more like saw-waves when you zoomed in)
User avatar
OpenXTalkPaul
Posts: 1574
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by OpenXTalkPaul »

That Skytopia site is a really cool, there's a bunch of interesting articles on there.
https://www.skytopia.com/project/scale.html
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

Sometimes when I play with the sliders I can almost hear the sound that it makes.
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

Okay so we've done simple circles, and just like my old high school thought, that means we're ready for harder math.
"On a 2D screen, going from left to right, how would one calculate the arc trajectory of projectile that has weight, like a cannonball"
Projectile_Trajectory.oxtStack
(13.12 KiB) Downloaded 15 times

--- Action is in the stack script!
The value that the scrollbar adjusts will change according to the property selected in the Control field.
Trajectory.gif
Trajectory.gif (68.72 KiB) Viewed 238 times
Here is a wikipedia article on "Artillery Games"
Example on Mac Classic
The classic Scorched Earth computer game, which would inspired the WORMS franchise.
And I'm like 80% sure this scene from the Disney film TRON is based on the games made with this simulation.
Here is a resource for educators and students

Ok we are armed with context let's get to the science of it.
Calculating the arc trajectory of a projectile with weight, such as a cannonball, involves understanding its motion under the influence of gravity.
Trajectory Definition:
The trajectory, also known as the flight path, is the path followed by a moving object under the action of gravity.
For short distances, the trajectory of a projectile (like a cannonball) can be approximated by a parabola.

Equations of Motion:

We’ll use the following equations of motion:
(h) = (x = V_x \cdot t) --- Horizontal Position
(v) = (y = h + V_y \cdot t - \frac{g \cdot t^2}{2}) --- Vertical Position
(x) represents the horizontal distance
(y) represents the vertical height
(V_x) is the horizontal velocity component.
(V_y) is the vertical velocity component.
(g) is the acceleration due to gravity.
(t) is the time.

Velocity Components:
The horizontal velocity component ((V_x)) is given by (V_0 \cdot \cos(\alpha)).
The vertical velocity component ((V_y)) is given by (V_0 \cdot \sin(\alpha)), where:
(V_0) is the initial velocity.
(\alpha) is the launch angle.

Combined Formula:
Combining the equations of motion and velocity components, we get the trajectory formula:
[y = h + x \cdot \tan(\alpha) - \frac{g \cdot x^2}{2 \cdot V_0^2 \cdot \cos^2(\alpha)}]

Interpretation:
(h) represents the initial height.
(x) is the horizontal distance traveled.
(\alpha) is the launch angle.
The term (\frac{g \cdot x^2}{2 \cdot V_0^2 \cdot \cos^2(\alpha)}) accounts for the effect of gravity on the trajectory.

Easy. :roll:

Some not so obvious math concepts in that equation:

Let’s break down the mathematical notation in the trajectory formula:
The symbol (\cdot) represents multiplication.
In the context of the trajectory formula, (x \cdot \tan(\alpha)) means multiplying the horizontal distance (x) by the tangent of the launch angle (\alpha).

The expression (\frac{g \cdot x^2}{2 \cdot V_0^2 \cdot \cos^2(\alpha)}) is a fraction.
Here’s what each part means:
(g) represents the acceleration due to gravity.
(x^2) is the square of the horizontal distance.
(V_0) represents the initial velocity.
(\cos^2(\alpha)) is the square of the cosine of the launch angle (\alpha).

In summary, the trajectory formula combines these components to describe the path of a projectile (like a cannonball) considering both horizontal and vertical motion.

Note the weird curly braces in the equation...well:
Curly Braces Around the 2:
The curly braces ({}) around the 2 in the expression (\frac{g \cdot t^2}{2}) are not standard mathematical notation.
They might have been used for emphasis or clarity, but mathematically, they don’t change the meaning of the expression.
In programming, you can ignore these curly braces. The expression remains the same without them.

Okay...ignore curly braces.

Important note after like 3 hours of fighting this thing. Gravity_Acceleration should be a negative value, that way it decelerates the object until the zenith and then causes it to plummet back to virtual earth. I asked ChatGPT four times how to get the arc to a target and it was like "Trial and error" and "Your code looks correct, except for everything you do to make your code make sense to anyone but a computer" Not one single time did it say "You know if your Gravity_Acceleration is not negative, your projectile will fly off toward infinity rapidly and then turn around and smack in you the face before you see it coming"

Also, because ChatGPT chewed me out on this: a note about Globals in this stack and others I make.
Apparently, Globals are dangerous in bigger projects. But when I'm experimenting I can't stand having the Message Box pop open every time I want to check that a variable has as sensible value, and then having to hunt down all the stray "put" tstatements I placed to test values. So I pile on the Globals so I can see what they are doing when I want to see them. Also so I don't have to use massively long arguments in handlers like "Update all the fields that show global values with all these globals values" or packing things into custom proeprties and then pulling them out again. It's also something I picked up from games where you can set a global value in an in-game console and the whole program knows what you changed without any issues. An extra benefit is that when the Message Box does appear...it's because something has goofed in my code, like I forgot to finish a Put line with an "into something". Usually those kind of lines are some extreme test of all the values in a handler or function, like there's 10 things changing and one of them is wrong, but which one?
User avatar
tperry2x
Posts: 1533
Joined: Tue Dec 21, 2021 9:10 pm
Location: Britain (Previously known as Great Britain)
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by tperry2x »

I don't know how you even begin to work this kind of stuff out.
It's insane. Insanely great.
All of these posts in this "xTalk Fun and Games" section deserve to be featured within the openXtalk application in my view.
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

The first step is asking a question. The second part is the patience and attention span to get through it. No distractions is a key. I was about to give up at a point and say "Well it shoots, you figure out how to get the arc working." but something at the back of my head said "all this gobbelygook math there...something don't look right" and not only was it the Gravity_Acceleration being positive instead of negative...but I had a typo in that variable in TWO PLACES. AAAGh!

I do hope we can compile a neat list of examples, that work. I went through the Sample Stacks because I know there was a broken example of A* Pathfinding out there in the wild. What a mess of stacks. Some cool ones, but so many broken things.
I got A* Pathfinding working 50% by the way, but today's a new day, time to make and break a new thing :lol:
Remind me to get back to that.

Something cool coming up!
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Circles & Sine Waves: Playing with Pi

Post by xAction »

Delicious xTalk Breakfast Projectiles now with 100% more Bounce!
Projectile_Trajectory_and_Bounce.oxtStack
(18.1 KiB) Downloaded 14 times
Trajectory_WithBounce.gif
Trajectory_WithBounce.gif (397.43 KiB) Viewed 216 times

How'd I do that bounce thing? So tired, can't think.
Oh right, if the trajectory lands at the same Y coordinate it started then time resets and the originX and originY get updated to the current position. Friction subtracts from the initial_velocity until it reaches zero, and then the ball is allowed to fall until it goes off screen and resets.

When selecting the The Time_Passed line the Hilite will turn red because:
A) you are controlling the time variable of the simulation , so it won't just run on it's own
B) it gets weird when time resets on the bounce, while you control time, and I haven't figured out how to fix that
Some neat weird things happen. Fun with numbers.
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests