I'm writing a simple shoot em up, before starting this a couple of weeks ago, I had no experience of using directx so bear with me :) I'm using Visual C + + .NET
In the DisplayItem method, I added a matrix for translation and a matrix for scaling, which are then combined for the world matrix. When i press a movement key, like the down arrow to move the ship down, the ship graphic doesnt move, it does get animated though. Then when I press spacebar, the ship and bullet graphic fly off together. When I hit space again they both return to where my ship should be and fly off again.
I'm totally stuck, I spent yesterday and a few hours today searching for the solution I need. Is it something to do with identity matrices? I looked them up but do not understand their purpose properly, and adding it to my code(probably incorrectly) just caused weird results. If you could tell me the answer I need with a little explanation I would be very pleased.
I have a class for creating and putting graphics on screen:
class CreateNewScreenItem
{
private:
float positionX; // controls x position on screen.
float positionY; // controls y position on screen.
float positionZ; // controls z position on screen.
float scaleX; // controls the scale on x axis
float scaleY; // controls the scale on y axis
int ImageSizeX; // size of the image on the x axis
int ImageSizeY; //size of the image on the y axis
int MaintainAspectRatio;
float speed;
int Direction;
bool ShowOnScreen;
int animation; //(0) no animation, (1)2 images for animation, (3) 3 images for animation;
int animatedcycle;
LPDIRECT3DTEXTURE9 D3DTextureItem[12]; //Create new instance of a Texture
public:
CreateNewScreenItem(float ipositionX, float ipositionY, float ipositionZ, float iscaleX, float iscaleY, float iImageSizeX, float iImageSizeY, int iMaintainAspectRatio, float ispeed)
{
positionX=ipositionX;
positionY=ipositionY;
positionZ=ipositionZ;
speed = ispeed;
MaintainAspectRatio = iMaintainAspectRatio;
ImageSizeX = iImageSizeX;
ImageSizeY = iImageSizeY;
scaleX = iscaleX;
scaleY = iscaleY;
if (MaintainAspectRatio == 1){
scaleX = scaleX * (ImageSizeX * 0.75) * 0.07;
scaleY = scaleY * ImageSizeY * 0.07;
}
ShowOnScreen = true;
}
public:
void MoveDown()
{
positionY= positionY - float(pixel*speed) ;
Direction = down;
animatedcycle++;
}
void NoMove()
{
Direction = down;
animatedcycle++;
}
void SetPositionX(float X_temp)
{
positionX=X_temp;
}
void SetPositionY(float Y_temp)
{
positionY=Y_temp;
}
float GetPositionX(void)
{
return positionX;
}
float GetPositionY(void)
{
return positionY;
}
void Shoot(float TempX, float TempY, int WhoFired, float iSpeed)
{
SetPositionX(TempX);
SetPositionY(TempY);
SetSpeed(iSpeed);
}
bool getShowOnScreen(void)
{
if (ShowOnScreen==true)
return true;
else
return false;
}
void setShowOnScreen(bool show)
{
ShowOnScreen=show;
}
void LoadImages(char file[],char file2[],char file3[],char file4[])
{
D3DXCreateTextureFromFile(DX_structure.D3DDevice , file ,&D3DTextureItem);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file2,&D3DTextureItem);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file3,&D3DTextureItem[up]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file4,&D3DTextureItem[down]);
animation = 0;
}
void LoadImages(char file1a[],char file1b[],char file1c[],char file2a[],char file2b[],char file2c[],
char file3a[],char file3b[],char file3c[],char file4a[],char file4b[],char file4c[])
{
D3DXCreateTextureFromFile(DX_structure.D3DDevice , file1a ,&D3DTextureItem);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file2a,&D3DTextureItem);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file3a,&D3DTextureItem[up]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file4a,&D3DTextureItem[down]);
D3DXCreateTextureFromFile(DX_structure.D3DDevice , file1b ,&D3DTextureItem[left2]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file2b,&D3DTextureItem[right2]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file3b,&D3DTextureItem[up2]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file4b,&D3DTextureItem[down2]);
D3DXCreateTextureFromFile(DX_structure.D3DDevice , file1c ,&D3DTextureItem[left3]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file2c,&D3DTextureItem[right3]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file3c,&D3DTextureItem[up3]);
D3DXCreateTextureFromFile( DX_structure.D3DDevice, file4c,&D3DTextureItem[down3]);
animation = 2;
}
void LoadImage(char file[])
{
D3DXCreateTextureFromFile(DX_structure.D3DDevice , file ,&D3DTextureItem[up]);
Direction = up;
}
void DisplayItem()
{
switch (animation)
{
case 0:
if (!ShowOnScreen)
return;
DX_structure.D3DDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
D3DXMatrixTranslation(&DX_structure.TranslationMatrix,positionX,positionY,positionZ);
D3DXMatrixScaling(&DX_structure.ScaleMatrix, scaleX, scaleY,1.0f);
DX_structure.MatrixWorld = DX_structure.ScaleMatrix * DX_structure.TranslationMatrix;
DX_structure.D3DDevice->SetTransform( D3DTS_WORLD, &DX_structure.MatrixWorld );
DX_structure.D3DDevice->SetTexture(0,D3DTextureItem[Direction]);
DX_structure.D3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
break;
case 2:
if (!ShowOnScreen)
return;
DX_structure.D3DDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
if (animatedcycle>2)
animatedcycle = 0;
D3DXMatrixTranslation(&DX_structure.MatrixWorld,positionX,positionY,positionZ);
D3DXMatrixScaling(&DX_structure.ScaleMatrix, scaleX, scaleY,1.0f);
DX_structure.MatrixWorld = DX_structure.ScaleMatrix * DX_structure.TranslationMatrix;
DX_structure.D3DDevice->SetTransform( D3DTS_WORLD, &DX_structure.MatrixWorld );
DX_structure.D3DDevice->SetTexture(0,D3DTextureItem[Direction+(animatedcycle*4)]);
DX_structure.D3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
break;
}
}
};
So, to display my ship graphic and a bullet I do these steps:
CreateNewScreenItem BULLET0(0,0,100, 1,1, 8,32, 1, 8);
CreateNewScreenItem PLAYERSHIP(0,0,100, 1,1, 80,80, 1, 1);
////////////////////////////////////////////////////////////////////////////////
PLAYERSHIP.LoadImages("Graphics/PlayerShip/PlayerShip_NormalThrust1.png","Graphics/PlayerShip/PlayerShip_NormalThrust2.png","Graphics/PlayerShip/PlayerShip_NormalThrust3.png",
"Graphics/PlayerShip/PlayerShip_NormalThrust1.png","Graphics/PlayerShip/PlayerShip_NormalThrust2.png","Graphics/PlayerShip/PlayerShip_NormalThrust3.png",
"Graphics/PlayerShip/PlayerShip_FullThrust1.png","Graphics/PlayerShip/PlayerShip_FullThrust2.png","Graphics/PlayerShip/PlayerShip_FullThrust3.png",
"Graphics/PlayerShip/PlayerShip_NormalThrust1.png","Graphics/PlayerShip/PlayerShip_NormalThrust2.png","Graphics/PlayerShip/PlayerShip_NormalThrust3.png");
BULLET0.LoadImage("Graphics/Projectiles/Bullet/Bullet.png");
////////////////////////////////////////////////////////////////////////////////
PLAYERSHIP.DisplayItem();
BULLET0.DisplayItem();
To move the ship around and temporary bullet fire code:
if (SolarWars.keypressed[DIK_DOWN] & KEY_PRESSED_DOWN){
PLAYERSHIP.MoveDown();
}
if (SolarWars.keypressed[DIK_SPACE] & KEY_PRESSED_DOWN){
fired = 0;
}
if(WhoFired == player_fired){
if (fired == 0){
float TempX = PLAYERSHIP.GetPositionX();
float TempY = PLAYERSHIP.GetPositionY();
BULLET0.Shoot(TempX, TempY, player_fired, 8);
fired=1;
}
if (fired == 1){
BULLET0.MoveUp();
}
}
I hope I've included everything needed to understand the problem, I have cut alot out.
It IS something to do with my matrices, before I added the scale and translation matrices I could move the ship around and fire the bullet, the code was like this:
D3DXMatrixTranslation(&DX_structure.MatrixWorld,positionX,positionY,positionZ);
DX_structure.D3DDevice->SetTransform( D3DTS_WORLD, &DX_structure.MatrixWorld );
DX_structure.D3DDevice->SetTexture(0,D3DTextureItem[Direction]);
DX_structure.D3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
[Edited by - mud1tza on January 9, 2006 3:31:43 PM]