SFML community forums
Help => Graphics => Topic started by: KasHKoW on September 11, 2011, 02:54:40 am
-
I have two different instances of the same cold. One works as accordingly planned and the other messes up. Possibly I dont' see the erroneous lines of code that I posted. I was therefore looking for some help on finding them or identifying the underlying problem.
Code 1 that works
#include <SFML/Graphics.hpp>
#define CIRCLE sf::Shape::Circle(0.f, 300.f, 5.f, sf::Color::Red, 1.f, sf::Color::Blue);
#define CIRCLE1 sf::Shape::Circle(800.f, 300.f, 5.f, sf::Color::Red, 1.f, sf::Color::Blue);
int main () {
sf::RenderWindow App(sf::VideoMode(800,600,32), "Horizontal Circles");
App.SetFramerateLimit(60);
sf::Shape C1 = CIRCLE;
sf::Shape C2 = CIRCLE1;
int dir = 0;
int dir2 = 0;
while(App.IsOpened()) {
sf::Event ev1;
if(App.GetEvent(ev1))
if(ev1.Type == sf::Event::Closed)
App.Close();
if(dir == 1 && C1.GetPosition().x < 2.f)
dir = 0;
if(dir == 0 && C1.GetPosition().x > 798.f) {
dir = 1;
}
if (dir == 1 )
C1.Move(-2.2,0.f);
if (dir == 0)
C1.Move(2.2, 0.f);
if(dir2 == 1 && C1.GetPosition().x < 2.f)
dir2 = 0;
if(dir2 == 0 && C1.GetPosition().x > 798.f) {
dir2 = 1;
}
if (dir2 == 0 )
C2.Move(-2.2,0.f);
if (dir2 == 1)
C2.Move(2.2, 0.f);
App.Clear();
App.Draw(C1);
App.Draw(C2);
App.Display();
}
return EXIT_SUCCESS;
}
This is code 2. It does half of what it's suppose to
#include <SFML/Graphics.hpp>
#include <iostream>
#define CIRCLE sf::Shape::Circle(0.f, 300.f, 5.f, sf::Color::Red, 1.f, sf::Color::Blue);
#define CIRCLE1 sf::Shape::Circle(800.f, 280.f, 5.f, sf::Color::Green, 1.f, sf::Color::Blue);
int main () {
sf::RenderWindow App(sf::VideoMode(800,600,32), "Horizontal Circles");
App.SetFramerateLimit(60);
sf::Shape C1 = CIRCLE;
sf::Shape C2 = CIRCLE1;
int dir = 0;
int dir2 = 0;
while(App.IsOpened()) {
sf::Event ev1;
if(App.GetEvent(ev1))
if(ev1.Type == sf::Event::Closed)
App.Close();
//where ball 1 testing begins
if(dir == 0 && 800.f - C1.GetPosition().x < 1.0 ){
//std::cout << (800.f- (C1.GetPosition().x)) << "Red This is Direction 0 ch to 1\n";
dir = 1;
}
if(C1.GetPosition().x - 2.f < 1.0 && dir == 1) {
dir = 0;
//std::cout << ((C1.GetPosition()).x - 2.f ) << "Red This is Direction 1 ch to 0\n";
}
//where ball 2 testing begins
if(dir2 == 1 && C1.GetPosition().x < 2.f){
dir2 = 0;
//change
}
if(dir2 == 0 && C1.GetPosition().x > 799.f) {
dir2 = 1;
}
//where ball2 movement two begins
if (dir2 == 0 )
C2.Move(-2.2,0.f);
if (dir2 == 1)
C2.Move(2.2, 0.f);
// This is where Ball movement 1 begins
if (dir == 1 ){
C1.Move(-2.2,0.f);
// std::cout << C1.GetPosition().x << std::endl;
}
if (dir == 0) {
C1.Move(2.2, 0.f);
// std::cout << C1.GetPosition().x << std::endl;
}
App.Clear();
App.Draw(C1);
App.Draw(C2);
App.Display();
}
return EXIT_SUCCESS;
}
-
I'm trying to spot the difference in the code but can't, can you highlight what differentiate between the two sources?
-
Exactly, nothing does differentiate.
Well except how ball 1 test arguments work. but ball 1 works perfectly.
ball 2 on the other hand using the same code in both sources acts differently
-
Actually this differentiate:
if(dir2 == 0 && C1.GetPosition().x > 799.f) {
dir2 = 1;
}
It compares against 798 in the other. Though I don't think that is the problem.
Are you a 100% sure that it is the green ball that doesn't work? Also exactly how does it not work? Does it not move or what?
-
Oh yeah... I was testing different things... like since I was moving every loop 2.2 i just changed the argument to ... idk it doesn't matter... looking back it was senseless. Anyhow the balls simple go back and forth, starting from one end and ending from the other. Once that is complete, they flip and do that repetitively in till the program ends. That happens in the first one... Although in the second one the second ball actualy does that and when coming back to the point where it originally began it goes right passed it and off the actual screen bypassing the if test that's suppose to switch its direction and make it go back in the direction it was just coming from.
Yes 100% sure its the green ball.
Also, I think it could be an internal problem in SFML. Possibly how the sprites are tested. At first I thought it could possibly be using a hybrid approach of testing if these objects met criteria to affect its behavior. But that doesn't seem to be the case and doesn't make sense as well.
If its not an of that, then its definitely my code.
-
Alright second question... Any specific reason you are doing ball2s test against circle 1's position? It might be that which is the problem. You do it in the first example but you might have a freaky accident of it just working for some reason "because the stars in the sky is aligned exactly right".
-
HOLY CRAP!!! haha i didn't even see that... smhv(violently)
that's so funny. Alright, this is where I officially stop using variables with too much alike names.
-
Hmm, i just thought of something. Lets say I had a bunch of different objects that just needed to move across the screen and they started at positions like that... could i just simply have 1 test like seen there in the first code to trigger all the balls movements back n forth decreasing overhead of a bunch of if tests.
P.S. I spent so many hours on that...
-
This is a text book case of one of programmers enemies... "Code Blind", you've been staring so much at your code for so long that you have gone blind to what is wrong in it. For your eyes everything looks alright but when an outsider looks at it he has a fresh view and can most often spot the error immediately.
This is some of the reasons why programmers are in reality social. From my experience, we are as most efficient when we can communicate ideas and thoughts with others around us.