### How to make an arcing trajectory for projectiles?

#### homeofdeath212

• Posts: 91
I have no idea how to explain this clearly so I have a picture that gives more details to what I want to do.

I want that when my [main actor] fires projectiles, the projectiles will move in an "arc" pattern[projectile trajectory].

What are the ways to do this?

Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2b239ac6186cd9beef4e6f0f5a956e83

#### merrak

• Posts: 2155
Top down game or side view?

#### homeofdeath212

• Posts: 91
Top down since that's what I'm working on.

Huh so there's multiple ways of doing this?
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2b239ac6186cd9beef4e6f0f5a956e83

#### homeofdeath212

• Posts: 91
bump.

Anyway the picture attached shows the perspective the game will have. If it helps.

Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2b239ac6186cd9beef4e6f0f5a956e83

#### merrak

• Posts: 2155
That image shows a fan, which is easier than an arc. To get a fan, fire projectile 1 in the direction of the ship. Fire the other two projectiles at an angle (ship angle +/- T)

The arc is more complicated, since the side projectiles will have to gravitate toward the trajectory of the middle projectile. I worked out the vector algebra for this, but I was rushed so you'll want to check my work. Here's the first projectile:

The projectile is fired with x velocity vx and y velocity vy.

To get the side projectiles, imagine another force pushing them along a vector orthogonal to projectile 1's velocity vector.

f designates the fraction this secondary force acts at as a fraction of the "forward" velocity vector. This doesn't give you an arc yet--rather, a fan. To get the arc, we'll want to reduce the impact of this secondary force as time elapses:

tmax represents the time it takes for the two side projectiles to align themselves in the same direction as the first projectile, and t is the current time. So your velocity formulas are

x velocity of projectile 2 = x velocity of projectile 1 - (f * t)/tmax * y velocity of projectile 1
y velocity of projectile 2 = y velocity of projectile 1 + (f * t)/tmax * x velocity of projectile 1

x velocity of projectile 3 = x velocity of projectile 1 + (f * t)/tmax * y velocity of projectile 1
y velocity of projectile 3 = y velocity of projectile 1 - (f * t)/tmax * x velocity of projectile 1

Initialize t = tmax and then subtract from t until it gets to 0. Once t = 0, discard these formulas and set the velocities of projectiles 2 and 3 equal the velocity of projectile 1.

Addendum. Once you get this working, there's opportunities for some interesting projectile patterns. For example, allow t to go negative and the side projectiles will start to gravitate back toward the middle projectile. You can also replace (f * t)/tmax with f*sin(t) to get a wiggle pattern.

« Last Edit: July 06, 2018, 12:07:33 pm by merrak »

#### TheIndieStation

• Posts: 2258
Ýaaay maths =D

No but in all seriousness, this is amazing stuff!

#### merrak

• Posts: 2155
Ýaaay maths =D

No but in all seriousness, this is amazing stuff!

Actually if anyone passing through here wants to learn more about vector and linear algebra, I found a pretty good introduction on Wolfire Blog: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/

It's incredibly useful for circumstances where you need to create your own physics model.

#### domagojbulat

• Posts: 172
Thanks, Merrak for the link, it is a very nice and useful blog. It starts simple but quickly things become really challenging.