Jump to content
  • Advertisement
Sign in to follow this  
Bujdasz

Making my character shoot a projectile in a game?

This topic is 3985 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

Now i have a sprite and i want it to emit a bullet or something like that,basically make it fire a weapon how would that be done. I don't even know where to start and how would i make it go forward until it hits an enemy.

Share this post


Link to post
Share on other sites
Advertisement
Well the way i whould do it is first make a bullet class with all the bullet properties you want. Then make a linked list that has all the bullets in the game in it, and then every graphics update comes calculate where they need to go then render them.

something like this:


class Bullet
{
public:
float x, y;
float angle;

void UpdatePosition(float x, float y, float angle);
void Render();
}

//initalize this some where in your code
list<Bullet> * Bullets = new list<Bullet>;

//somewhere in your input source
if(/*upkey was pressed*/)
{
Bullet * newBullet = new Bullet();
newBullet->x = blahx //your characters position
newBullet->y = blahy // your characters y
newBullet->angle = blahangle // your characters angle

Bullets->push_back(newBullet);
}

void Bullet::UpdatePosition(float x, float y, float angle)
{
//calculate the position here
//with a bunch of if then that are cased by the angle of the bullet
}

void Bullet::Render()
{
//render here
}


Share this post


Link to post
Share on other sites
To understand if you shoot an enemy, you might use a ray/vector for bullet and check for collision. (google collision detection for detail)

foreach enemy
if ( CheckCollision(bullet->ray, enemy->boundingBox) )
enemy->hitPoints--
else
// you missed

If you are using directx, d3dx has some intersection test methods aka D3DXIntersect, D3DXIntersectSubset...

Share this post


Link to post
Share on other sites
Man i need to look into vectors more,im looking to add bullets to two enemies who are stationary and then to my main character who is mobile every which way.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!