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

Author Topic: comparing shapes position to each other (detect collision)  (Read 4384 times)

0 Members and 1 Guest are viewing this topic.

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
comparing shapes position to each other (detect collision)
« on: June 29, 2012, 12:49:07 pm »
hello i am making a pong game and i need to compare the x position of the panel to the x position of the ball. but if i use GetPosition() the x value for the panels is always 0 so i can't really compare them that way. i believe i need some kind of conversion. but i don't know which. i have tried to use TransformToGlobal but that didn't work.
below is the code (minimalisezed)
sf::Vector2f pos = ball.TransformToGlobal(ball.GetPosition());
sf::Vector2f pos1 = panel1.TransformToGlobal(panel1.GetPosition());
sf::Vector2f pos2 = panel2.TransformToGlobal(panel2.GetPosition());
std::cout<<pos1.x<<","<<pos1.y<<std::endl;
std::cout<<pos2.x<<","<<pos2.y<<std::endl;
 
it outputs always something like this
0,122
0,-101
full code under this if someone wants to test:
#include "stdafx.h"
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes"/*, sf::Style::Fullscreen*/);

        //define shapes
        sf::Shape panel1=sf::Shape::Rectangle(20, 300, 30, 360, sf::Color::White);
        sf::Shape panel2=sf::Shape::Rectangle(770, 300, 780, 360, sf::Color::White);
        sf::Shape ball=sf::Shape::Circle(395, 330, 10, sf::Color(200,200,200));
    ball.SetCenter(0,0);
        bool game=false;
        int direction=0;

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

                        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                App.Close();

                        if (Event.Type == sf::Event::KeyPressed && Event.Key.Code==sf::Key::Space)
                game=true;
        }

        // Clear screen
        App.Clear();

        // Draw shapes
                App.Draw(panel1);
                App.Draw(panel2);
                App.Draw(ball);
       
                //elapsed time
                float ElapsedTime = App.GetFrameTime();
               
                //move panels
                if (App.GetInput().IsKeyDown(sf::Key::Up))    {
                       
                        panel2.Move(0, -110 * ElapsedTime);
                       
                                sf::Vector2f pos1 = panel1.TransformToGlobal(panel1.GetPosition());
                                sf::Vector2f pos2 = panel2.TransformToGlobal(panel2.GetPosition());
                                std::cout<<pos1.x<<","<<pos1.y<<std::endl;
                                std::cout<<pos2.x<<","<<pos2.y<<std::endl;
                }
                if (App.GetInput().IsKeyDown(sf::Key::Down))  panel2.Move(0, 110 * ElapsedTime);

                if (App.GetInput().IsKeyDown(sf::Key::U))     panel1.Move(0, -110 * ElapsedTime);
                if (App.GetInput().IsKeyDown(sf::Key::J))     panel1.Move(0, 110 * ElapsedTime);

                if (game) {
                        if (direction==0) {
                                                sf::Vector2f pos = ball.TransformToGlobal(ball.GetPosition());
                                if (pos.x+(-49.1934955f * ElapsedTime)<-320) {
                                        direction=1;
                                } /*else if (pos.x) {
                               
                                }*/
else {
                                ball.Move(-49.1934955f * ElapsedTime, -98.386991f * ElapsedTime);
                                }
                        }
                        if (direction==1) {
                                sf::Vector2f pos = ball.TransformToGlobal(ball.GetPosition());
                                if (pos.x+(49.1934955f * ElapsedTime)>320) {
                                        direction=1;
                                } else {
                                ball.Move(-98.386991f * ElapsedTime, 49.1934955f * ElapsedTime);
                                }
                        }
                }

        // Finally, display the rendered frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: June 29, 2012, 02:28:00 pm by gelatine »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: comparing shapes position to each other
« Reply #1 on: June 29, 2012, 01:46:49 pm »
There's nothing strange about this. You never change the x position of the panels (sprite.Move(x, y)) so it will never change from 0 to anything else.

What's the purpose of comparing them? What are you trying to achive?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: comparing shapes position to each other
« Reply #2 on: June 29, 2012, 01:53:37 pm »
i want to see if the ball hits the panel. so i need the x coordinate of the panel and the x coordinate of the ball.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: comparing shapes position to each other
« Reply #3 on: June 29, 2012, 01:59:59 pm »
So you're trying to achive a simple collision detection.
You can compare the x and y positions quite good with GetPosition() but if you don't move your panel in the x direction, the x position will always be 0.

I suggest you google a bit on the topic of collision detection.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: comparing shapes position to each other
« Reply #4 on: June 29, 2012, 02:02:40 pm »
        sf::Shape panel1=sf::Shape::Rectangle(20, 300, 30, 360, sf::Color::White);
        sf::Shape panel2=sf::Shape::Rectangle(770, 300, 780, 360, sf::Color::White);

I don't think this does what you intend it to. I'm pretty sure you're just creating a rect at position (0,0) whose top/left and bottom/right corners are at [20, 300] and [30, 360]. Essentially a large black sprite with your rectangle in its bottom right corner.

Create them like this:
        sf::Shape panel1=sf::Shape::Rectangle(0, 0, 30, 360, sf::Color::White);
        sf::Shape panel2=sf::Shape::Rectangle(0, 0, 780, 360, sf::Color::White);

And then Move() or SetPosition() them to the desired location. If you do this GetPosition should return the desired result, and you can continue to move() them around without your coordinates getting mucked up.

I believe the same is true of your code to create the circle.

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: comparing shapes position to each other
« Reply #5 on: June 29, 2012, 02:14:25 pm »
i don't really get what you mean.
the way the rectangles are placed now is fine for me. or maybe you mean something else :s .
so if i use your code then i get a huge rectangle whit 1 corner at the right place:
sf::Shape panel1=sf::Shape::Rectangle(0, 0, 30, 360, sf::Color::White);
                panel1.SetPosition(20,300);
« Last Edit: June 29, 2012, 02:21:55 pm by gelatine »

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: comparing shapes position to each other
« Reply #6 on: June 29, 2012, 02:34:11 pm »
Sorry I should have subtracted the difference from the bottom right corner as well.

sf::Shape panel1=sf::Shape::Rectangle(0, 0, 10, 60, sf::Color::White);
                panel1.SetPosition(20,300);
Like this. It should result in a rectangle that appears the same as the one in your first post, but now SFML correctly knows where the objects position is.

GetPosition should then return a vector that points to (20, 300) rather than (0,0); And you can use it to compare the objects position.

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: comparing shapes position to each other (detect collision)
« Reply #7 on: June 29, 2012, 03:29:58 pm »
it's starting to work with the x-variables now i  have to add the y coordinates ;D
and thanks for helping me

gelatine

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: comparing shapes position to each other (detect collision)
« Reply #8 on: June 29, 2012, 10:00:12 pm »
ok i got it fully workin now =d

 

anything