Shooting - line3df

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Shooting - line3df

Post by JP »

I'm trying to improve my shooting code from the code in the tech demo but i'm having difficulty.

The code in the tech demo is perfect for shooting non moving objects, but if the object is moving then it just detects the point at which the collision would happen at when the projectile is fired, which isn't necessarily the same point as the projectile will collide with when it gets there, ie the enemy may have moved out of the way, so the projectile should keep going until it hits say a wall.

So i've been trying to do this so that when a projectile is fired it gets added to an array which is checked through each frame to see if it will actually collide in that current frame with either a wall or an enemy.

I have a line of flight for the projectile. But i'm actually having problems here. basically if i want to shoot at a certain enemy then i will shoot at a certain 3D coord, my target; the enemies location. So i could create a flyStraightAnimator which will fire a projectile to that point. But what if the enemy has moved by the time to projectile gets there? The flyStraighAnimator would not make the projectile continue on until it hit something else, so that's what i want to know how to do.

how can i create a long enough line3df so that the projectile will actually go on and on until it hits something (my environment is enclosed, and limited so there's no chance that it won't ever hit anything). I've tried doing some stuff with equations of lines etc. but i couldn't get it working properly.

So having a start point and a point i want the line to pass through, how i can i make a line of say length 5000?
Image Image Image
A.Russell
Posts: 56
Joined: Sun May 15, 2005 2:52 pm

Post by A.Russell »

I am not sure that I understand your question.

Do you want to fire a missile so that it will collide with a target object provided it maintains a constant velocity ? If so, I have an algorithm for you, though I wrote it for another engine in a c-script so it will require a little bit of conversion.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yeah that's it i think..

I have a point i want to fire a projectile from. And i target that projectile is shot towards. And if the enemy that is being shot at doesn't move then the projectile will collide with that enemy, but if the enemy moves out the way the projectile will just continue on until it hits something else.

Anything you've got that can help with that would be great!

(c-script, whatever that may be, will need alot of conversion i should think as i'm using java! :lol: )
Image Image Image
Andreas
Posts: 166
Joined: Sun Oct 31, 2004 7:15 am
Location: Münster / Germany
Contact:

Post by Andreas »

Couldn't you just do collision detection with walls and use the resulting point in a FlyStraightAnimator (i.e. ignore players).

Then test every frame if the bullets in your array hit a player (two nested for loops) and if they hit explode at the current (the player's) position.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yeah i thought of doing that actually. And it might be a good way to do it, but i wasnt sure about how to deal with the impact effect with the wall... As in the tech demo the impact is queued when the shot is made, but then if the projectile hits an agent that impact with the wall wont be required so will have to be removed somehow, but im sure i can get a way to do that.
Image Image Image
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Oh well i still need to know a good way to get the line for the projectile to fly along which will go from the start point, through the target (enemy position) and then hit into a wall. I had some code to do that but it wasn't doing it properly at all. It was based on the y = mx+c equation of a line. is there something in Irrlicht which will allow me to define a line based on a start point and a mid point, and then no defined end point... probably not...
Image Image Image
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Here's what i've got currently:

vector3df end = target;
vector3df start = getFiringPosition();
float grad = (end.getZ() - start.getZ()) / (end.getX() - start.getX());
float c = -(grad*start.getX())+start.getZ();
line3df line = new line3df(start, new vector3df(5000, end.getY(), -(grad*5000+c)));

That uses the y = mx+c equation of a line, should work ok as my agents are only shooting horizontally, so it's basically 2 dimensional.

The problem is that most of the time the agent ends up shooting backwards for some reason, but sometimes forward... When surely it should stay in the same direction atleast :s. Also for one agent it shoots straight through the other agent (what i want, that's good) but for another agent he's always shooting at an angle to the left :s

This is doing my head in and i don't have long to get my project finished and can't spend ages messing around with stuff like this as i've got loads of AI i need to implement. I'm just so stuck on this and am making no progress! :cry:
Image Image Image
A.Russell
Posts: 56
Joined: Sun May 15, 2005 2:52 pm

Post by A.Russell »

Ihave to go out to work very soon, so just a quick description of my method of targeting:

Take the velocity vector of the missile,
rotate it to point at the target
add the velocity vector of the target to the result

Your vector will now point in the direction to launch your missile, convert it to an angle and shoot.

If the target object continues at the same velocity, the two objects will collide.

This is doing my head in and i don't have long to get my project finished and can't spend ages messing around with stuff like

Code: Select all


Welcome to the world of programming.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Ok cheers for that, something to consider atleast! I might just have to give it a rest and try and get some other stuff done and then come back to it later.
Image Image Image
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

Here's another idea, once that might prove less computationally expensive. When you fire a bullet, instantly do a raytest to determine if it will hit, if it doesn't, ignore it. If it does, calculate in how much time it will take before the bullet gets to its destination and flag that time in some event data structure. Each frame, decrement each of these 'hit' times until they run out (shouldn't take long). When they run out of time, do another test to see if whatever was hit is still there and apply damage, otherwise delete the bullet. It may be a little harder to set this up, but at least you aren't checking every bullet every frame. Plus, in the near-miss cases you could do a special effect (like near miss sound effect, etc...). Just an idea...
needforhint
Posts: 322
Joined: Tue Aug 30, 2005 10:34 am
Location: slovakia

Post by needforhint »

I would use movingEnemyNode->getAbsolutePosition() vector and yourcamera->getTarget() vector and use
irr::core::vector3d< T >::dotProduct ( const vector3d< T > & other ) const
or
vector3d<T> irr::core::vector3d< T >::crossProduct ( const vector3d< T > & p ) const
or
bool irr::core::vector3d< T >::isBetweenPoints ( const vector3d< T > & begin,
const vector3d< T > & end
) const [inline]
and use some arounding acception.... I teka exam from linear algebra tommorow :(
what is this thing...
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

pfo wrote:Here's another idea, once that might prove less computationally expensive. When you fire a bullet, instantly do a raytest to determine if it will hit, if it doesn't, ignore it. If it does, calculate in how much time it will take before the bullet gets to its destination and flag that time in some event data structure. Each frame, decrement each of these 'hit' times until they run out (shouldn't take long). When they run out of time, do another test to see if whatever was hit is still there and apply damage, otherwise delete the bullet. It may be a little harder to set this up, but at least you aren't checking every bullet every frame. Plus, in the near-miss cases you could do a special effect (like near miss sound effect, etc...). Just an idea...
Cheers, that sounds good... But i still have the problem of making the extended line of flight for the projectile, so that it starts at the start point, goes through the target point and then hits the first wall it comes into contact with. I just can't get that bit working at all :s
Image Image Image
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

So having a start point and a point i want the line to pass through, how i can i make a line of say length 5000?
Ah, ok, I missed that part. I don't know, but maybe this would work:

asuming start point is a vector, and the point you want to pass through is a vector:

unit vector from start to end would be:
Vector3 dir = (end - start).normalize();

To make a line of length 5000.0 in that direction, do:
line.end = line.start + (dir * 5000.0);

That should work... I think.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Ok, what would dir be? Wouldnt that have to be a vector? Or what value is it?

EDIT: Doh, just read your reply again and seen what dir is, and it IS a vector, but how can dir * 5000 work then?
Image Image Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Multiplying a vector by a scalar is the same as multiplying each component of the vector by that same scalar. You end up with a vector that is 5000 times longer than the length of dir, which happens to be 1 unit long.
Post Reply