-
Laser.setFillColor(sf::Color::Red);
Laser.setSize(sf::Vector2f(700, 10));
Laser.setOutlineColor(sf::Color::Transparent);
Laser.setOutlineThickness(4);
sf::RectangleShape enemy;
enemy.setFillColor(sf::Color::Magenta);
enemy.setSize(sf::Vector2f(10,10));
enemy.setPosition(300, 300);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
Laser.setPosition(sf::Vector2f(snakebody.getPosition()));
if(sf::Vector2f(Laser.getPosition()) == (sf::Vector2f(enemy.getPosition())))
window.close(); // Window Close is just to test and see if its
I want to make it so that if any part of Laser(including the transparent outline) touches (enemy) than the window will shut down...What i have runs fine and is syntactically correct. But the way im doing it. I believe Laser and enemy's position and size would have to be exactly the same for the window to close. The close is just a test by the way, ill add what i actually want it to do after i figure this out. Thanks for any help. Snakebody is a 10x10 rectangle moveable "character". and the laser shoots from the right side. So whereve the character is the laser will shoot from that place. My style is disgusting, i know. I hope its not too hard to read those few lines.
-
well to check for collision you can do it many ways, you can check if a point is inside a rectangle, for example, if you take the X, Y, Width and Height of a object you can find out a rectangle,
X = 0
Y = 0
Width = 10
Height = 10
thats a 10 by 10 rectangle,
now if we had a block that is at position (2,4) with a width of 1 and height of 1, we will have a collision
now to do this you have to do a if statement
we have snake and block
snake[x = 0,y = 0,width = 10,height = 10]
block[x = 2,y = 4,width = 1,height = 1]
lets check for a point is inside a rectangle
if((block.x >= snake.x || block.x <= snake.w + snake.x) == true && (block.y >= snake.y || block.y <= snake.height + snake.y) == true)
{
//we have a collision
}
let me know if you need anymore help. Hope this helps
-
Both of you please make use of the code=cpp tag, it makes reading stuff so much more enjoyable. ;)
As for collision detection, google will give you tons of links, so will the forum search function. Afaik there's even a tutorial in the wiki about this.
The most easiest collision detection is to check if two rectangle intersect or if a point is inside a rectangle. For both the sf::Rect has a function (intersect(), contains()) so you only have to generate two rectangles and check for 9ntersection. But keep in mind this detection can fail at higher movement speed. ;)
-
A simple solution, is to enlargen the rect if your pacman is going at high speed.
float speed;
if( pacman.getVecloity > 10) speed = 30;
else if (pacman.getVelocity > 15) speed = 45;
sf::FloatRect(pacman.getPosition + speed, etc);
As exploiter says there are tons of guides and/or ready made functions floating around on the web, so feel free to use one of them if you don't want make your own.
-
Thanks exploit! is there any way to choose the color when using rect<T>??
-
Ehrm sf::Rect<T> is just a 'mathematical object' you can draw it or anything, so no there's no sense in it having a property for color. Also there's the documentation (http://www.sfml-dev.org/documentation/2.0/) where you can look up exactly what functions a class has. ;)
-
That's my way how to check the collision between the player (spaceship) and every asteroid im looping through:
void PlayState::CheckCollisions()
{
std::list<Asteroid>::iterator It;
for(It = asteroids.begin(); It != asteroids.end(); It++)
{
if(It->getPos().x <= playerRect.left + playerRect.width &&
It->getPos().x + It->getRect().width >= playerRect.left &&
It->getPos().y <= playerRect.top + playerRect.height &&
It->getPos().y + It->getRect().height >= playerRect.top) {
// asteroid collides player
It = asteroids.erase(It);
playerLives--;
if (playerLives == 0)
gameOver = true;
Lives.setString("Lives: " + toString(playerLives));
}
}
}