So after a really long break from CodeSpells, I've picked it up again to finally tackle some of the code I haven't finished. Here I'll be post a whole bunch of code that demonstrates how to move orbs in certain ways by using a lot of math. Stay tuned!
First is the relative to world acceleration spell. It allows you to accelerate the orb in any direction you provide. In the image below, I've set the acceleration to ( 0 , -10 , 0 ) meaning 0 in the x direction, 10 meters per second per second in the negative y direction and 0 in the z direction. This simulates ballistic trajectory very well and is a (relatively...) small amount of code.
Public spell: Acceleration xyz
Variables (with units):
- accelVector is the corresponding directions and magnitudes you want the orb to accelerate in (m/(s^2))
- speed is the initial speed of the orb (m/s)
- timeStep is the time between each calculation, the lower the number, the more smooth the motion but uses more system resources (s)
Video of above code:
This one was a bit of a nightmare. So this next spell is the relative to player acceleration spell. So the difference between the world relative and the player relative is that the way the orb moves is based on the initial direction that the player was looking on cast. So here, instead of (x,y,z) we have (right,up,forward). The code works by first rotating your (right,up,forward) input to an (x,y,z), which is the hard part. Then we can easily use the previous moveStep function to accelerate the orb in our rotated vector.
For example, to make the orb accelerate to the left of the player, it is done by having a negative number in the 'right' coordinate like (-10,0,0). The code and video below shows an acceleration vector of (0,0,-20) which means that the orb will accelerate right back at the player at 20 m/s^2 since the final coordinate is forward and the negative of forward is backward.
Public spell: Acceleration Player
The cream of the crop. This one is the parametric orb spell. The reason this spell is the cream of the crop is because it allows you to move the orb in a path set by a set of parametric equations. So long as you know the equations you can make the orb move in that path. This is some powerful stuff because it allows you use so many mathematical operations, for example, the trigonometric functions. These allow you to make many weird and wonderful curvy, periodic orb paths.
So for each spatial coordinate (x,y,z) or (right,up,forward), you specify an equation in terms of a parameter like time (in this code it's t) which determines the position of the orb at that time. In this spell, every timeStep seconds it calculates each of these equations and will accordingly adjust its direction and velocity to to be able to arrive at the world position that the equations specify. Variables that can be changed are the first six in On Create and paramOut in the function getParamPos.
- relativeToPlayer: when true the equations have coordinates (right,up,forward), false is (x,y,z)
- addInitialVelocity: when true the equations add the initial speed of the orb to the equation, when false, the orb will move only according to the parametric equations
- delay: how long until the orb starts moving according to the equations in seconds
- timeFactor: this essentially how fast the parameter t ticks forward with time, say if timeFactor is 1, if time passed is 2 seconds then t is 2, if timeFactor is 6, if time passed is 2 seconds then t is 12
- paramPos: the set of three equations describing the orb's path
Public spell: Parametric Orb
Some examples of equation sets with videos:
Videos (format: shape (relativeToPlayer, addInitialVelocity))
1 - Snake (true, false)
2 - Left-curving crescent (true, true)
3 - Corkscrew (true, true)
4 - Expanding corkscrew (true, true)
5 - Infinity symbol (true, false)
There are lots more examples of parametric equations out there. https://en.wikipedia.org/wiki/Parametric_equation