Predict target location at projectile's arrival

hi all.
Im trying to figure out, how to predict needed projectile direction, so two moving objects can fire and hit each other. its sometime frustrating, more precisely now… i have this code, all needed stuff exist (tryfindrigidbody actualy finds it)

[java]
/**
* Predicts location of target in future.
* @param collision
* @param useContactPoint use contact point instead of using geometry location
* @return
*/
protected Vector3f predictLocation(CollisionResult collision, boolean useContactPoint) {
Vector3f contact = useContactPoint ? collision.getContactPoint() : collision.getGeometry().getWorldTranslation();
float distance = useContactPoint ? collision.getDistance() : NodeHelper.getLocalTranslation(collision.getGeometry(), parent).length();
// Get my velocity
Vector3f linearVelocity = parentRigidBody.getLinearVelocity();
// Try to get target’s velocity
RigidBodyControl rb = tryFindRigidBody(collision.getGeometry(), 3);
if(null != rb) {
linearVelocity.addLocal(rb.getLinearVelocity().subtract(linearVelocity));
}
// Compute needed lead offset
Vector3f movingOffset = linearVelocity.mult(distance / (linearVelocity.length() + projectileSpeed));
// Get predicted direction
Vector3f predicted = contact.add(movingOffset);
return predicted;
}

/**
 * Tries to find rigid body control.
 * @param geometry
 * @param iterations
 * @return 
 */
protected RigidBodyControl tryFindRigidBody(Geometry geometry, int iterations) {
    RigidBodyControl rb = null;
    Spatial child = geometry;
    for(int i = 0; i < 3; i++) {
        rb = child.getControl(RigidBodyControl.class);
        if(null != rb) {
            break;
        }
        child = child.getParent();
    }
    return rb;
}

[/java]

im out of ideas what could be wrong :frowning:
ascaria

I wouldn’t throw physics objects to simulate arrows to begin with. If you use your own algorithm for the trajectory and only check for collisions with ray tests you can use the same algorithm to predict where the arrow will hit.

If you calculate out all the variables and solve for x at time t there would be no guarantee that this is how bullet itself will simulate the scenario.

the only way you could do this is to actually throw an arrow in a bullet physics world under the same conditions and see where it goes. kind of like how the military fires a test round on their artillery before firing “for effect”

Yeah, and thats completely over the top for arrows in a game.

im using rigid bodies of player and enemy to detect their linear velocity only.
distance is between enemy is from players node and enemys geometry
aiming is from barrel which is non-phy node at enemys contact point or geometry

i managed to get fairly good accuracy, but with increased relative speed (and not distance), projectiles begin to miss always the same. every projectile miss for example 1 meter to the left if two objects move fast relatively to each other. but if both objects flies for example 2000kmh in ± same direction, accuracy is good.

so i thought i can do some scattering so every projectile flies in slightly different path. so this imaginary scattering can be done with slightly adjusting movingOffset’s xyz lets say 5% randomness

this has drawback that it can miss stationary object, which now is being hit at 100% ratio. so maybe i can calculate relative linear velocity of two objects (lets say 1% of this velocity) and use it as some randomness multiplier, so more the same is velocity, more precise will be aiming

what do you think? :slight_smile:

edit: or maybe i need to add iterpolation ammount between target’s physloc and modelloc (and also my ship’s) to the predicted location, because projectiles aims behind the target

edit: or more simply, enemyLinearVelocity.multLocal(1.05f);

I still think you should just calculate the trajectory yourself, you can decide how fine grained you make the in between ray tests then. Real arrows don’t behave like thrown stones anyway.

i have difficulties to understand you normen :slight_smile: even if i just calculate trajectory myself (i dont know how to do it) i need to predict contact point either. and what you mean by multiple “ray tests” and not only one “ray test”

i think to do things this way:

laser beam: simple glowing cylinder (for example 1000wu in length) and ray every update “inside it”, and check if it hits anything which distance is x < cylinderLength

turret firing projectile: projectile is simple geometry with some control attached to it which is changing translation every update. also fire ray at every step and if ray hits anything between two steps, collision occurs, also do collideWith on projectile itself to test if projectile is overlapping enemy geometry

missile launch: physics with geometry guided by applying forces, so same as ship

a missile and arrow rely on air to guide them. They point in the direction they are moving because of their fins flying through air.

Bullet would simulate that. itll treat your objectives more like asteroids hurling through space.

i actually was in the same situation once without thinking…

in this video i had missiles launched through use of bullet physics: - YouTube

note that that missiles only point straight up or straight down, and to change orientaton i manually flip them using a timer so they appear to be going down at the right time. also they just randomly splatters on the screen because i had no way to aim them.

in this video i switch from physics to using motion paths:

i dont have to predict where they land, i tell them where to land by setting the last waypoint on the motion path… i can create whatever flight path is needed (in this case lots of interesting patterns). and they arch with the curve of the flight path so i dont have to manually orient them. (also i dont think i can launch 1000 missiles in physics and expect any kind of performance)

from experience let me say you dont want to use physics for this.

these small turrets can rotate and fire unguided projectiles, i dont know why you all suggesting not to predict target location. i simply wants to “fire and forget” in right angle

im still using bombcontrol for projectiles as it was in tutorial

were not saying you shouldnt predict it the location. were saying the projectile shouldnt be simulated in the bullet physics world. instead you should use some non physics ways to calculate its trajectory.

besides not being able to know where the projectile will go. you’ll probably also have clipping issues if its too fast and things like that.

I was once working on targeting for AI.
I found this guide pretty useful:
http://www.gamasutra.com/blogs/KainShin/20090515/83954/Predictive_Aim_Mathematics_for_AI_Targeting.php

It lays out a few different targeting approaches. Some of which can account for basic gravity or other constant acceleration effects.

1 Like
@Ascaria said: i have difficulties to understand you normen :) even if i just calculate trajectory myself (i dont know how to do it) i need to predict contact point either. and what you mean by multiple "ray tests" and not only one "ray test"

i think to do things this way:

laser beam: simple glowing cylinder (for example 1000wu in length) and ray every update “inside it”, and check if it hits anything which distance is x < cylinderLength

turret firing projectile: projectile is simple geometry with some control attached to it which is changing translation every update. also fire ray at every step and if ray hits anything between two steps, collision occurs, also do collideWith on projectile itself to test if projectile is overlapping enemy geometry

missile launch: physics with geometry guided by applying forces, so same as ship

Its not hard to understand.

You calculate the trajectory: Trajectory - Wikipedia

Then you make a ray test on the first small part of the trajectory in the first frame, then in the second frame the next small part etc. In effect you then get a simulation of an arrow that moves in a certain time along the trajectory and checks for collisions in between. If you did only one ray check the arrow would be infinitely fast, arriving within one frame at the target.

@normen
so you mean something like this:
projectile geometry with some ProjectileControl attached

  • quaternion rotation and vector3f “linear velocity” is given
  • every control update, move projectile by translation.add(linVel)
  • every control update fire raycast at direction and check if anything collided within distance between two steps

if i understand good, this may be your suggested solution

i have related question to Ray.java, i saw some limit i it, so it means i can limit ray’s length so it is not infinite ?

@FuzzyMonkey
i will definitely try this link :slight_smile:

Think it as a function, not as a stepped movement, you said you wanted to be able to predict the result and you go down a road where you have to run a simulation again.

@normen i read all your posts again and i think you suggesting me to use same algorithm for predicting location and actually simulate predicted projectile flight so two house-flies by one hit and it guarantes that if enemy doesnt change velocity, predicted location will be hit location everytime without error, because it is same algorithm. it is definitely best solution, but i dont know if im capable of doing it, i cant imagine how to solve it, it seems to be a bit hard for me. i think for starters i use something simpler and try to implement your way in some code-enhancing time.

thanks much for efforts of all of you :slight_smile: