Trajectory calculations!

Hello,

Let me put this out there first. I’m NOT a mathematician, so understanding this: is not at all within my capacity.

So, does anyone here have any code or framework, helpful hints etc, with going about making a firing solution for a turret?

Cheers.

Below is the code for my projectile, it’s not a physical object, so, I may have made it wrong.

gravityInertia = Physics.gravity * Time.deltaTime;
			gravityInertia.y = Mathf.Clamp(gravityInertia.y, -21.1f, Mathf.Infinity);
			
	 		transform.position = transform.position + transform.forward * Velocity * Time.deltaTime + (gravityInertia);
			
			lookAtPos = (transform.position + transform.forward * Velocity) + (Physics.gravity * Time.deltaTime);
			transform.LookAt(lookAtPos);
1 Like

I’m not the best at math either… I’d probably use iTween: see here (accurate lob). iTween is free and you could buy the accurate lob example for a dollar, if it’s close to what you’re looking for.

without drag it would be something like this, I think:

var initialVelocityVector : Vector3 = Vector3(100,0,0);
var gravityVector: Vector3 = Vector3(0,-9.8,0);
function Update()
{
     transform.position += (initialVelocityVector + 0.5*gravityVector*Time.deltaTime)*Time.deltaTime;
}
1 Like

To be clear, you’re trying to lob a projectile at a specific point?

Correct.

Also, anyone else NOT getting email notifications? My spam folder is empty…

I downloaded the lob project, and. I have NO idea how this works. lol.

What I would do if I were you is lerp the projectile’s x-z values to the desired position, and set the y position based on that. In my example below, I used the sine function to calculate y position(since it has the shape of a trajectory.
Completely untested, my math and code might be (probably is) off.

var speed : float = 1.0; //how fast the projectile travels
var target : Transform; //what the projectile tries to hit
var trajectoryHeight : float = 10.0; //the peak of the trajectory

private var startPos : Vector3; //the position we started at
private var lerpPos : float = 0.0; //the lerp position we're at

function Start(){
 startPos = transform.position;
}

function Update(){
 lerpPos = Mathf.Clamp(lerpPos + Time.deltaTime * speed, 0.0, 1.0); //lerp values need to be between 0 and 1
 transform.position = Vector3.Lerp(startPos, target.position, lerpPos);
 transform.position.y = trajectoryHeight *Mathf.Sin(lerpPos * Mathf.Pi); //multiplying by pi gives it a cycle of 2 - we'll only use half a cycle
}

As said, I haven’t tested that at all. It may not compile, and it may not work if it does compile. It should be enough to get you started though.
You’ll probably also want to add some damping to lerpPos so it’ll slow down as it goes upwards and speed up as it goes down.

Hope this helps.

-Lincoln Green

Lerping causes the trajectory to be circular, where as real trajectory creates a parabola.

I found this. It has a whole bunch of ‘working’ code at the bottom, but I have no idea how to implement it. It doesn’t seem to explain itself much. Maybe I just lack the common knowledge.

1 Like

Just to add, I’m looking for a real world simulating trajectory calculation. minus air drag.

I cheated a bit to make a lob effect, but it works… my set up:

my turret is set at a 45 degree angle always… but is free to rotate around…just elevation is frozen
I have a max power setting
Max range… at max power the round lands at max range

I detect targets within max range… get their distance from turret
Use the formula

Power = 1-(distance / maxDistance)*maxPower;

Since my elevation is always a 45… the. Projectile lobs… and the turret lowers the power put into each shot to hit pretty accuratly.

Kind of a hack, but works for what I wanted… and fairly simple…

If you need any code example let me know…can post later

I saw this/something similar to this, on Unity Answers. Sadly, it’s not too applicable to an RTS. But thanks anyway.

In what way doesn’t it work in rts? Worked fine for my top down viewed rts / tower defense prototype.

Just looked on wikipedia - Projectile motion - Wikipedia
Under - Angle θ required to hit coordinate (x,y)
can’t you just use that? And use a sensible initial velocity value.

vel = up to you
range = up to you
alt = up to you

g = gravity = 9.81

vel2 = vel * vel
vel4 = vel * vel * vel * vel
dis2 = distance * distance

num = vel2 + sqrt(vel4 - g * (g * dis2 + (2 * alt * vel2)))
dom = g * distance
angle = atan(num / dom)

I think theres a few checks you have to do like it wont hit the point if the (vel4 - g * (g * dis2 + (2 * alt * vel2)))
is negative.

This is very much what I found on Unity Answers. Yet, I don’t know what to do with that number now that I have it. What does it do?

This outputs the angle of your barrel that fires the projectile.

what’s wrong with the code I gave you above?
here is a webplayer example of it.
right click to rotate the turret, hold left click to launch with power.

why not use a parabolic equation to solve your problem that is what it was made for… Just google parabolic equation and start putting in variables to make it work…

So I set the barrel’s X rotation to the returned value?

Also, ivkoni, your example is not suitable for what I’m doing, and boco, I mentioned before that I have no idea how those equations work.

It is not clear from your post what you are doing.
If you want the projectile to hit a sertain position, then you need to solve the equation for an initial velocity.
The initial velocity is a vector with direction and magnitude. Keep one fixed and solve for the other.