Hello,
i am programming a small game for quite some time. We started coding a small FPS-Shooter inside of a project at school to get a bit experience using directX.
I dont know why, but i couldnt stop the project and started programming at home aswell. At the moment i am trying to create some small AI. Of cause thats definatlly not easy, but thats my personal goal anyways. The topic could prolly fill multiple books hehe.
I've got the walking part of my bots done so far. They walk along a scriped path. I am not working on the "aiming" of the bots.
While programming that i hit on some math problem i couldnt solve yet. I hope of your input on this to help me get further. Concepts, ideas and everything else are highly appreciated.
Problem:
Calculate the position (D3DXVECTOR3) where the curve of the projectile (depends on gravity, speed), hit the curved of the enemys walking path (depends on speed). We assume that the enemy walks in a constant line.
Known variables:
float projectilSpeed = 2000 m/s //speed of the projectile per second
float gravitation = 9.81 m/s^2 //of cause the gravity lol
D3DXVECTOR3 targetPosition //position of the target stored in a vector (x,y,z)
D3DXVECTOR3 projectilePosition //position of the projectile
D3DXVECTOR3 targetSpeed //stores the change of the targets position in the last second
Variabledefinition
ProjectilePosition at time of collision = ProjectilePos_t
TargetPosition at time of collision = TargetPos_t
ProjectilePosition at time 0, now = ProjectilePos_0
TargetPosition at time 0, now = TargetPos_0
Time to impact = t
Aim-angle = theta
My try:
Found a formular to calculate "drop" (Drop of the projectile based on the gravity) on Wikipedia:
float drop = 0.5f * gravity * t * t
The speed of the projectile has a horizontal and a vertical part.. Found a formular for that on wikipedia aswell:
ProjectilVelocity.x = projectilSpeed * cos(theta)
ProjectilVelocity.y = projectilSpeed * sin(theta)
So i would assume this is true for the projectile curve:
ProjectilePos_t.x = ProjectilePos_0.x + ProjectileSpeed * t
ProjectilePos_t.y = ProjectilePos_0.y + ProjectileSpeed * t + 0.5f * gravity * t * t
ProjectilePos_t.z = ProjectilePos_0.z + ProjectileSpeed * t
The target walk with a constant speed, so we can determine his curve by this:
TargetPos_t = TargetPos_0 + TargetSpeed * D3DXVECTOR3(t, t, t)
Now i dont know how to continue. I have to solve it somehow to get a hold on the time to impact somehow.
As a basic formular i could use:
float time = distanz / projectileSpeed
But that wouldnt be truly correct as it would assume a linear "Trajectory". We just find this behaivor when using a rocket.
I hope i was able to explain the problem as much as possible. If there are questions left, feel free to ask me!
Greets from germany,
Frank