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

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

0 Members and 2 Guests are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Collision - example(?)
« Reply #15 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).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fuv

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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Collision - example(?)
« Reply #17 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 ?
Mindiell
----

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Collision - example(?)
« Reply #18 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.
SFML / OS X developer

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Collision - example(?)
« Reply #19 on: June 10, 2010, 10:55:31 am »
I don't see anything wrong with the small piece of code you posted Fuv.

Ashenwraith

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

Fuv

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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Collision - example(?)
« Reply #22 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 ?
Mindiell
----

Fuv

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

G.

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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Collision - example(?)
« Reply #25 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ashenwraith

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

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Collision - example(?)
« Reply #27 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.
Mindiell
----

Fuv

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

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Collision - example(?)
« Reply #29 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:
SFML / OS X developer

 

anything