DirectX First Person Shooter Projectile?

neonet
01-02-2006, 03:42 PM
Hello,
I have a first person camera setup and am wondering how do I make a projectile fire at the direction/angle the camera is facing at the center like in most first person shooters? :confused:

Iceplug
01-02-2006, 04:09 PM
I think you missed the DirectX forum which is above... but, you should have variables that indicate the location and the direction of the camera.

A very simple trigonometric way of telling your camera where its three vectors should be can be done by:
From.X = yourXlocation
From.Y = yourYlocation
Andto.X = From.X + Cos(yourangle)
Andto.Y = From.Y + Sin(yourangle)

You will already have these angles at your disposal.
So, to make a projectile fire (assuming you know how to create one), you just start it off at your camera's andto point (if you don't want it to hit yourself) or the from point, and give it an angle the same as yours, along with some speed.
The velocity in each direction, also very simple trig, can be calculated identically:
Vx = Speed * Cos(thisangle)
Vy = Speed * Sin(thisangle)

You then move the projectile accordingly:
ProjX += Vx * TimeChg
ProjY += Vy * TimeChg
:)

Where TimeChg is the amount of time between frames... you can either use Environment.TickCount or just set it to 1 for fixed movement.

neonet
01-02-2006, 08:17 PM
I tried what you mentioned and still can't get it to work.

blnProjectileFire = True when I press fire button, for starters I want to adjust the projectile vector each time the fire button is pressed.

Also, the concept is 3D first person shooter , the camera is fully rotatable, so how do I handle the Z axis?

The example below shows what the code I'm using looks like in the render block:

'MOVE PROJECTILE.
If blnProjectileFire = True Then
'GET PROJECTILE VECTOR DESTINATION.
vecProjectile.X = vecProjectileStartPos.X + Cos(CameraVectors.vecCameraLook.X)
vecProjectile.Y = vecProjectileStartPos.Y + Sin(CameraVectors.vecCameraLook.Y)

'MOVE PROJECTILE.
vecProjectile.X += vecProjectileStartPos.X * 1
vecProjectile.Y += vecProjectileStartPos.Y * 1

blnProjectileFire = False
Else
'GET CURRENT CAMERA POSITION AND SET AS PROJECTILE START.
vecProjectileStartPos.X = CameraVectors.vecCameraEye.X
vecProjectileStartPos.Y = CameraVectors.vecCameraEye.Y
End If

I think you missed the DirectX forum which is above... but, you should have variables that indicate the location and the direction of the camera.

A very simple trigonometric way of telling your camera where its three vectors should be can be done by:
From.X = yourXlocation
From.Y = yourYlocation
Andto.X = From.X + Cos(yourangle)
Andto.Y = From.Y + Sin(yourangle)

You will already have these angles at your disposal.
So, to make a projectile fire (assuming you know how to create one), you just start it off at your camera's andto point (if you don't want it to hit yourself) or the from point, and give it an angle the same as yours, along with some speed.
The velocity in each direction, also very simple trig, can be calculated identically:
Vx = Speed * Cos(thisangle)
Vy = Speed * Sin(thisangle)

You then move the projectile accordingly:
ProjX += Vx * TimeChg
ProjY += Vy * TimeChg
:)

Where TimeChg is the amount of time between frames... you can either use Environment.TickCount or just set it to 1 for fixed movement.

Iceplug
01-03-2006, 05:08 AM
You're not giving me a lot of information to go on, so I'm just going to give you stuff that would work in my project:

If your Yaxis is up, then you need to switch my Y with my Z.

For doing the full 3D look, you need two angles (I use spherical coordinate angles)
So you have two angles: one is for turning and one is for looking (up/down)
The turnangle is like before: turning around on the X and Y axis.
The lookangle, when positive, is looking up the Z axis and, when negative, is looking down the Z axis.

Your positions remain unchanged (and you can handle Z with changes in elevation by yourself), but your camera looking direction changes a tad with slightly more challenging trig:
Andto.X = From.X + Cos(yourangle) * Cos(lookangle)
Andto.Y = From.Y + Sin(yourangle) * Cos(lookangle)
Andto.Z = From.Z + Sin(lookangle)
The same formula will apply to the projectile velocity: simply follow our pattern.

Everything else remains unchanged (aside from following the other pattern for supporting the Z direction in speed and projectile movement).

neonet
01-03-2006, 11:40 AM
Well I still couldn't get your method to work for a moving camera, so I went ahead and tried to use same method as is used to move the camera eye position based on the LookAt with some subtraction and this is what it looks like:

vecProjectile.X -= CameraVectors.vecCameraLook.X * -moveSpeed * elapsedTime
vecProjectile.Y -= CameraVectors.vecCameraLook.Y * -moveSpeed * elapsedTime
vecProjectile.Z -= CameraVectors.vecCameraLook.Z * -moveSpeed * elapsedTime

Or it can be written as such:
vecForward = CameraVectors.vecCameraLook 'Set forward vector when ready to fire. (SKIP THIS ONCE FIRE BUTTON IS PRESSED AND CONTINUE TO THE CODE BELOW.)

vecForward.Multiply(-moveSpeed * elapsedTime) 'Set the amount to move projectile.
vecProjectileCurrentPos.Subtract(vecForward) 'Move projectile forward/outward.

Another method which someone on a Microsoft forum mentioned was just use ray intersection, use the position and lookat vectors and subtract the position from the lookat to produce the ray then do intersection test on mesh objects, I already know how to do that and wanted to send a visible projectile from the very center of a crosshair, so the formula above works.

It is crude and jerky so I'll have to adjust the scale and other things to smooth out the overall look of the movement outward, also, the vecCameraLook will need to be changed to a temporary projectile vector otherwise when I move the camera the projectile will move towards the center of the camera's crosshair.

That's it for now unless I come accross some other issue's.

Thanks for the help. :D


You're not giving me a lot of information to go on, so I'm just going to give you stuff that would work in my project:

If your Yaxis is up, then you need to switch my Y with my Z.

For doing the full 3D look, you need two angles (I use spherical coordinate angles)
So you have two angles: one is for turning and one is for looking (up/down)
The turnangle is like before: turning around on the X and Y axis.
The lookangle, when positive, is looking up the Z axis and, when negative, is looking down the Z axis.

Your positions remain unchanged (and you can handle Z with changes in elevation by yourself), but your camera looking direction changes a tad with slightly more challenging trig:
Andto.X = From.X + Cos(yourangle) * Cos(lookangle)
Andto.Y = From.Y + Sin(yourangle) * Cos(lookangle)
Andto.Z = From.Z + Sin(lookangle)
The same formula will apply to the projectile velocity: simply follow our pattern.

Everything else remains unchanged (aside from following the other pattern for supporting the Z direction in speed and projectile movement).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum