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

Author Topic: I need help with this game i am trying to make.  (Read 6827 times)

0 Members and 2 Guests are viewing this topic.

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
I need help with this game i am trying to make.
« on: April 25, 2011, 12:33:50 am »
Hi guys, i have only recently started SFML C++ C::B and i know how to control things, create windows,views but i am getting really confused. i have no idea when it comes to collision detection and physics.

all i have got is my background a wall and a ball loaded to the center, i don't know how i can make collision detection or make the ball move on the certain angle, Help me please i have read all the simplest collision detection pages and i don't understand it. i will post my code here and can some one tell me what i need to do please i will learn off that code you post me.

Code: [Select]

#include<SFML/Graphics.hpp>

int main()
{

    sf::RenderWindow App(sf::VideoMode(1000, 600, 32), "SFML/Graphics");

    sf::Image bPong;
    if (!bPong.LoadFromFile("bPong.png"))
    return EXIT_FAILURE;

    sf::Sprite sbPong(bPong);

    sf::Image iBall;
    if (!iBall.LoadFromFile("Ball.png"))
    return EXIT_FAILURE;

    sf::Sprite sBall(iBall);

    sf::Image Wall;
    if (!Wall.LoadFromFile("Wall.png"))
    return EXIT_FAILURE;

    sf::Sprite sWall(Wall);

    while (App.IsOpened())
    {
        sf::Event Event;

        while (App.GetEvent(Event))
        {

            if (Event.Type == sf::Event::Closed)
            App.Close();

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

        float ElapsedTime = App.GetFrameTime();

        if (App.GetInput().IsKeyDown(sf::Key::Up)) sWall.Move(0, -100 * ElapsedTime);
       
       
        App.Clear();
        App.Draw(sbPong);
        App.Draw(sBall);
        App.Draw(sWall);
        App.Display();
    }



    return EXIT_SUCCESS;
}


i currently can only pull the wall up but i am not finishing until i understand.[/code]
I'm tring to learn SFML Help? Collision?

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
I need help with this game i am trying to make.
« Reply #1 on: April 25, 2011, 11:03:53 am »
Moving the ball on the certain angle is pretty simple. In the Pong example it's done like this:
Code: [Select]

// Move the ball
            float factor = ballSpeed * window.GetFrameTime();
            ball.Move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);

But I find it more natural to do it like this:
Code: [Select]

// Move the ball
            float factor = ballSpeed * window.GetFrameTime();
            ball.Move(std::cos(ballAngle) * factor, -std::sin(ballAngle) * factor);

The only difference is the minus sign. It affects the direction where the ball is going at a certain angle. For example, let's say that the ballAngle is 45. In the first piece of code the ball would travel to the bottom right corner. On the second example it would travel to the upper right corner.

Here's a modification of your code that only moves the ball:
Code: [Select]

include<SFML/Graphics.hpp>
#include <cmath>

#define PI 3.14159

 int main()
 {

     sf::RenderWindow App(sf::VideoMode(1024, 768, 32), "SFML/Graphics");

     sf::Image iBall;
     if (!iBall.LoadFromFile("Ball.png"))
     return EXIT_FAILURE;

     iBall.SetSmooth(false);

     sf::Sprite sBall(iBall);

     sBall.SetRotation(45.f);   // Set some angle to demonstrate that it actually moves as it should
     sBall.SetPosition(App.GetWidth()/2, App.GetHeight()/2);  // Put the ball on the center of the screen

     while (App.IsOpened())
     {
         sf::Event Event;

         while (App.GetEvent(Event))
         {
               
             if (Event.Type == sf::Event::Closed) {
                App.Close(); }
               
             if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
         }

         float speed = 80.f * App.GetFrameTime();    // Calculate the speed to move the ball
         
         sBall.Move(cos(sBall.GetRotation()/180*PI)*speed, -sin(sBall.GetRotation()/180*PI)*speed);  // Move the ball (sBall.GetRotation() gives the angle in degrees so it has to be converted to radians. The formula is degrees/180*PI)

         App.Clear();
         App.Draw(sBall);
         App.Display();
     }



     return EXIT_SUCCESS;
 }


EDIT:
Actually it doesn't really make sense to rotate the ball when there's absolutely no need for it. So this:
Code: [Select]

sBall.SetRotation(45.f);   // Set some angle to demonstrate that it actually moves as it should

Could be changed to this:
Code: [Select]

float ballAngle = 45.f;  // Set some angle to demonstrate that it actually moves as it should

And this:
Code: [Select]

sBall.Move(cos(sBall.GetRotation()/180*PI)*speed, -sin(sBall.GetRotation()/180*PI)*speed);  // Move the ball (sBall.GetRotation() gives the angle in degrees so it has to be converted to radians. The formula is degrees/180*PI)

To this:
Code: [Select]

sBall.Move(cos(ballAngle/180*PI)*speed, -sin(ballAngle/180*PI)*speed);  // Move the ball

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
I need help with this game i am trying to make.
« Reply #2 on: April 25, 2011, 04:20:15 pm »
Thanks for your help I'm going to try it. Also do you know collision detection?
I'm tring to learn SFML Help? Collision?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I need help with this game i am trying to make.
« Reply #3 on: April 25, 2011, 04:31:50 pm »
Please avoid full quotes, the original version is just above... ;)
Laurent Gomila - SFML developer

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
I need help with this game i am trying to make.
« Reply #4 on: April 25, 2011, 07:30:34 pm »
Quote from: "Girby2K11"
Thanks for your help I'm going to try it. Also do you know collision detection?

Yes, I know something about collision detection, too.

First it would be useful to know which collisions you would exactly want to detect and how would you want to react to them. You could start by telling what kind of game you are making. :)

It would also be  useful to know how familiar you are with vectors and trigonometric functions. Vectors can sometimes provide really nice solutions.

In the Pong example the collision with the edges of the screen is very simple: when the ball reaches the edge of the screen, either the game ends or the ball's direction is negated. For example, this is the code what happens when the ball hits the top:
Code: [Select]

if (ball.GetPosition().y < 0.f)  // This checks if the ball has reached the top
            {
                ballSound.Play();   // Play the sound
                ballAngle = -ballAngle;   // negate the ball's angle to make it appear as if it bounces off the wall
                ball.SetY(0.1f);  // I'm guessing this is needed to make sure that the ball can't get stuck outside the game area. I tested without it and it appeared to work without it (at least with this setup).
            }


The collision between the paddle and the ball, on the other hand, is a simple box-to-box collision detection. You know the coordinates of the objects and you know their sizes so you can just compare the coordinates taking the sizes into account, and determine whether those objects are on top of each other. In semi-pseudo code it would be something like this (assuming that the images are drawn so that the coordinates are on the upper left corner):
Code: [Select]

If object1.x + object1.width > object2.x And object1.x < object2.x + object2.width And object1.y + object1.height > object2.y And object1.y < object2.y + object2.height Then
    Objects_1_and_2_are_on_top_of_each_other
EndIf

So the detection is basically just comparing x and y coordinates taking the sizes of the objects into account.

I really can't help you more than that before you tell me what you actually want to do. :)

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
I need help with this game i am trying to make.
« Reply #5 on: April 25, 2011, 09:18:26 pm »
Quote from: "Dimple"
Quote from: "Girby2K11"
Thanks for your help I'm going to try it. Also do you know collision detection?

Yes, I know something about collision detection, too.

First it would be useful to know which collisions you would exactly want to detect and how would you want to react to them. You could start by telling what kind of game you are making. :)

It would also be  useful to know how familiar you are with vectors and trigonometric functions. Vectors can sometimes provide really nice solutions.

In the Pong example the collision with the edges of the screen is very simple: when the ball reaches the edge of the screen, either the game ends or the ball's direction is negated. For example, this is the code what happens when the ball hits the top:L
Code: [Select]

if (ball.GetPosition().y < 0.f)  // This checks if the ball has reached the top
            {
                ballSound.Play();   // Play the sound
                ballAngle = -ballAngle;   // negate the ball's angle to make it appear as if it bounces off the wall
                ball.SetY(0.1f);  // I'm guessing this is needed to make sure that the ball can't get stuck outside the game area. I tested without it and it appeared to work without it (at least with this setup).
            }


The collision between the paddle and the ball, on the other hand, is a simple box-to-box collision detection. You know the coordinates of the objects and you know their sizes so you can just compare the coordinates taking the sizes into account, and determine whether those objects are on top of each other. In semi-pseudo code it would be lsomething like this (assuming that the images are drawn so that the coordinates are on the upper left corner):
Code: [Select]

If object1.x + object1.width > object2.x And object1.x < object2.x + object2.width And object1.y + object1.height > object2.y And object1.y < object2.y + object2.height Then
    Objects_1_and_2_are_on_top_of_each_other
EndIf

So the detection is basically just comparing x and y coordinates taking the sizes of the objects into account.

I really can't help you more than that before you tell me what you actually want to do. :)


I want to make a pong game and under stand bounding box collisions. I get really confused with things but after awhile I understand them.
I'm tring to learn SFML Help? Collision?

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
I need help with this game i am trying to make.
« Reply #6 on: April 26, 2011, 03:50:30 pm »
Then I would advice you to take a really good look at the Pong example that comes with the library and ask here if there's something that you don't understand. It has pretty much everything you need for basic physics (at least for a start).

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
I need help with this game i am trying to make.
« Reply #7 on: April 26, 2011, 05:01:29 pm »
Quote from: "Dimple"
Then I would advice you to take a really good look at the Pong example that comes with the library and ask here if there's something that you don't understand. It has pretty much everything you need for basic physics (at least for a start).


Where can  find this Pong example it comes with the sfml download?
I'm tring to learn SFML Help? Collision?

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
I need help with this game i am trying to make.
« Reply #8 on: April 26, 2011, 05:22:27 pm »
Yes, if you downloaded the full SDK or the SFML 2.0 snapshot.

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
I need help with this game i am trying to make.
« Reply #9 on: April 26, 2011, 05:48:59 pm »
Quote from: "Dimple"
Yes, if you downloaded the full SDK or the SFML 2.0 snapshot.


I have just been looking at the code and i don't think i will be able to know all of that stuff. I only wanted simple collision detection for boxes.
I'm tring to learn SFML Help? Collision?

Father_Sloth

  • Newbie
  • *
  • Posts: 48
    • View Profile
I need help with this game i am trying to make.
« Reply #10 on: May 08, 2011, 11:29:40 pm »
Can I ask how you would do collision with the bat if it is drawn from the centre?

Also whenever I use your method for bouncing the ball off the bat it always goes towards the bottom left corner, what am I doing wrong?

 - Simon

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
I need help with this game i am trying to make.
« Reply #11 on: May 09, 2011, 12:06:57 am »
Quote from: "Father_Sloth"
Can I ask how you would do collision with the bat if it is drawn from the centre?

Also whenever I use your method for bouncing the ball off the bat it always goes towards the bottom left corner, what am I doing wrong?

 - Simon


I am not too good with collision detection, ask Dimple, he gave me a really good and quick tutorial for collision detection.
I'm tring to learn SFML Help? Collision?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
I need help with this game i am trying to make.
« Reply #12 on: May 09, 2011, 12:37:50 am »
Girby2K11, you should really not always quote the full post. This has no advantages, but makes it unnecessary hard to read the important parts.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
I need help with this game i am trying to make.
« Reply #13 on: May 09, 2011, 11:04:01 am »
Quote from: "Father_Sloth"
Can I ask how you would do collision with the bat if it is drawn from the centre?

Assuming that you are using bounding box detection, you would use exactly the same code for the detection and only change the coordinates a bit. Let's say that our box's center is at (x,y). Now the upper left corner is at (x-boxWidth/2, y-boxHeight/2), the right upper corner is at (x+boxWidth/2, y-boxHeight/2), the left lower corner is at (x-boxWidth/2, y+boxHeight/2) and the lower right corner is at (x+boxWidth/2, y+boxHeight/2). Now you can use the same code to detect the collision, you only need to change the coordinates (and you would probably only need the upper left corner).
Quote from: "Father_Sloth"

Also whenever I use your method for bouncing the ball off the bat it always goes towards the bottom left corner, what am I doing wrong?

 - Simon

I'm not actually sure whose method you are referring to but it seems that you don't calculate the angle correctly. Seeing the relevant part of the code would be helpful, now I can only guess what's actually wrong.

Father_Sloth

  • Newbie
  • *
  • Posts: 48
    • View Profile
I need help with this game i am trying to make.
« Reply #14 on: May 09, 2011, 07:49:01 pm »
Well I tried implementing your method actually :wink: but I think I've screwed up somewhere as my angle always goes towards the bottom left corner, which results in the opponent gaining a point :( Heres the code anyway:

Collision:
Code: [Select]
bool Collision (const sf::Sprite& a, const sf::Sprite& b)
{

int aX = a.GetPosition().x;
int bX = b.GetPosition().x;
int aY = a.GetPosition().y;
int bY = b.GetPosition().y;
int aW = a.GetSize().x;
int bW = b.GetSize().x;
int aH = a.GetSize().y;
int bH = b.GetSize().y;


if ((aX < (bX + bW)) && (aX + aW) > bX && (aY < (bY + bH)) && ((aY + aH) > bY))
{

return true;

}

return false;

}


The bounce:
Code: [Select]
// Check collision between Bat and ball

if (Collision(PlayerBat, Ball))
{
Ball.Move(cos((ballAngle/180)*PI) * ballSpeed, sin((ballAngle/180)*PI) * ballSpeed);  // Move the ball
}


Thanks Dimple your the man :)

 - Simon