SFML community forums

Help => General => Topic started by: Fuv on June 09, 2010, 04:46:31 pm

Title: Collision - example(?)
Post by: Fuv on June 09, 2010, 04:46:31 pm
I read about collision at wiki:
http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection
I added that class to my program, but I dont know how to use it at all. There isnt any example...

I have two Sprites - Object and Bar. And now how to set collision?

I wrote code:
Code: [Select]
if(Collision::BoundingBoxTest(Object, Bar))
   {
return 0;
   }

and nothing happens. Why?

Kind Regards
Fuv
Title: Collision - example(?)
Post by: G. on June 09, 2010, 07:19:35 pm
Well are they colliding ? (Are the two sprites overlaping each other ?)
Title: Collision - example(?)
Post by: Fuv on June 09, 2010, 07:43:45 pm
Sure.
One image is over the 2nd one.

Screenshot:
http://img96.imageshack.us/img96/4148/screenshotfq.jpg
A small sprite behind the big one.

Kind Regards
Fuv
Title: Collision - example(?)
Post by: Ashenwraith on June 09, 2010, 08:50:13 pm
I'm kinda rusty at C++ so I don't know if this syntax is proper but assuming it is..

After taking a quick peak it looks like that function returns a bool so your if check should ask if true or false.
Title: Collision - example(?)
Post by: Fuv on June 09, 2010, 09:12:37 pm
Yes. Function returns true or false. So if function returns true

Code: [Select]
if(true)
return 0;


and if not
Code: [Select]
if(false)
return 0;


Everything is good.

Kind Regards
Fuv
Title: Collision - example(?)
Post by: Ashenwraith on June 09, 2010, 09:25:10 pm
So is the problem solved?
Title: Collision - example(?)
Post by: Fuv on June 09, 2010, 09:27:22 pm
No. Becouse the program doesnt return if there is collision between these two objects. It is not working.
It doesnt matter between:
Code: [Select]
if(Function())
{
}

and
Code: [Select]
if(Function()==true)
{
}
Title: Collision - example(?)
Post by: Ashenwraith on June 09, 2010, 09:35:05 pm
Did you try:

Code: [Select]

if(Collision::BoundingBoxTest(Object, Bar)==true)
{
      std::cout<<"Collision occurred!"<<"\n";
}


This is asking if the function exists:

Code: [Select]

if(Function())
{

}
Title: Collision - example(?)
Post by: Laurent on June 09, 2010, 09:41:33 pm
Quote
This is asking if the function exists

No no, this is calling the function and testing the boolean it returns. What your code does is to transform the boolean into another boolean of the same value.

Following your point of view, we could as well write
Code: [Select]
if (((Function() == true) == true) == true)
{
   ...
}

;)
Title: Collision - example(?)
Post by: Ashenwraith on June 09, 2010, 09:52:00 pm
Quote from: "Laurent"
Quote
This is asking if the function exists

No no, this is calling the function and testing the boolean it returns. What your code does is to transform the boolean into another boolean of the same value.

Following your point of view, we could as well write
Code: [Select]
if (((Function() == true) == true) == true)
{
   ...
}

;)


I meant if you wrote this it checks if it exists:

Code: [Select]

if(Collision::BoundingBoxTest){}


Without a value isn't the same thing as
Code: [Select]

if(true){}

but it also means
Code: [Select]

if(false){}


Meaning there is no difference in the choice execution of the code?

I use this in javascript all the time and it works:

Code: [Select]

if(Collision::BoundingBoxTest(Object, Bar)==true){}



Are you sure you don't mean this sets the value?

Code: [Select]

if(Collision::BoundingBoxTest(Object, Bar)=true){}
Title: Collision - example(?)
Post by: Fuv on June 09, 2010, 10:05:18 pm
It is not working. Can you tell me how collision should look like? Or what is wrong in my code? Or maybe you need more code?
Title: Collision - example(?)
Post by: Fuv on June 09, 2010, 10:05:57 pm
It is not working. Can you tell me how collision should look like? Or what is wrong in my code? Or maybe you need more code?
Title: Collision - example(?)
Post by: Ashenwraith on June 09, 2010, 10:11:01 pm
Quote from: "Fuv"
It is not working. Can you tell me how collision should look like? Or what is wrong in my code? Or maybe you need more code?


Well one problem it could be is that depending on what sfml version you are using the Rectangle code has changed so you might have to alter that.

But still it should be returning either true or false or at least crashing/erroring out.
Title: Collision - example(?)
Post by: Laurent on June 09, 2010, 10:12:38 pm
Ashenwraith, I don't get your point. In its first piece of code there are parenthesis and arguments, so it's a function call which returns a boolean.
Title: Collision - example(?)
Post by: Ashenwraith on June 09, 2010, 10:20:13 pm
Quote from: "Laurent"
Ashenwraith, I don't get your point. In its first piece of code there are parenthesis and arguments, so it's a function call which returns a boolean.


Well after checking the code it appears there is two ways to do it and both work.

I didn't know there was a default behavior for checking true and false with ! and the compiler was smart enough to figure it out.

When I learned C++ you had to write your own BOOL!
Title: Collision - example(?)
Post by: Nexus on June 09, 2010, 11:19:08 pm
Quote from: "Ashenwraith"
Well after checking the code it appears there is two ways to do it and both work.
Well,
Code: [Select]
if(Function)is completely useless in every case, because the if condition is always true. You can't check the existence of a function or something like this.

Quote from: "Ashenwraith"
I didn't know there was a default behavior for checking true and false with ! and the compiler was smart enough to figure it out.
It's the common way to check bool conditions, this is not even function-related. The compiler doesn't have to be smart at all. ;)

Quote from: "Ashenwraith"
When I learned C++ you had to write your own BOOL!
Did you learn C++ before 1998? Even then, I don't think this was ever the case, since even C can handle boolean expressions like this (take any integer instead of bool).
Title: Collision - example(?)
Post by: Fuv on June 10, 2010, 07:12:59 am
Dont argue with each other, becouse it gives me nothing! Tell me why collision is not working...
Title: Collision - example(?)
Post by: Mindiell on June 10, 2010, 08:54:43 am
Quote from: "Ashenwraith"
Quote from: "Fuv"
It is not working. Can you tell me how collision should look like? Or what is wrong in my code? Or maybe you need more code?


Well one problem it could be is that depending on what sfml version you are using the Rectangle code has changed so you might have to alter that.
Seems Ashenwraith asked you something interesting, no ?
Title: Collision - example(?)
Post by: Hiura on June 10, 2010, 10:17:37 am
Just one point :
In C++ you can't check (with an if statement or anything else) the existence of a function (or even a class). It's because C++ is a static language. For example in Objective-C you can do these checks because it's a dynamic language.

The only way to know if a class/function/whatever exists is to try using it and see if the compiler yells at you.
Title: Collision - example(?)
Post by: G. on June 10, 2010, 10:55:31 am
I don't see anything wrong with the small piece of code you posted Fuv.
Title: Collision - example(?)
Post by: Ashenwraith on June 10, 2010, 02:50:57 pm
Quote from: "Nexus"
Quote from: "Ashenwraith"
When I learned C++ you had to write your own BOOL!
Did you learn C++ before 1998? Even then, I don't think this was ever the case, since even C can handle boolean expressions like this (take any integer instead of bool).


http://www.google.com/search?hl=en&source=hp&q=compiler+bool+support&btnG=Google+Search

You must be pretty new to programming if you don't know this. I'm only 26 and C++ was a lot different in the 90's. A lot of the compilers (most?) did not have a built in boolean and you had to make your own or find one to include. Writing your own boolean was seen as a cross-compiler/platform solution and useful because you could make it an int or whatever you wanted for your program's specific needs.

Because of this Computer Science courses discouraged dependency on any built-in boolean data type.
Title: Collision - example(?)
Post by: Fuv on June 10, 2010, 03:58:02 pm
Im not interested in what u are arguing about now. I just want to know why my collision is not working and so far it seems no one knows. I give you all my code, so if here is somebody who can explain me why it is not working I would be very, very grateful.

Code: [Select]
#include <SFML/Graphics.hpp>
#include "collision.h"
#include <iostream>
 
using namespace sf;
 
 
int main()
{
RenderWindow App(VideoMode(800, 600, 32), "Game");

App.ShowMouseCursor(false);



   sf::Image IMGBackground, IMGLudzik, IMGKursor;
   
   if(!IMGBackground.LoadFromFile("back.tga"))
      return 1;
   if(!IMGLudzik.LoadFromFile("lud.tga"))
      return 1;
   if(!IMGKursor.LoadFromFile("kursor.tga"))
  return 1;
 
   IMGKursor.CreateMaskFromColor(Color(255,0,253), 0);
   IMGLudzik.CreateMaskFromColor(Color(0,0,0), 0); //przezroczystosc na czarny
   Sprite Background(IMGBackground);
   Sprite Ludzik(IMGLudzik);
   Sprite Przesz(IMGLudzik);
   Sprite Kursor(IMGKursor);

 
   Background.SetPosition(0.f, 0.f);
   Background.Resize(800, 600);
 
   Ludzik.SetPosition(400.f, 350.f);
   Ludzik.SetScale(0.5f, 0.5f);

   Przesz.SetPosition(300.f, 300.f);
   
   Kursor.SetPosition(500.f, 500.f);
 
   Event evt;
   
   if(Collision::BoundingBoxTest(Ludzik, Przesz)==true)
   {
  std::cout<<"Ble";
return 0;
   }

   while(App.IsOpened())
   {
      while(App.GetEvent(evt))
      {
         if(evt.Type == Event::Closed)
            App.Close();
         if(evt.Type == Event::KeyPressed && evt.Key.Code == Key::Escape)
            App.Close();
if (evt.Key.Code == sf::Key::F1)
{
sf::Image Screen = App.Capture();
Screen.SaveToFile("screenshot.jpg");
         }
 
      }
 
      float time = App.GetFrameTime();
      if(App.GetInput().IsKeyDown(Key::Up))
         Ludzik.Move(0, -100 * time);
      if(App.GetInput().IsKeyDown(Key::Down))
         Ludzik.Move(0, 100 * time);
      if(App.GetInput().IsKeyDown(Key::Left))
         Ludzik.Move(-100 * time, 0);
      if(App.GetInput().IsKeyDown(Key::Right))
         Ludzik.Move(100 * time, 0);
 
 
      App.Clear();
      App.Draw(Background);
      App.Draw(Ludzik);
 App.Draw(Przesz);
 sf::Vector2f CursorPos = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
      Kursor.SetPosition(CursorPos);
      App.Draw(Kursor);
      App.Display();
   }
}


Kind Regards
Fuv
Title: Collision - example(?)
Post by: Mindiell on June 10, 2010, 04:10:26 pm
Quote from: "Fuv"
Im not interested in what u are arguing about now. I just want to know why my collision is not working and so far it seems no one knows. I give you all my code, so if here is somebody who can explain me why it is not working I would be very, very grateful.
Don't have to be unpolite, everybody can speak about lto of things on those forums. Let's ask them to do it on another thread could be smarter ;)

By the Way :
Code: [Select]

   Ludzik.SetPosition(400.f, 350.f);
   Ludzik.SetScale(0.5f, 0.5f);

   Przesz.SetPosition(300.f, 300.f);
   
   if(Collision::BoundingBoxTest(Ludzik, Przesz)==true)
   {
  std::cout<<"Ble";
return 0;
   }

Your code seeme to test just only one time the collision. What are the size of the two images ? Because maybe "Przesz" is not 100px width nor 50px height...
Can you show us the BoundingBoxTest function too please ?
Title: Collision - example(?)
Post by: Fuv on June 10, 2010, 04:44:50 pm
Quote
Your code seeme to test just only one time the collision. What are the size of the two images ? Because maybe "Przesz" is not 100px width nor 50px height...


Does it have to be exactly 100x50? Why? I have the same images and one was scaled to 0,5height and 0,5 width. But also after making it without scaling it is not working.

Quote
Your code seeme to test just only one time the collision.


I don t know how to make it other way.

Quote
Can you show us the BoundingBoxTest function too please ?


As I said at the beginning(but becouse of that off-topic it is not clear) I m using wiki example from there:
http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection

Kind Regards
Fuv
Title: Collision - example(?)
Post by: G. on June 10, 2010, 07:19:13 pm
You call the collision function outside of the main loop. It is called only one time, just after the creation of your sprites.
You should call the BoudingBoxTest function inside your main loop, then move your sprite onto the other and it will return true.
Title: Collision - example(?)
Post by: Nexus on June 10, 2010, 08:06:31 pm
Quote from: "Ashenwraith"
You must be pretty new to programming if you don't know this. I'm only 26 and C++ was a lot different in the 90's. A lot of the compilers (most?) did not have a built in boolean and you had to make your own or find one to include.
I only referred to the statements if (expr) or if (!expr), which were already in C possible to write, where no bool has ever existed. Sorry if I didn't express myself clearly.
Title: Collision - example(?)
Post by: Ashenwraith on June 11, 2010, 04:05:45 am
Quote from: "Nexus"
Quote from: "Ashenwraith"
You must be pretty new to programming if you don't know this. I'm only 26 and C++ was a lot different in the 90's. A lot of the compilers (most?) did not have a built in boolean and you had to make your own or find one to include.
I only referred to the statements if (expr) or if (!expr), which were already in C possible to write, where no bool has ever existed. Sorry if I didn't express myself clearly.


I'm not trying to argue or anything.

A lot of people set their bools to ints to return 0 along with other stuff so I don't see how that would be possible, but before this year I wrote my last C++ program around 99/2000 so I don't know.

But I don't really care, I learned something new to apply so I'm happy. That's what I'm here for.

Quote from: "Fuv"
Im not interested in what u are arguing about now. I just want to know why my collision is not working and so far it seems no one knows. I give you all my code, so if here is somebody who can explain me why it is not working I would be very, very grateful.


Hey we were waiting for you to post code and we told you might have rectangle problems if your SFML is incompatible (if you are using 2.x, I don't think it's been updated for 1.6).

From a quick peak I can see you need to put your if collision check inside of your app's while loop like G said. It's a common mistake.
Title: Collision - example(?)
Post by: Mindiell on June 11, 2010, 11:02:56 am
Quote from: "Fuv"
Quote
Your code seeme to test just only one time the collision. What are the size of the two images ? Because maybe "Przesz" is not 100px width nor 50px height...


Does it have to be exactly 100x50? Why? I have the same images and one was scaled to 0,5height and 0,5 width. But also after making it without scaling it is not working.

Nop, thoses sizes where the minimas in order to make a collision between the two sprites.
As G. said, you have to do the test collision after each movement, in the main loop.
Title: Collision - example(?)
Post by: Fuv on June 11, 2010, 05:35:49 pm
Thanks. It works.

And could you tell me what is yours idea to write condition? I mean I dont know what to write in here:

Code: [Select]
if(Collision::BoundingBoxTest(Ludzik, Przesz))
      {
              //here
      }


To make real collision. I found no examples in wiki.

Kind Regards
Fuv
Title: Collision - example(?)
Post by: Hiura on June 11, 2010, 06:45:57 pm
depends on your objects : they may explodes, or disappear, or bounce, or split, or make fireworks, or transform into a LapinGeantDevoreurDePlanète, or ... I think you get my point  :wink:
Title: Collision - example(?)
Post by: Fuv on June 11, 2010, 06:56:07 pm
Heh, yeah indeed.

I just want that one object can not go into the 2nd one. I want to make that one cannot cover 2nd in one place. No explosions there :)

Kind Regards
Fuv
Title: Collision - example(?)
Post by: Hiura on June 11, 2010, 07:08:06 pm
so ? what's your problem ?

Try to describe on paper what you want (geometry, pseudocode, ...) and then try to implement it.
If you have a problem on a precise point ask for help. But we won't do all the stuff for you. That's no help for you in the long run.
Title: Collision - example(?)
Post by: Fuv on June 11, 2010, 09:21:37 pm
But I dont have even any idea. I tried like that, but not working.

Code: [Select]
    if(!Collision::BoundingBoxTest(Ludzik, Przesz))
{
if(App.GetInput().IsKeyDown(Key::Up))
Ludzik.Move(0, -100 * time);
   if(App.GetInput().IsKeyDown(Key::Down))
    Ludzik.Move(0, 100 * time);
   if(App.GetInput().IsKeyDown(Key::Left))
    Ludzik.Move(-100 * time, 0);
if(App.GetInput().IsKeyDown(Key::Right))
    Ludzik.Move(100 * time, 0);
}


I dont know how it should be solved.