Make an image face the direction its travelling

Started by
16 comments, last by dpadam450 12 years, 4 months ago

You need to get a book on coding and take step by step because the fact that you are this lost you need to take a step back and get something that will teach you step by step.


double angle = 180.0 / 3.14 * Math.Atan(bug.getPos()-> bug.getVel().Y, bug.getPos()->bug.getVel().X);
Why are you putting in position when my code takes atan(velocityY/velocityX). You threw in 2 extra pieces of data and then called the positions as if they are pointers?

You also need to look up the C sharp Atan function and see if it gives radians or degrees. With radians you need the 180/3.14, with degrees you don't. If you still have trouble, then spend a few bucks and buy a used C# book.


I'd down vote you if I could. He's asking a math question, not a programming question. The only thing he's done wrong here is post in the wrong forum.

I'll point out, I'm not a math wiz, so my terminology is probably off, but you should get the idea, and I'm assuming you want this all to be in 2D.

Now lets assume, the angle were going to be facing in is in radians (not degrees), and vec2 is a structure with a x and a y value.


Vec2 charPosition; // = the character we what to face towards our target.
Vec2 targetPosition; // = what were are trying to look at.

// to calculate the angle or character needs to be at to face the target.
angle = atan2(targetPosition.y - charPosition.y, targetPosition.x - charPosition.y);

// now if you want to find out how much on either axis you should move each step
xVelocity = cos(angle);
yVelocity = sin(angle);

// You would then normalize those velocities and multiply them by the frame delta, and your desired velocity per second.


Once again, this assume your angle is in radians, and that atan2, cos and sin deal with radians. They do in C++, but you should verify they do in C# aswell.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
Well he missed the theory and the code I posted, which if you look at his next try code, it is completely random guessing, not the theory, but the programming itself. Hes grabbing the velocity of the position and said "that gives me an error though".

You don't need to find the length. A triangle uses proportions. As the x grows, the y gets smaller, as the y grows the x gets smaller. It is simply atan(y/x). Your velocity makes a triangle, one edge is he x direction and the other edge is the y direction. Atan computes the angle given 2 leg lengths of a triangle (in this case tan = opposite/adjacent = y/x). Whether you normalize the vector first or not does not change the value of y/x

y/x = A
normalize(x and y ) = y/length , x/length
(y/length)/(x/length) = A still

9/3 = 3
If length of vector = 9.4
normalize(y and x) = .957 , .319
.957/.319 = 3 still, so no need to do anything other than:

take the Atan of the velocity y divided by x. That gives you either an angle in degrees or radians.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

And of course if the direction x is left, then look at my original code post.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Thanks for the reply free world.
I've been told I need to find the length, as the velocity is constantly updating. Also I want them to face the way they are travelling not just to face the coordinates of an object on the screen, which makes things a little more difficult?

I will try that tomorrow dpadam, but my lecture said once I normalise the vector I take that and multiply it by the velocity, to get a unit vector, then pass that into atan2?
Well if you would just try the 1 line of code that is all you need:
http://msdn.microsoft.com/en-us/library/system.math.atan.aspx

What you are referring to is, calculating a velocity each frame which your object is moving, normalizing it, and then multiplying by SPEED, not multiplying by velocity. Velocity is a direction, speed is a scalar magnitude. East is a vector, 90mph is a speed. East*90mph = 90 mph going East. Typically you store velocity direction as unit length with a speed to control speed.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


Well if you would just try the 1 line of code that is all you need:
http://msdn.microsof....math.atan.aspx

What you are referring to is, calculating a velocity each frame which your object is moving, normalizing it, and then multiplying by SPEED, not multiplying by velocity. Velocity is a direction, speed is a scalar magnitude. East is a vector, 90mph is a speed. East*90mph = 90 mph going East. Typically you store velocity direction as unit length with a speed to control speed.



I've done this and it doesnt work:

http://pastebin.com/4HYTe6Yx

it rotatates them but not in the right direction and some of them dissapear when they reach the center of the screen, might just give up on this.

Before I do though, I'm calling that in the draw method which is called as many timers a second as possible, is that right?

Thanks for the reply free world.
I've been told I need to find the length, as the velocity is constantly updating. Also I want them to face the way they are travelling not just to face the coordinates of an object on the screen, which makes things a little more difficult?

I will try that tomorrow dpadam, but my lecture said once I normalise the vector I take that and multiply it by the velocity, to get a unit vector, then pass that into atan2?


If they aren't facing a point in space, what are they facing towards?

Angle, is the direction they are facing. You need to determine what they are facing at, otherwise your question is mute, and you just need to determine the amount of rotation you want them to face... ie just pick a number.

You're trying to do everything backwards, which I believe is confusing you. You're trying to convert a 2D vector into an angle. You should do it the other way, face the object were you want to face them, then calculate how much you need to step on either axis to move in that direction.

and this is the last time I will visit this post. good luck and happy coding, keep at it.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Well in trigonometry, 0 degrees is pointing to the right, 90 points up. So is your image without rotation pointing up or is it pointing to the right. If velocity = 1,0, then your angle would be 0, and imply your object should not be rotated. So an unrotated image needs to be facing to the right. Either subtract 90 degrees from the rotation, or export your image facing to the right.

Again you are putting in all kinds of random steps to fix this problem. For instance where do you translate the bug to its position?

//Rotate image first, then translate
glTranslate(bug position)
glRotate(angle based on velocity)

In your code, translation is probably in the .Draw() method which means your doing that wrong as well because you are translating first.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement