Values wont stay negative

I have a function that rotates a vector 90 degrees. Only problem is, i never get negative values. For some strange reason, no matter what i do, the value of x is positive. And even if the x is negative. Once i put it into direction (struct with 2 values x and y) it turns again to positive. Im using VSC++2013 ultimate. Can anyone tell me what is causing this strange problem. At first direction was SDL_Point, so i thought it was SDL problem, but now it seems its something else.

1
2
3
4
5
6
7
		if (c == '-')
		{
			int x =  direction.y;
			x *= -1;
			int y = direction.x;
			direction = { x, y };
		}
what is direction? How y and x are declared?
Direction just tells which way to move next. it should have 4 states. (0,5) (5,0) (0,-5) (-5,0)

1
2
3
4
5
6
typedef struct Point{
int x;
int y;
}Point;

Point direction;
Last edited on
Code should work fine. Try placing breakpoint inside this if statement. I think that execution does not run these statements at all. Mayby problem is with your conditions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
struct Point
{
    int x;
    int y;
};

int main()
{
	Point direction {5, 1};
    int x =  direction.y;
    x *= -1;
    int y = direction.x;
    direction = { x, y };
    std::cout << direction.x << ' ' << direction.y;
}
-1 5
So i made it like this. Its inside the if statement. Since it gets printed out, the condition is initialized. But i always get positive values. I have another if statement like this. It turns the other way and it seems to be working fine. I get occasional negative values from there.

1
2
int x =  direction.y * -1;
			cout << x;
Are you sure that both those statements are not execute at the same time negating each other?

Can you post minimal code example which reprodices problem?
Oh ok... I found the problem. The problem was elsewhere. The list of commands it uses to doe the flips, it has faults in it. Need to fix that
This code looks okay to me. I notice that the code does NOT rotate the angle. It reflects it about the Y axis. So I suspect the problem is elsewhere. Can you post the full rotation code?
It rotates point 90° counterclockwise around point [0; 0]. Everything is correct here.
Topic archived. No new replies allowed.