#include "Ship.h"
static DXSound& dxSound = DXSound::getInstance();
static DXLib& dxLib = DXLib::getInstance();
Ship::Ship(const std::string name)
{
vec.x=velocity;
health=255;
shields=255;
weapon=0;
filename=name;
boostEnabled=false;
radius = 50;
}
Ship::~Ship()
{
}
bool Ship::create(LPDIRECT3DDEVICE9 device)
{
position.x = 0.0f;
position.y = 0.0f;
position.z = 0.0f;
vec.x = 0.0f;
vec.y = 180.0f;
vec.z = 1.0f;
objectModel = new Model();
for(unsigned int i=0;i<20;i++)
{
PulseCannon* c = new PulseCannon("cannonball.X");
c->create(DXLib::getInstance().getDevice());
float x = -9999;
float y = -9999;
float z = -9999;
c->setPosition(D3DXVECTOR3(x,y,z));
bulletBuffer.push_back(c);
}
return objectModel->loadModel(device,filename);
}
void Ship::render(LPDIRECT3DDEVICE9 device)
{
D3DXMatrixTranslation(&transMatrix,position.x,position.y,position.z);
D3DXMatrixRotationY(&rotMatrix,D3DXToRadian(vec.y));
D3DXMatrixMultiply(&transMatrix,&rotMatrix,&transMatrix);
D3DXMatrixRotationZ(&rotMatrix,D3DXToRadian(vec.z));
D3DXMatrixMultiply(&transMatrix,&rotMatrix,&transMatrix);
D3DXMatrixRotationX(&rotMatrix,D3DXToRadian(vec.x));
D3DXMatrixMultiply(&transMatrix,&rotMatrix,&transMatrix);
device->SetTransform(D3DTS_WORLD,&transMatrix);
objectModel->render(device);
for(unsigned int i =0;i<cannon.size();i++)
{
if(cannon!=NULL)
{
D3DXVECTOR3 pos = cannon->getPosition();
if(pos.z>5000||pos.y<-5000)
{
freeBullet(i);
}
else
{
cannon->move();
cannon->render(device);
}
}
}
vec.x=0;
vec.y=0;
}
void Ship::moveShip(int dir,float a_rate)
{
D3DXVECTOR3 tmpLook = dxLib.g_vLook;
D3DXVECTOR3 tmpRight = dxLib.g_vRight;
if( dir==DIK_UP )
{
dxLib.g_vEye.y -= 10*a_rate;
//position.y-=10;
}
if( dir==DIK_DOWN)
{
dxLib.g_vEye.y += 10*a_rate;
//position.y+=10;
}
if(dir == DIK_RIGHT)
{
dxLib.g_vEye -= (tmpRight*-10)*a_rate;
//position.x-=10;
}
if(dir == DIK_LEFT)
{
dxLib.g_vEye += (tmpRight*-10)*a_rate;
//position.x+=10;
}
if(dir==DIK_X)
{
vec.z+=15.0f;
vec.x+=10.0f;
}
if(dir==DIK_Z)
{
vec.z-=15.0f;
vec.x-=30.0f;
}
if(dir==DIK_B)
{
boostEnabled=true;
}
if(dir==DIK_SPACE)
{
dxSound.loadWaveFile("pulse.wav");
dxSound.playWave();
int index = findFreeBullet();
if(index!=-1)
{
PulseCannon* c = bulletBuffer[index];
bulletBuffer.erase(bulletBuffer.begin()+index);
float x = position.x;
float y = position.y;
float z = position.z;
z+=50;
c->setPosition(D3DXVECTOR3(x,y,z));
cannon.push_back((PulseCannon*)c);
}
}
bool b = false;
if(position.z>4900)
{
b=true;
}
if(boostEnabled)
{
if(b)
{
position.z-=30;
}
else{position.z+=30;}
}
else
{
if(b)
{
position.z-=10;
}
else{position.z+=10;}
}
dxLib.g_vEye -= (tmpLook*-10)*a_rate;
}
int Ship::findFreeBullet()
{
for(unsigned int index=0;index<bulletBuffer.size();index++)
{
D3DXVECTOR3 pos =bulletBuffer[index]->getPosition();
if(pos.x==-9999)
{
bulletBuffer[index]->setPosition(D3DXVECTOR3(0,0,0));
return index;
}
}
return -1;
}
void Ship::freeBullet(int index)
{
cannon[index]->setPosition(D3DXVECTOR3(-9999,-9999,-9999));
bulletBuffer.push_back(cannon[index]);
cannon.erase(cannon.begin()+index);
}
void Ship::setPosition(D3DXVECTOR3 pos)
{
position.x=pos.x;
position.y=pos.y;
position.z=pos.z;
}
int Ship::getHealth()
{
return health;
}
int Ship::getShields()
{
return shields;
}
int Ship::getWeapon()
{
return weapon;
}
void Ship::setHealth(int h)
{
health=h;
}
void Ship::setShield(int s)
{
shields=s;
}
void Ship::setWeapon(int w)
{
weapon=w;
}
D3DXVECTOR3 Ship::getPosition()
{
return position;
}
Camera problems in 3D environment
hi,
i have this problem. i currently have a simple 3d game written with a long floor (vertex drawing) with a texture of grass on it(like star fox).anyway my problem is i have a ship positioned in the environment, and i can move it left\right up\down.but the problem is that i want the ship to move at a constant rate,(like starfox) except what happens is that the ship rips off into the distance but the camera doesnt stay is persuit. heres the code, i would appreciate any help i can get..
EDIT: Added source tags and title.
[Edited by - 23yrold3yrold on August 22, 2004 9:57:24 PM]
PLEASE ALWAYS INCLUDE LENGTHY CODE IN <SOURCE> tags.
Other than that, why don't you reset the projection matrix to directly behind the ship? You can't get the springy camera effect with it too easily, but it is much easier.
And, just as another note, you should go through the code, find where you think the problem lies, and if we need to see more, we'll ask for it.
EDIT: and I notice you haven't actually included anything about the setting of the projection matrix, or any translations to it, which is probably your problem. If this is the case (and/or you have no idea what the projection matrix is), then you need to create one.
I actually can't provide any help right now, first because I use VB6, not C++, and second because I don't have any of my code handy. I would suggest you browse the artices, and/or [google] for answers. If you still need help, I and/or others will be more than happy to assist!
Oh - and don't leave subjects untitled.
Have a nice day/night/whatever!
Other than that, why don't you reset the projection matrix to directly behind the ship? You can't get the springy camera effect with it too easily, but it is much easier.
And, just as another note, you should go through the code, find where you think the problem lies, and if we need to see more, we'll ask for it.
EDIT: and I notice you haven't actually included anything about the setting of the projection matrix, or any translations to it, which is probably your problem. If this is the case (and/or you have no idea what the projection matrix is), then you need to create one.
I actually can't provide any help right now, first because I use VB6, not C++, and second because I don't have any of my code handy. I would suggest you browse the artices, and/or [google] for answers. If you still need help, I and/or others will be more than happy to assist!
Oh - and don't leave subjects untitled.
Have a nice day/night/whatever!
Hi, sorry about the format of the question before.. didnt realise i forgot the source tags. You asked about my projection matrix. i did define one but, i presume you mean that each time the ship moves I need to do something like this. right?
D3DXMATRIX matView; D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3(0.0f, 0.0f,-10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f ) ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 200.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement