Sign in to follow this  
nano511

Problem with shooting bullets

Recommended Posts

Right now when i try to shoot the monsters dont get hit
Here is the code to see if the monster got hit

[code]

bool Monster::IsHit( int bulletXPos, int bulletYPos )
{
if( bulletXPos >= xPos + 10 && bulletXPos <= xPos + 30 )
{
if( bulletYPos >= yPos && bulletYPos <= yPos + 74 )
{
health -= 1;
xVel = 0;
return true;
}
}

return false;
}
[/code]



And here is some other relevant code that is preformed before monster.IsHit() is called
[code]

// If the player presses space, create a new bullet
if( player->shoot == true )
{
bullet = new Bullet;
bullets.push_back( bullet );
bullets.front()->xPos = player->xPos;
bullets.front()->yPos = player->handgun.yPos;
if( player->direction == LEFT )
bullets.front()->xVel = -15;
else if( player->direction == RIGHT )
bullets.front()->xVel = 15; }

// Move the bullet
if( !(bullets.empty()) )
{
bullets.front()->xPos += bullets.front()->xVel;
}
[/code]

E: i also get this error sometimes:
[code]
Debug Assertion Failed!
2010\...\include\vector
Line: 70

Expression: vector iterator not dereferencable

For information on how your program can cause assertion failure, see the Visual C++ documentation on assets
[/code]

Share this post


Link to post
Share on other sites
Most likely it's incorrect math, but I don't know what the dimensions of your 'Monster' sprite is, so I'm not entirely sure on that. But do the bullets act as you want them to? It's just that there is no collision detection ?

Share this post


Link to post
Share on other sites
[quote name='lukeymoo' timestamp='1313195420' post='4848470']
Most likely it's incorrect math, but I don't know what the dimensions of your 'Monster' sprite is, so I'm not entirely sure on that. But do the bullets act as you want them to? It's just that there is no collision detection ?
[/quote]

The dimensions are fine. There are no 'actual' bullet's atm so i cant really see whats going on but from debugging im pretty sure they're acting as i want them to.. should i make a bullet sprite to see?

Share this post


Link to post
Share on other sites
Ok so what happens if a monster is hit? Is the bullet removed so that you can shoot a new bullet? I don't know if one bullet is enough to kill a monster but if it isn't and the bullet is never removed it will not be much to see. If one bullet isn't enough to kill a monster it will not be much to see. The bullet will go through the monsters and move outside the screen.

Share this post


Link to post
Share on other sites
Video doesn't exist, but I think you should make a sprite for the bullet, it will aid in the debugging of your game, and also provide a new milestone in your game(New art) :]
But yeah, make a sprite, and see what happens to the bullet...

You'll have to add some code to determine whether or not to draw the bullet sprite, like for example
[font="CourierNew, monospace"] [/font]

[source lang='cpp']
[font="CourierNew, monospace"][size="2"]if(!monster.isHit(bulletx, bullety))[/size][/font]
[font="CourierNew, monospace"][size="2"]{[/size][/font]
[font="CourierNew, monospace"][size="2"] apply_surface(blah...);[/size][/font]
[font="CourierNew, monospace"][size="2"] or for allegro[/size][/font]
[font="CourierNew, monospace"][size="2"] masked_blit(blah..);[/size][/font]
[font="CourierNew, monospace"][size="2"]}[/size][/font]
[/source]

[font="CourierNew, monospace"][size="2"]But basically this just checks to see if the function isHit(int,int) returns true[/size][/font]
[font="CourierNew, monospace"][size="2"]and if it returns false, then it would draw the bullet as usually but if the function returns true[/size][/font]
[font="CourierNew, monospace"][size="2"]it would never execute the code within' the 'if' statement.[/size][/font]
[font="CourierNew, monospace"][size="2"]As for collisions, I recommend adding 'int' variables, called x1, y1, x2, y2, x3, y3, x4, y4[/size][/font]
[font="CourierNew, monospace"][size="2"]to your 'Monster' class, to provide more OO code. x1, would be top-left corner X and y1 would be top-left corner Y.[/size][/font]
[font="CourierNew, monospace"][size="2"]Etc, as for initializing these variables, they would need to be constantly updated, in the while loop that your game runs on, so just stick function in there, to update the bounding-box coordinates continuously, some of this might not be optimized code, but for the complexity of your game, I don't think you'll need 'optimized' code.[/size][/font]
[font="CourierNew, monospace"] [/font]
[font="CourierNew, monospace"][size="2"]EDIT....[/size][/font]
[font="CourierNew, monospace"][size="2"]P.S I'd make the bullet sprite a circle, within' a 16x16 square...Try and make the bullet any other way, and you'll soon discover why I recommended you do circles...[/size][/font] Edited by lukeymoo

Share this post


Link to post
Share on other sites
Oooh sorry i forgot i was messing around with my albums lol. [url="http://s276.photobucket.com/albums/kk15/stkguy/MyGameVideos/action=view¤t=20110812_2247_45.mp4"]here[/url] is the video


The bullet is actually a rectangle. I was thinking the same thing about aiding in debugging, so i made the sprite before i made the video so you could see. In the video the bullet doesnt go too far if nothing hit it, but i fixed it.

PS. I dont get why i should have made it a circle?

I fixed it. The problem was i was using .front() instead of .back()

Share this post


Link to post
Share on other sites
Oh, I thought you were making a top down, if you were making a top down the bullet, would have to oriented in a specific direction.

As for the Assertion Failure, I've never had that issue, but your probably using a library or function incorrectly. What line does it point to, and could you please show the line(s) where the error is at.

From the video, it seems like it only happens when the bullet collides with the monster to the right.

Share this post


Link to post
Share on other sites
If there is only ever 1 bullet at a time, why have a vector? Why not just have a single Bullet *ActiveBullet variable? Set to to NULL when there's no bullet, otherwise, set it to the current bullet.

I would also suggest you use the dimension of the objects colliding, instead of hard-coding the variables. Something like this:

[code]
struct Rectangle
{
int xPos;
int yPos;
int Width;
int Height;
};

// where Bullet is defined:
class Bullet
{
.
.
.
// Dimension is retrieved from the Sprite size representing the bullet
Rectangle Dimension;

// Strength is how hard the bullet hits, set to 1 for default gun
int Strength;

// Velocity is standard velocity for this bullet type
int Velocity;
.
.
.
};

bool Monster::IsHit(Bullet *BulletToCheck )
{

// Assuming Monster has a Dimension variable as well
if( Intersects(BulletToCheck->Dimension, Dimension)
{
health -= BulletToCheck->Strength;
xVel = 0;
return true;
}

return false;
}

// defined in some utility class maybe?
// Use reference so it only passes the pointer, doesn't copy full structure
bool Intersects(Rectangle &Obj1, Rectangle &Obj2)
{

if ((Obj1.xPos+ Obj1.Width>= Obj2.xPos) && (Obj1.xPos<= Obj2.xPos+ Obj2.Width))
if ((Obj1.yPos+ Obj1.Height>= Obj2.yPos) && (Obj1.yPos<= Obj2.yPos+ Obj2.Height))
{
return true;
}
}

return false;
}


// code for handling shooting
// If the player presses space, create a new bullet
if( player->shoot == true )
{
ActiveBullet = new Bullet(player->xPos, player->yPos);
if( player->direction == LEFT )
ActiveBullet ->xVel = 0 - ActiveBullet ->Velocity;
else if( player->direction == RIGHT )
ActiveBullet ->xVel = ActiveBullet ->Velocity;
}

// Move the bullet
if(ActiveBullet )
{
ActiveBullet ->xPos += ActiveBullet ->xVel;
}


// When looping through all the monsters, check if hit
if (MonsterToCheck->IsHit(ActiveBullet)
{
delete ActiveBullet;
ActiveBullet = NULL;
}
[/code]
[quote name='nano511' timestamp='1313194517' post='4848466']
Right now when i try to shoot the monsters dont get hit
Here is the code to see if the monster got hit

[code]

bool Monster::IsHit( int bulletXPos, int bulletYPos )
{
if( bulletXPos >= xPos + 10 && bulletXPos <= xPos + 30 )
{
if( bulletYPos >= yPos && bulletYPos <= yPos + 74 )
{
health -= 1;
xVel = 0;
return true;
}
}

return false;
}
[/code]



And here is some other relevant code that is preformed before monster.IsHit() is called
[code]

// If the player presses space, create a new bullet
if( player->shoot == true )
{
bullet = new Bullet;
bullets.push_back( bullet );
bullets.front()->xPos = player->xPos;
bullets.front()->yPos = player->handgun.yPos;
if( player->direction == LEFT )
bullets.front()->xVel = -15;
else if( player->direction == RIGHT )
bullets.front()->xVel = 15; }

// Move the bullet
if( !(bullets.empty()) )
{
bullets.front()->xPos += bullets.front()->xVel;
}
[/code]

E: i also get this error sometimes:
[code]
Debug Assertion Failed!
2010\...\include\vector
Line: 70

Expression: vector iterator not dereferencable

For information on how your program can cause assertion failure, see the Visual C++ documentation on assets
[/code]

[/quote]

Share this post


Link to post
Share on other sites
And as soon as you start having more than 1 bullet in the vector, it will break, because you insert the bullet at the back, and then alter the front item.

Typically, you build the object before inserting it. You don't half-build it, insert it, and then finish building it in-place.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this