shoot projectile from mesh "towards mousecursor"?

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.
Post Reply
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

shoot projectile from mesh "towards mousecursor"?

Post by suliman »

Hi

I have a spaceship and a chasecam behind it (finally almost got the chasecam to work properly!). The ship should be able to fire projectiles from its cannons (each cannon defined maybe as vect3d from spaceships mesh center)

The projectiles need to start moving (get velocity vector) "towards the mouse cursor" so the player can aim at things in front of him with the mouse and shoot at them. This is not the same as shooting in a fps where the player "is" the camera.

This is exactely what is done in games like freelancer and darkstar one. But ive no idea on how to do it.

Thanks for your help
(also email notification and avator upload doesnt work for me on this forum... Any tips?)
Erik
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

some pseudo code-

Code: Select all

vector3df gunPos; // position of gun nozzle
vector3df targetPos; // target position
f32 bulletSpeed; // speed of projectile
line3df fireLine; // a ray from the mouse position like in the collision example
if (collision with enemy nodes) // like in collision example
{
   // aim roughly where the ship will be when the bullet reaches it
   vector3df shipVelocity; // get velocity of target ship
   vector3df shipDistance = gunPos.getDistanceFrom(intersection);
   u32 intersectTime = (shipDistance / bulletSpeed).getLength();
   targetPos = intersection + (shipVelocity*intersectTime);
}
else 
   targetPos = fireLine.end;

bulletVelocity = (targetPos-gunPos).normalize() * bulletSpeed;

// now move the bullet by bulletVelocity each milisecond

using getDistanceSQ, getLengthSQ and having a squared bullet speed would be faster, but you can see the idea i hope.

oh and email notification and avatar uploads are turned off on all sourceforge hosted websites for security reasons. :(
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Post by suliman »

would this add a "aim here to hit the moving target" kind of feature? Otherwise i dont understand why target vel mattters.

Dont i have to specify some kind of range to where the mousecursor "is" in the 3d world? Otherwise how do i know what angle the bullet should leave the ship?

I must be able to fire into the air, not only if i have a collision already with a node.

Or am i completely off here :) This is considered (by me) as one of the hardest things in my game so far.

E
harukiblue
Posts: 49
Joined: Sun Dec 10, 2006 6:23 pm

I see

Post by harukiblue »

you are trying to find a way to have the users mouse at X distance from the barrel of the gun, in order to figure a vector to throw the projectile onto. Is the distance going to vary, posibly depending upon the desired target or max ranges of barrel of the gun? Or is it going to be a static distance from the barrel? Either way, I understand what you are trying to do, and it is simple (well maby not SIMPLE) math to figure out the projectiles vector, however a piece to that puzzle is the distance from the barrel and needs to be calculated 1st. Or at least as far as I understand the concept it is. Get back with us mate :wink:
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Post by suliman »

well thats a good question :)

with distance im not sure yet. I think there will be a fixed "considered distance" but maybe this must be edited along the way.

Can we for the sake of discussion just call it static float dist?
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

yes the pseudocode was an "aim here to hit the moving target" example. the "like in collision example" comments are relating to your original question, as the collision tutorial (tutorial 7) shows how to get a ray from the mouse position and project it into world space.
i tend to use a maximum lifespan for a projectile rather than distance, then when working out the ray just move its end-point by velocity*maxlifetime.
to get the start point, you'll just need to do "gunNode->getAbsoluteTransformation().transformVect(startPos)", where startPos is the nozzle of your gun.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply