Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision "dont know how"  (Read 2820 times)

0 Members and 1 Guest are viewing this topic.

edumoette

  • Newbie
  • *
  • Posts: 23
    • View Profile
Collision "dont know how"
« on: July 21, 2012, 04:19:38 am »
Code: [Select]
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);
Code: [Select]
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.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Collision "dont know how"
« Reply #1 on: July 21, 2012, 05:11:02 am »
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
« Last Edit: July 21, 2012, 12:19:01 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Collision "dont know how"
« Reply #2 on: July 21, 2012, 09:24:03 am »
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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

natchos

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
    • Email
Re: Collision "dont know how"
« Reply #3 on: July 21, 2012, 02:46:53 pm »
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.

edumoette

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Collision "dont know how"
« Reply #4 on: July 21, 2012, 08:02:41 pm »
Thanks exploit! is there any way to choose the color when using rect<T>??

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Collision "dont know how"
« Reply #5 on: July 21, 2012, 08:53:32 pm »
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 where you can look up exactly what functions a class has. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chris231

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Collision "dont know how"
« Reply #6 on: July 21, 2012, 09:22:33 pm »
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));
                }
        }
}

 

anything