Jump to content
  • Advertisement
Sign in to follow this  
Hussam Ali

Shooting a bullet to the mouse position?

This topic is 2110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been trying to solve this one problem now for the past few hours. I have managed to make the bullet travel to the mouse position on the mouse click. But this is the problem i am having. When i click somewhere new, the new bullet which is created travels to that position but so does the old ones which are already produced. I am use DirectX and c++. This is all my code for the bullet:

[source]class BulletManager
{
public:
class ObjectFactory
{
public:

class Object
{
public:
Vector3 Position;
Vector3 oldPosition;
Vector3 Velocity;
Vector3 Size;
D3DXVECTOR2 oldpos;
D3DXVECTOR2 pos;
double bullet_direction;
double storedirection;
Vector3 storeStuff[100];
int counter = 0;
void Init()
{
Size.x = 5;
Size.y = 5;
Size.z = 0;

Position.x = 240;
Position.y = 450;
Position.z = 0;



Velocity.x = 20.0f;
Velocity.y = 20.0f;
Velocity.z = 0.0f;
}

void OnMouseClick()
{
bullet_direction = atan2(pos.y - Position.y, pos.x - Position.x);
storedirection = bullet_direction;
bullet_direction = 0;
}

void Update()
{
pos = InputManager::Get().GetMousePos();

oldPosition.x = Position.x;
oldPosition.y = Position.y;

if (InputManager::Get().IsButtonReleased(VK_LBUTTON))
{
oldpos = pos;
bullet_direction = atan2(oldpos.y - Position.y, oldpos.x - Position.x);
}
for (int i = 0; i < 100; i++)
{

}
//4 * cos(bullet_direction);
Position.x += 4*cos(bullet_direction);
Position.y += 4*sin(bullet_direction);
//bullet_direction = 0;

}
};
Object object;
Object* Create()
{
Object* o = new Object();

o->Init();

mObjectList.push_back(o);

return o;
}

void Update()
{
for (ObjectList::iterator it = mObjectList.begin(); it != mObjectList.end(); it++)
{
(*it)->Update();

}
}

void Render()
{
CUSTOMVERTEX *verts = new CUSTOMVERTEX[6 * mObjectList.size()];

int iVert = 0;

ContentManager::TextureContent* pTexture = (ContentManager::TextureContent*)ContentManager::Get().GetContent("bullet");

if (pTexture && pTexture->GetTexture() && pTexture->GetTexture()->texture)
{
RenderManager::Get().g_pd3dDevice->SetTexture(0, pTexture->GetTexture()->texture);
}
else
{
RenderManager::Get().g_pd3dDevice->SetTexture(0, NULL);
}

for (ObjectList::iterator it = mObjectList.begin(); it != mObjectList.end(); it++)
{
int colour = D3DCOLOR_XRGB(255, 255, 255);

Vector2 corners[4];

verts[iVert].pos = (*it)->Position; verts[iVert].color = D3DCOLOR_XRGB(255, 255, 255); verts[iVert].uv0 = corners[0]; iVert++;
verts[iVert].pos = (*it)->Position + Vector3((*it)->Size.x, 0, 0); verts[iVert].color = D3DCOLOR_XRGB(255, 255, 255); verts[iVert].uv0 = corners[1]; iVert++;
verts[iVert].pos = (*it)->Position + Vector3(0, (*it)->Size.y, 0); verts[iVert].color = D3DCOLOR_XRGB(255, 255, 255); verts[iVert].uv0 = corners[2]; iVert++;

verts[iVert].pos = (*it)->Position + Vector3((*it)->Size.x, 0, 0); verts[iVert].color = D3DCOLOR_XRGB(255, 255, 255); verts[iVert].uv0 = corners[1]; iVert++;
verts[iVert].pos = (*it)->Position + Vector3((*it)->Size.x, (*it)->Size.y, 0); verts[iVert].color = D3DCOLOR_XRGB(255, 255, 255); verts[iVert].uv0 = corners[3]; iVert++;
verts[iVert].pos = (*it)->Position + Vector3(0, (*it)->Size.y, 0); verts[iVert].color = D3DCOLOR_XRGB(255, 255, 255); verts[iVert].uv0 = corners[2]; iVert++;
}

RenderManager::Get().g_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x00000001);
RenderManager::Get().g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
RenderManager::Get().g_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

RenderManager::Get().g_pd3dDevice->SetTransform(D3DTS_WORLD, &Matrix::Identity);
RenderManager::Get().g_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2 * mObjectList.size(), verts, sizeof(CUSTOMVERTEX));

RenderManager::Get().g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
}

void Release()
{
for (ObjectList::iterator it = mObjectList.begin(); it != mObjectList.end(); it++)
{
delete (*it);

}

mObjectList.clear();
}

typedef std::list ObjectList;

ObjectList mObjectList;
};

struct CUSTOMVERTEX
{
Vector3 pos;
DWORD color;
Vector2 uv0;
};

ObjectFactory TheObjectFactory;
CUSTOMVERTEX vertexList[6];
IDirect3DVertexDeclaration9* vertexDecleration;

void Init()
{

}

bool Update()
{
TheObjectFactory.Update();
return false;
}

void Render()
{
TheObjectFactory.Render();
}

void Exit()
{
TheObjectFactory.Release();
}

};[/source]*>
 

 
 I know and understand my problem. The problem is that all the Positions are being updated to the new bullet_direction, but I don't know how to give the seperate code for the position of the bullet. Any heads up on how to solve this using the code which i have there? e.g. using arrays to store the variables or something like that. Thanks :)

Share this post


Link to post
Share on other sites
Advertisement

Make a bullet class that stores its own position and destination. The globals you're setting now on input are fine, just pass them into the bullet that is being fired (probably an Init() or something like that).

Share this post


Link to post
Share on other sites

To make your life easier, please don't nest type declarations unless you have to, and please read up on constructors and destructors.

class Foo {
public:
  Foo() {
    //this is the constructor. it runs when the object is created
  }
};

class Bar {
public:
  void update() {
    //blah-blah
  }
  Foo obj; //You can use Foo here because it's declared above. You don't have to nest Foo within Bar.
};

That's a starting point, at least. It should make it easier to see what you're doing here.

Edited by Khatharr

Share this post


Link to post
Share on other sites

In Zombienation (my project)

class Bullet : public sf::RectangleShape
{
public:
    Bullet(float speed,sf::Vector2f direct,sf::Vector2f pos);
    bool update(float dt);
    double getDistance()
    {
        return distanceTraveled;
    }

private:
    float speed;
    sf::Vector2f direction;
    double distanceTraveled; // if distance is to long then delete bullet from memory
    sf::Vector2f position, startingPosition;
};
bool Weapon::shot(sf::Vector2f pos, sf::Vector2f direction)
{
    if((roundsIn > 0) && canShot)
    {
        isShooting = true;
        for(unsigned int x = 0; x < bulletCont.size(); x++)
        {
            if(bulletCont[x] == nullptr) // when one is empty fill it with new bullet
            {
                bulletCont[x] = new Bullet(1600,direction,pos);
                canShot = false; // each weapon have different fire rate 
                roundsIn--;
                return true;
                break;
            }
        }
        bulletCont.push_back(new Bullet(1600,direction,pos));
        canShot = false;
        roundsIn--;
    }
    else
    {
        isShooting = false; // weapon is empty or have to wait for new bullet
        return false;
    }
    if(roundsIn == 0)
    {
        reloadingCall = true;
    }
}

where bulletCont is a vector of Bullet's pointers. Simple.

 

And direction

void CCharacter::shot()
{
    sf::Vector2f tempDirect(aim); // aim store where mouse is
    recoil(&tempDirect);
    if(actual->shot(pos,tempDirect)) // player tried too shot
    {
        crosshair->pushSize(1.1);
    }
}
void CCharacter::updateDirection()
{
    direct.x = (aim.x-400);
    direct.y = (aim.y-300);

    double len = sqrtf(direct.x*direct.x+direct.y*direct.y);
    direct.x/=len;
    direct.y/=len;
}

If you want I could send you all files (cpp + hpp of those 3 classes)

Share this post


Link to post
Share on other sites

Thanks for the replies :) I will try and do it myself by creating a new class for bullet which creates the bullet. I will pass those values of position into the bullet which is created. I will let you know how i get on :) Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!