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

Author Topic: Collision - example(?)  (Read 13061 times)

0 Members and 2 Guests are viewing this topic.

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Collision - example(?)
« 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

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Collision - example(?)
« Reply #1 on: June 09, 2010, 07:19:35 pm »
Well are they colliding ? (Are the two sprites overlaping each other ?)

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Collision - example(?)
« Reply #2 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

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Collision - example(?)
« Reply #3 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.

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Collision - example(?)
« Reply #4 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

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Collision - example(?)
« Reply #5 on: June 09, 2010, 09:25:10 pm »
So is the problem solved?

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Collision - example(?)
« Reply #6 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)
{
}

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Collision - example(?)
« Reply #7 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())
{

}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Collision - example(?)
« Reply #8 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)
{
   ...
}

;)
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Collision - example(?)
« Reply #9 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){}

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Collision - example(?)
« Reply #10 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?

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Collision - example(?)
« Reply #11 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?

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Collision - example(?)
« Reply #12 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Collision - example(?)
« Reply #13 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.
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Collision - example(?)
« Reply #14 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!