Shooting - line3df
Shooting - line3df
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?
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?
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! )
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! )
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.
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...
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!
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!
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.
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 likeCode: Select all
Welcome to the world of programming.
-
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
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...
-
- Posts: 322
- Joined: Tue Aug 30, 2005 10:34 am
- Location: slovakia
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
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...
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 :spfo 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...
-
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
Ah, ok, I missed that part. I don't know, but maybe this would work: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?
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.