Hey, I'm trying to use trig to create a rotating tank (you rotate the tank by pressing left and right, and move forward and back by using up and down). I'm using this formula (C++):

tank.movex = cos(tank.direction*0.0174532925)*tank.move;
tank.movey = sin(tank.direction*0.0174532925)*tank.move;
tank.x += tank.movex;
tank.y += tank.movey;

tank.move is 5 if the up arrow is pressed and it's -5 if the down arrow is pressed. tank.direction tank.direction increases by 3 when your press the left arrow, and it decreases by 3 when you press the right arrow (i have a function make sure that it's within the range 0-359 inclusive)

should this work? because when the angle gets to 45 degrees it starts acting like it's at 135.

Recommended Answers

All 5 Replies

does it only start acting like that once it hits 45 degree's, or is it acting like that the entire time? if its acting like that the entire time, just subtract 90 degree's from it.

does it only start acting like that once it hits 45 degree's, or is it acting like that the entire time? if its acting like that the entire time, just subtract 90 degree's from it.

Simply subtracting 90 doesn't work. This makes the movement when 0 is the direction weird, while it's fine when I don't subtract 90. Besides, I wouldn't want to just subtract 90 without knowing why I'm doing it.

Okay, the problem wasn't what I mentioned above. For some reason the expression that tank.movey equals is always the negative of what it should be. I can multiply the expression by -1 to make it work, but I don't like randomly multiplying things in my code just because I found that they work. I want to find what's causing the problem. Does anyone have any ideas?

As long as its always the negative of what it should be; you're ok to put in a * -1, you could also try swapping the cos and sin exprs around, so that movex is the sin, and movey is the cos. I wouldn't worry about it, the problem arises when you're using a diferent idea of what up/down left/right and 0 rads is to the maths itself.. In 2D, I always find that 0 rads in a formula ends up pointing over to the right on screen rather than the oft-desired up, but thats easily solved by adjusting the input angle. It's a good idea to establish a convention early on, and encapsulate it away so you can change your easily mind later.

I think I found the problem. I forgot that I was working in a world where y increases as it goes down, not up. So the values were actually what I expected them to be, those values just didn't produce the result I wanted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.