SFML community forums
Help => General => Topic started 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:
if(Collision::BoundingBoxTest(Object, Bar))
{
return 0;
}
and nothing happens. Why?
Kind Regards
Fuv
-
Well are they colliding ? (Are the two sprites overlaping each other ?)
-
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
-
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.
-
Yes. Function returns true or false. So if function returns true
if(true)
return 0;
and if not
if(false)
return 0;
Everything is good.
Kind Regards
Fuv
-
So is the problem solved?
-
No. Becouse the program doesnt return if there is collision between these two objects. It is not working.
It doesnt matter between:
if(Function())
{
}
and
if(Function()==true)
{
}
-
Did you try:
if(Collision::BoundingBoxTest(Object, Bar)==true)
{
std::cout<<"Collision occurred!"<<"\n";
}
This is asking if the function exists:
if(Function())
{
}
-
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
if (((Function() == true) == true) == true)
{
...
}
;)
-
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
if (((Function() == true) == true) == true)
{
...
}
;)
I meant if you wrote this it checks if it exists:
if(Collision::BoundingBoxTest){}
Without a value isn't the same thing as
if(true){}
but it also means
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:
if(Collision::BoundingBoxTest(Object, Bar)==true){}
Are you sure you don't mean this sets the value?
if(Collision::BoundingBoxTest(Object, Bar)=true){}
-
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?
-
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?
-
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.
-
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.
-
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!
-
Well after checking the code it appears there is two ways to do it and both work.
Well,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.
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. ;)
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).
-
Dont argue with each other, becouse it gives me nothing! Tell me why collision is not working...
-
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 ?
-
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.
-
I don't see anything wrong with the small piece of code you posted Fuv.
-
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.
-
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.
#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
-
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 :
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 ?
-
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.
Your code seeme to test just only one time the collision.
I don t know how to make it other way.
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
-
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.
-
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.
-
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.
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.
-
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.
-
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:
if(Collision::BoundingBoxTest(Ludzik, Przesz))
{
//here
}
To make real collision. I found no examples in wiki.
Kind Regards
Fuv
-
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:
-
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
-
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.
-
But I dont have even any idea. I tried like that, but not working.
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.