Resource Math Snippets

Discussion of tools and resources for game developers

Post » Tue Sep 08, 2015 1:43 am

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.
B
7
S
2
Posts: 92
Reputation: 663

Post » Tue Sep 08, 2015 2:46 am

Lerp(sprite1.x,sprite2.x,0.5)
Lerp(sprite1.y,sprite2.y.0.5)
Image ImageImage
B
172
S
50
G
182
Posts: 8,432
Reputation: 115,083

Post » Wed Sep 09, 2015 6:52 pm

great idea!!! I'm working on a game right now that makes heavy use of arcs and circles - i will definitely post my problems and solutions here.
Image
Get it on Google play or play on papio.nu
B
11
S
2
Posts: 79
Reputation: 786

Post » Wed Sep 09, 2015 7:13 pm

Amazing :) I would love to see. I found something to, in another thread. Someone sent me some examples. I'm gonna go through them pretty soon. If I come up with something interesting I'll post them. The knowledge above is a great resource. It's very hard to make games when you can't even capture points. I think a thread like this would help people quite a bit.
B
7
S
2
Posts: 92
Reputation: 663

Post » Tue Sep 29, 2015 1:06 pm

Using the examples in the original post slightly differently...

Pick an x,y within Range (circular) of Sprite:

a = random(0, 359)
d = random(0, range)
x = Sprite.X + d * cos(a)
y = Sprite.Y + d * sin(a)

By using circular creation zones you can avoid the boxy looking start when picking lots of locations (e.g. when creating a lot of particles or explosions)
B
5
Posts: 17
Reputation: 259


Return to Tools and Resources

Who is online

Users browsing this forum: No registered users and 1 guest