Archived

This topic is now archived and is closed to further replies.

ParityCheck

Bullets don't shoot right?

Recommended Posts

I''m working on a simple 3d shooter game program. I have a createBullet class that creates an instance of a cube object which I''m for now using as a bullet, that goes in the direction of where my camera is currently facing. The problem is that my ''bullet'' only shoots in one direction no matter what direction I''m facing it just shoots in one freaking direction (not the direction I''m facing) I''m emulating the shooting by d3dxmatrixtranslate and I''m passign to the bullets shoot method the cameras currentposition and its target position and am incrementing the bullets position using. currentpos.x+=targetpos.x etc But still he keeps ''flying south '' no matter what direction I''m facing. How do I fix it so that my bullet flies in the direction of my camera target. I must be missing something, any suggestions guys

Share this post


Link to post
Share on other sites
The following code is the function what actually moves my bullet forward.

D3DXMATRIX matrixTranslateFront, matrixTranslateBack ,matrixMovement;

D3DXMatrixTranslation(&matrixTranslateFront, SourcePos.xPos+=TargetPos.xPos*2, -(SourcePos.yPos+=TargetPos.yPos*2),SourcePos.zPos+=TargetPos.zPos*2);//, i);
D3DXMatrixTranslation(&matrixTranslateBack, -(SourcePos.xPos+=TargetPos.xPos*2), SourcePos.yPos+=TargetPos.yPos*2, SourcePos.zPos+=TargetPos.zPos*2);//, i);
D3DXMatrixMultiply(&matrixMovement,&matrixTranslateFront,&matrixTranslateBack);

lpDevice->SetTransform(D3DTS_WORLD, &matrixMovement);
Bullet->RenderShape();

The variables SourcePos, TargetPos are passed to the object on construction in its constructor. SourcePos is the cameras position and TargetPOs is the cameras view direction.
Do u suppose that the targetPos variable isn''t being properly updated. My camera moves and rotates around perfectly. Just that it shoots in just one direction nomatter where I''m facing.
I coudl be facing south and pushing shoot and he''d still shoot north. What could be the problem ?

Share this post


Link to post
Share on other sites
Please say you don''t really have all those += in your arguments.

I''m not going to take the time to dissect what''s happening to those variables, and what''s being passed to the functions, with that code. I don''t imagine anyone else will either. Perhaps if you cleaned up the code you might spot your bug.

Share this post


Link to post
Share on other sites
Gee I mean actually I only want my bullets position to shift to its target position at each increment, what would be an easier way to do so. I do admit that code is well a ''little'' messy :D but I''d like some suggestions as to how to actually implement a shooting bullet. please

PS. should I mail just my bullet code..

Share this post


Link to post
Share on other sites
quote:

I'm not going to take the time to dissect what's happening to those variables, and what's being passed to the functions, with that code. I don't imagine anyone else will either. Perhaps if you cleaned up the code you might spot your bug.



Agreed, if you want us to debug your code you really need to refactor it into a more understandable format. Also it would help if you would use source tags; you can read about source tags in the FAQ at the upper right corner of the screen. Here is what you code looks like with source tags:


D3DXMATRIX matrixTranslateFront, matrixTranslateBack ,matrixMovement;

D3DXMatrixTranslation(&matrixTranslateFront, SourcePos.xPos+=TargetPos.xPos*2, -(SourcePos.yPos+=TargetPos.yPos*2),SourcePos.zPos+=TargetPos.zPos*2);//, i);

D3DXMatrixTranslation(&matrixTranslateBack, -(SourcePos.xPos+=TargetPos.xPos*2), SourcePos.yPos+=TargetPos.yPos*2, SourcePos.zPos+=TargetPos.zPos*2);//, i);

D3DXMatrixMultiply(&matrixMovement,&matrixTranslateFront,&matrixTranslateBack);

lpDevice->SetTransform(D3DTS_WORLD, &matrixMovement);
Bullet->RenderShape();


_______________________________________
Understanding is a three edged sword...


[edited by - Sean Doherty on November 14, 2003 2:26:26 PM]

Share this post


Link to post
Share on other sites
Ugh just email me, I am not at my home comp., this bullet stuff is babies work. I myself would cast a ray for it. But increment aint so bad. But your collision system will have to account for it, and assuming you can''t get this right, I am sure you dont have a working collisions system.

My camera works off just 2 vectors. A position vector and a normalized sphere rotate vector. So standing at (0,0,0) and looking at (0,1,0) will look up. Then you use your normalized (0,1,0) to create a vel vector for the bullet... which is incremented not by frame but by time... ugh why by frame!? I would run it into Opcode (check google) get a close AABB proximity and then do a sphere-tri collision for an exact point on the remaining. Toss on a decal and then you have a bullet hitting a wall.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
he sounds like he''s read too many tutorials and hasn''t written a line of what he''s being an asshole about.

Share this post


Link to post
Share on other sites
quote:

Ugh just email me, I am not at my home comp., this bullet stuff is babies work.



jimmynelson,

Nobody is saying that this is a hard problem to solve; we are just requested that the code be put in a more readable form prior to asking the question. As for your comment about this problem being baby stuff; what do you need emailed; if it is such an easy problem why not just give the answer of the top of your head?

PS: I read your profile, on October 13, 2003 you just learning how to create a d3d device using the app wizard. The following is a partial quote from your post:


quote:

I am having trouble getting a poitner working with the LPDIRECT3DDEVICE9. I use the appwizard and add the following code...





_______________________________________
Understanding is a three edged sword...

Share this post


Link to post
Share on other sites
Ok here is the camera code...


Vector cameraDirection;
Vector cameraPosition;

//setup camera matrix to the basics
D3DXMatrixIdentity(&m_matIdentity);
D3DXMatrixIdentity(&m_matView);
D3DXMatrixIdentity(&m_matPosition);

//using quaterions to set yall,pitch,roll even though it isn''t
//necessary it works mighty nice

D3DXQUATERNION qR;
D3DXQuaternionRotationYawPitchRoll(&qR, cameraDirection.y, cameraDirection.x, 0);

//transform
//affine with quaterion then inversed will return a usable
//view matrix

D3DXMatrixAffineTransformation( &m_matPosition, 1.0f, NULL, &qR, &cameraPosition );
D3DXMatrixInverse( &m_matView, NULL, &m_matPosition );

//last step is set your device to m_matView


Mix that with the velocity for the gun. So like

vector gunVelNorm=cameraDirection;

(camera direction should be normalized)

float gunVel=distance to move * time elasped since last frame

vector bulletPos+=gunVel*gunVelNorm;

I think that should be it...

Oh my trouble was with pointers through a gamestatecontroller. I figured it out that same day.

Hope this was helpful

Share this post


Link to post
Share on other sites
OK guys Here is what I''m like doing. Its a very crude method though and I seriously doubt that anyone actually implements

bullets like this. Actually I have 2 classes one is my cube class , all that does is setup vertices and the VBUffer and does

the rendering of the ''cube'' aka bullet.



The other class is my bullet class which actually does the shooting and has 3 methods.

1-a constructor which just does practically nothing but setup local variables.


CBullet::CBullet(LPDIRECT3DDEVICE8 alpDevice)
{
lpDevice = alpDevice;
MAX_RANGE = 100;

Bullet = NULL;
isLoaded = false;
};





2- a LoadBullet function that takes in the cameras CurrentPosition and TargetPosition and creates an object of a ''cube ''

class setting it to the cameras source position. This function is called when I push my ''fire'' key.



//call this method on push of button

CBullet::LoadBullet(float xPos, float yPos, float zPos, float xTPos, float yTPos, float zTPos)
{
TargetPos.xPos = xTPos; // Camera
TargetPos.yPos = yTPos; // View
TargetPos.zPos = zTPos; // Target

SourcePos.xPos = xPos; // Camera
SourcePos.yPos = yPos; // Position
SourcePos.zPos = zPos; //

Bullet = new CCube(lpDevice);
Bullet->CreateTexture("Texture.bmp");
Bullet->SetSize(5,5,12);

Bullet->SetPos(SourcePos.xPos, SourcePos.yPos, SourcePos.zPos);
isLoaded = true;
BulletState = LOADED;
Status->WriteText("The bullet is ready",0,10,0,450);


};




3 - A MoveBullet which actually is supposed to move the bullet in the direction of the target position via matrix

transformations and then call the cube object (that was created in the loadbullet func) and render it. This is like called in

my gameloops render function, I''ve set a boolean value that is set to true if my fire key is pressed i.e. my loadbullet

function is performed. This move bullet is then called and like goes on until a the game loop has like rendered a number of

times until when the bullet has traversed a certain distance and then the bullet objects clear method is called that resets

the boolean value and clears the bullets variables.


//Called continuously in the render loop
CBullet::MoveBullet(float i) //i is an incremented value passed to the function from the render loop
{
if (i < MAX_RANGE)
{
D3DXMATRIX matrixTranslateFront, matrixTranslateBack ,matrixMovement;


SourcePos.xPos+=TargetPos.xPos;
SourcePos.yPos+=TargetPos.yPos;
SourcePos.zPos+=TargetPos.zPos;

D3DXMatrixTranslation(&matrixTranslateFront,-(SourcePos.xPos), (SourcePos.yPos), (SourcePos.zPos));
D3DXMatrixTranslation(&matrixTranslateBack, (SourcePos.xPos), -(SourcePos.yPos), (SourcePos.zPos));
D3DXMatrixMultiply(&matrixMovement,&matrixTranslateFront,&matrixTranslateBack);

lpDevice->SetTransform(D3DTS_WORLD, &matrixMovement);
Bullet->RenderShape();
isLoaded = true;
}
else
{
Bullet->CleanThis();
Bullet = NULL;

isLoaded = false;
}

};



It is noteworthy that until the bullet object reaches its end I can''t shoot another bullet.

The result however is that my dumb bullet flies in just one direction no matter where I am facing when I make the shot.

This is a totally crappy method and I am beginning to realise that just by writing this post. Still there''s my source code.

It a little too spagetti for an object oriented one. I would appreciate help in this respect or if possible another easier more practical way to implement this. I have just begun getting my feet wet in directx 8 .

Share this post


Link to post
Share on other sites
Here''s some pseudocode. Note that I''m typing it quickly and so I''m not saying this will work flawlessly and niether that this is the best way to do it. Nevertheless, it should be able to help you a little:


class Bullet
{
D3DXVECTOR3 Start; // Position from where the bullet was shot
D3DXVECTOR3 End; // Point the camera was looking at
D3DXVECTOR3 Dir; // Bullet direction
float T; // Factor to travel along the trajectory
D3DXVECTOR3 Position; // Current bullet position
float Speed; // Bullet speed
float MaxDistance; // Maximum distance the bullet should travel

... etc ...
};

Bullet::Shoot()
{
Start = GetCameraPosition();
End = GetCameraLookAtPoint();
Dir = End - Start;
D3DXVec3Normalize(Dir);
T = 0;
}

bool Bullet::Update(float ElapsedSeconds)
{
T += ElapsedSeconds * Speed;

// Make sure the bullet doesn''t go on for ever
if (T > MaxDistance)
return false;

Position = Start + T * Dir;

return true;
}

Bullet::Render()
{
// Render bullet mesh at Position
}


I hope this helps you.

Regards,


Mariano Ruggiero
Lead Programmer
ONIRIC GAMES

Share this post


Link to post
Share on other sites
Ok, from your last post:

You update your position by target. Fine.
Then you build a matrix to translate x, y, z.
Then you build a matrix to translate -x, -y, z.
You then multiply these matrices, with a result of translating by 0, 0, 2z.

Why are you building two translation matrices, one almost the opposite of the other? I think the first matrix by itself is probably what you''re trying to achieve.

Share this post


Link to post
Share on other sites

SourcePos.xPos+=TargetPos.xPos;
SourcePos.yPos+=TargetPos.yPos;
SourcePos.zPos+=TargetPos.zPos;


I believe that part of your problem is here? Why are you adding the target position to the source position? You want to calculate the vector between the source and target and move the bullet in the direction of the vector.

I also don't understand why you have two matrixes:


D3DXMatrixTranslation(&matrixTranslateFront,-(SourcePos.xPos), (SourcePos.yPos), (SourcePos.zPos));
D3DXMatrixTranslation(&matrixTranslateBack, (SourcePos.xPos), -(SourcePos.yPos), (SourcePos.zPos));
D3DXMatrixMultiply(&matrixMovement,&matrixTranslateFront,&matrixTranslateBack);


You basically have to:

1. increment the bullets position by a calculated vector
2. D3DXMatrixTranslation(&matTranslation, vecPosition.x, vecPosition.y, vecPosition.z);
3. lpDevice->SetTransform(D3DTS_WORLD, &matTranslation);

I think that is it pretty much all you need, given you don't have any other translations.

_______________________________________
Understanding is a three edged sword...


[edited by - Sean Doherty on November 14, 2003 11:01:36 PM]

Share this post


Link to post
Share on other sites

T += ElapsedSeconds * Speed; // Make sure the bullet doesn''t go on for ever
if (T > MaxDistance)
return false;


Interesting, comparing your maximum distance as a scalar produces less code.

_______________________________________
Understanding is a three edged sword...

Share this post


Link to post
Share on other sites
I think the variable name TargetPos is kind amisleading as the variable being passed to the function is not actually the

target point of the camera. Here is the code that is invoking the load bullet function describes earlier

//on keypress fire

aBullet->LoadBullet(CurrentPos.xPos,CurrentPos.yPos,CurrentPos.zPos, CameraLookPos.x, CameraLookPos.y, CameraLookPos.z);



The currentPos is the currnet position & the cameralookpos is actually whats being passed. The camera look pos is constantly

changed in this piece of code in my setupcamera function


D3DXMATRIX matrixCamera;
D3DXVECTOR3 CameraPos(CurrentPos.xPos, CurrentPos.yPos, CurrentPos.zPos);

LookPos.x = (float)sin(LookAtAngle);
LookPos.y = sinf(LookAtUpAngle);
LookPos.z = (float)cos(LookAtAngle);



CameraLookPos = LookPos + CameraPos; //add the two vectors
D3DXMatrixLookAtLH(&matrixCamera,&CameraPos, &CameraLookPos, &D3DXVECTOR3(0.0f,1.0f,0.0f)); //Which way is Up???


lpDevice->SetTransform(D3DTS_VIEW, &matrixCamera);

D3DXMATRIX matrixProject;

//setup the field of view, aspect ratio, near clipping plane, far clipping plane
D3DXMatrixPerspectiveFovLH(&matrixProject, D3DX_PI/4,1.0f,1.0f,500.0f);

//apply the transformation
lpDevice->SetTransform(D3DTS_PROJECTION, &matrixProject);

//Set Z buffering enabled
lpDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
//set lighting
lpDevice->SetRenderState(D3DRS_LIGHTING, TRUE);



About using 2 translation matrices I myself don''t get it as to why 2 matrices are needed. by using just one matric my bullet

just hangs around in midair until the loop terminates. Actually on pressing the fire key and invoking the code above the

following function in my game renderloop is initiated i.e. this is where my bullet actually moves.


//RENDER GAME LOOP

if(aBullet->IsLoaded())//GetBulletStat()==LOADED)//if the bullet is loaded
{ // Shoot
i++;
aBullet->MoveBullet(i);
}



the bullet actually moves on each loop of the render game function. The isLoaded function returns true only after the fire

button has been pressed. The parameter i being passed to the movebullet function is just an increment on the number of

iterations the loop has made. upon reahing the maximum iteration the bullet is removed and variables cleared to original.

this sorta started off as a buildup on some tutorials. The bullet is actually dependent on the render loop for the ''chaneg in

distance'' if I may be allowed to say so . for some reason could the problem be in the render loop on the setcamera function

...

Share this post


Link to post
Share on other sites
uh yeah that too so any suggestions guys. actually I was wondering whether I'm making a goof up in the lookpos and cameralookpos vector variables or so. It seems like a simple problem gone to herculean proportions. I really do need to get this working guys any suggestions please

[edited by - ParityCheck on November 15, 2003 1:44:49 AM]

Share this post


Link to post
Share on other sites
I've tried my best to make my bullet move in the direction its aimed at but nothing seems to work.

What could be the problem in my code guys? Its posted on the previous page.

I'd like to know if there are any other techniques on implementing this. Or rather what would be the easiest and sure fre way of implementing bullets shooting in using DX8 and VC6. I'd be ready to start this bullet shooting from scratch. help and suggestion would be most greatly appreciated guys

[edited by - ParityCheck on November 15, 2003 4:37:35 AM]

Share this post


Link to post
Share on other sites
quote:
I''d like to know if there are any other techniques on implementing this. Or rather what would be the easiest and sure fre way of implementing bullets shooting in using DX8 and VC6. I''d be ready to start this bullet shooting from scratch. help and suggestion would be most greatly appreciated guys


Have you looked at the pseudocode I posted?


Mariano Ruggiero
Lead Programmer
ONIRIC GAMES

Share this post


Link to post
Share on other sites