Hi, I am making a first person shooter in directX 10 using c++.
I would post my code for help, because I know this is standard practice here, but it is simply too much to put on here. Anyway I don’t know if i’ll be able to get what I’ve got working as the bullets are just going in stupid directions starting from next to the gun.
My question here is, does anyone know how to just make a bullet move towards crosshairs?
I have crosshairs in front of the gun (there actually just cube objects in the shape of crosshairs).
Does anyone know a way in directX10 using c++ to make something just move towards a point at a given speed because surely I can just make my bullets move towards the crosshair and then not have to worry about the code I have at the moment which basically is determining which way the gun is facing and coming out at that angle (which it isnt working properly at all).
I figure if there is a way of making the bullet move towards a specific point then that’s far easier than what I’m doing now.
I know this wasn’t particularly clear, but any help would be much appriciated as I am properly stuck on this.
Here’s the function for bullet direction:
void BDirection(float speed){
float frameTime = 0.01;
initVecDir.x = 0;
initVecDir.y = 0;
initVecDir.z = 1;
D3DXVec3TransformCoord(&newVecDir, &initVecDir, &matAllRotations2);
D3DXVec3Normalize(¤tVecDir, &newVecDir);
float adjustedSpeed = speed*frameTime;
for (int i =0; i<CountB; i++){
BULLETS[i].initVec.x += currentVecDir.x*adjustedSpeed; BULLETS[i].initVec.y += currentVecDir.y*adjustedSpeed;
BULLETS[i].initVec.z += currentVecDir.z*adjustedSpeed;
}
}
bullet render:
for(int i =0; i< CountB; i++){
D3DXMatrixRotationX( &matBulletRotX, BULLETS[i].RotVec.x);
D3DXMatrixRotationY( &matBulletRotY, BULLETS[i].RotVec.y);
D3DXMatrixRotationZ( &matBulletRotZ, BULLETS[i].RotVec.z);
D3DXMatrixScaling(&matScale2, 1,1,1);
D3DXMatrixTranslation (&matTranslate2, BULLETS[i].initVec.x, BULLETS[i].initVec.y, BULLETS[i].initVec.z);
matAllRotations2 = matBulletRotX * matBulletRotY * matBulletRotZ;
matBulletWorld = matAllRotations2 * matScale2 * matTranslate2;
mWorldViewProjection = matBulletWorld * mView * mProj;
// Update the effect's variables.
V( g_pWorldViewProjection->SetMatrix( (float*)&mWorldViewProjection ) );
V( g_pWorld->SetMatrix( (float*)&matBulletWorld ) );
V( g_pTime->SetFloat( (float)fTime ) );
V( g_pCameraPosition->SetFloatVector( (float*)g_Camera.GetEyePt() ) );
for ( UINT iSubset = 0; iSubset < BULLETS[i].bullet.GetNumSubsets(); ++iSubset )
{
RenderSubset2( iSubset, i );
}
}
You need to determine the vector that the user is looking at, depending on what type camera you used, this would be the look-at vector. This becomes the velocity of the object. You also need to rotate the object to match this if it can be seen.
If you are having the bullets originate from the gun itself, you need to determine where the cross-hairs actually point at to compensate for the difference between what the users sees and where the bullet will end up. I would suggest looking at the “Pick” sample from the DirectX SDK.
[Edit] I rewrote it after reading you question again, sorry.