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

Author Topic: Possibly two different instances of the same code don't work  (Read 3456 times)

0 Members and 1 Guest are viewing this topic.

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Possibly two different instances of the same code don't work
« 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
Code: [Select]

#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
Code: [Select]

#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;
}

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Possibly two different instances of the same code don't work
« Reply #1 on: September 11, 2011, 03:05:00 am »
I'm trying to spot the difference in the code but can't, can you highlight what differentiate between the two sources?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Possibly two different instances of the same code don't work
« Reply #2 on: September 11, 2011, 03:07:12 am »
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

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Possibly two different instances of the same code don't work
« Reply #3 on: September 11, 2011, 03:14:26 am »
Actually this differentiate:
Code: [Select]
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?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Possibly two different instances of the same code don't work
« Reply #4 on: September 11, 2011, 03:33:59 am »
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.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Possibly two different instances of the same code don't work
« Reply #5 on: September 11, 2011, 03:37:19 am »
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".
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Possibly two different instances of the same code don't work
« Reply #6 on: September 11, 2011, 03:54:31 am »
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.

KasHKoW

  • Newbie
  • *
  • Posts: 41
    • View Profile
Possibly two different instances of the same code don't work
« Reply #7 on: September 11, 2011, 03:56:27 am »
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...

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Possibly two different instances of the same code don't work
« Reply #8 on: September 11, 2011, 04:12:14 am »
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.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything