Call to Action
This is something that doesn't seem to be around a lot, unless people ask for help. I thought it might be interesting to start a math resource thread. This is where we can help each other learn some great math ideas to help in our games. If people help support the thread by adding their own math knowledge I would learn, and others as well. We should keep the math relevant to Construct 2, and share snippets that would actually help people develop better games.
I just want to add the premise that the math below was not created by me. You can thank Archipetes, lol, just joking. I got help from here on the Scirra forums to gamedev, but it's good stuff, and should help people, so I'm sharing my findings.
Some ideas of things people could use in this thread:
1) The simplest way to move an object in a circle.
2) How to move an object along an arc.
3) How to move an object at an arc to 90 degree position (Like would be needed for a circular menu.)
If someone would add those I would gladly test them, save them for my own efforts, and post them here in the first topic. Also feel free to add any other math bits that would help us here in the forums.
Note - correct anything below, and I'll test, and update.
Lerp With Delta Time
Thanks to a blog post by Ashley, the current C.E.O of Scirra, we learn to use lerp with delta time. The method below is how it works to a 't'. The numbers seem to be pretty precise, so I'll explain below. For an explanation beyond my grasp read his post.
a = starting point.
b = destination.
lerp(a, b, 1 - f ^ dt)
The lerp function will ease from point a to b. F is a float between 0.1 and 0.9 which will increase or decrease the speed. The lower the number the faster the object will move.
https://www.scirra.com/blog/ashley/17/u ... delta-time
Note - A good substitute for lerp is lunarray's plugin: LiteTween. It's great if you don't quite like the behavior of a regular lerp. There's a bit extra with it too, and quite easy to learn.
Any Point, Any Angle, Any Distance Away
x = Sprite.X + d * cos(a)
y = Sprite.Y + d * sin(a)
The method above will set x and y any number of pixels set by d(distance) in any direction set by the angle. So lerp the following: Sprite.X + 100 * cos(90), Sprite.Y + d * sin(90) will move the sprite 100px in the 90 degrees direction.
Find an X,Y Within Range
X = sin(random(0, 360)) * random(1, 100)
y = cos(random(0,360))) * random(1, 100)
The following methods will pick a point roughly 1 - 100px away in a random direction. Basically a location within a 100px area. This needs to be calibrated to consider the offset of the sprites size, as the center is usually the starting point, unless specified elsewhere during the sprite editing process.
Find Point Between Sprites
Sprite 1 is traveling halfway to sprite 2.
Adjust fract, or fraction, to change distance.
fract = 0.5
Sprite1.x = Sprite2.x + frac * (Sprite1.x - Sprite2.x)
Sprite1.y = Sprite2.y + frac * (Sprite1.y - Sprite2.y)
Example:
Sprite1.x = Sprite2.x + 0.5* (Sprite1.x - Sprite2.x)
Sprite1.y = Sprite2.y + 0.5 * (Sprite1.y - Sprite2.y)
Above will move sprite1 halfway of the way towards sprite2.
Note - The lower the decimal the closer the sprite will move.