• Create Account

Banner advertising on our site currently available from just \$5!

### #Actualneomaycry

Posted 24 July 2013 - 07:12 AM

I am working on a space shooter game, using SFML and c++.  My game has an enemy that flies to the center of the screen and shoots a laser which rotates 360 degrees.  Everything about this process looks great except one fact: the laser needs to be "clipped" when it encounters a tile (so the player can hide behind the tile to avoid it). Take a look at the following image:

[attachment=16964:laserball.png]

Also, here is the code I have written:

for(int i=0;i<numBlocks;i++)
for(int j=0;j<numEnemies;j++)
if(Collision::BoundingBoxTest(block[i].sprite, enemy[j].laser)
&& block[i].alive && enemy[j].alive && enemy[j].id == LASERBALL)
{
std::cout << "Laser collided with tile!" << std::endl;
enemy[j].laserCollidedWithTile = true;
enemy[j].laser.setScale(laserW * (block[i].sprite.getPosition().x /
enemy[j].sprite.getGlobalBounds().width), 1); //fix this
}


The collision itself is working fine.  The problem I am having is figuring out the math of how to scale the laser so it appears to be blocked by the tile.  I've also considered approaching this problem by resizing the sprite's textureRect.

If you have time, I would appreciate feedback regarding my approach and how to fix it.  Remember that the laser is a rotated object.

EDIT: 'laserW' is a global variable used for testing purposes.  It contains the width of the laser sprite, which is 1060px.

### #2neomaycry

Posted 24 July 2013 - 06:45 AM

I am working on a space shooter game, using SFML and c++.  My game has an enemy that flies to the center of the screen and shoots a laser which rotates 360 degrees.  Everything about this process looks great except one fact: the laser needs to be "clipped" when it encounters a tile (so the player can hide behind the tile to avoid it). Take a look at the following image:

[attachment=16964:laserball.png]

Also, here is the code I have written:

for(int i=0;i<numBlocks;i++)
for(int j=0;j<numEnemies;j++)
if(Collision::BoundingBoxTest(block[i].sprite, enemy[j].laser)
&& block[i].alive && enemy[j].alive && enemy[j].id == LASERBALL)
{
std::cout << "Laser collided with tile!" << std::endl;
enemy[j].laserCollidedWithTile = true;
enemy[j].laser.setScale(laserW * (block[i].sprite.getPosition().x /
enemy[j].sprite.getGlobalBounds().width), 1); //fix this
}


The collision itself is working fine.  The problem I am having is figuring out the math of how to scale the laser so it appears to be blocked by the tile.  I've also considered approaching this problem by resizing the sprite's textureRect.

If you have time, I would appreciate feedback regarding my approach and how to fix it.  Remember that the laser is a rotated object.

EDIT: 'laserW' is a global variable used for testing purposes.  It contains the width of the laser sprite, which is 1060px.

### #1neomaycry

Posted 24 July 2013 - 06:42 AM

I am working on a space shooter game, using SFML and c++.  My game has an enemy that flies to the center of the screen and shoots a laser which rotates 360 degrees.  Everything about this process looks great except one fact: the laser needs to be "clipped" when it encounters a tile (so the player can hide behind the tile to avoid it). Take a look at the following image:

[attachment=16964:laserball.png]

Also, here is the code I have written:

for(int i=0;i<numBlocks;i++)
for(int j=0;j<numEnemies;j++)
if(Collision::BoundingBoxTest(block[i].sprite, enemy[j].laser)
&& block[i].alive && enemy[j].alive && enemy[j].id == LASERBALL)
{
std::cout << "Laser collided with tile!" << std::endl;
enemy[j].laserCollidedWithTile = true;
enemy[j].laser.setScale(laserW * (block[i].sprite.getPosition().x /
enemy[j].sprite.getGlobalBounds().width), 1); //fix this
}


The collision itself is working fine.  The problem I am having is figuring out the math of how to scale the laser so it appears to be blocked by the tile.  I've also considered approaching this problem by resizing the sprite's textureRect.

If you have time, I would appreciate feedback regarding my approach and how to fix it.  Remember that the laser is a rotated object.

PARTNERS