x


0-360 Y degree from Vector3.Angle

Having trouble with my pixel-movement code, which I'm pretty sure is having problems with Vector3.Angle Made in java, kind of stumped.

//loc = Vector2, this moves towards the endpoint.
//endpoint = Vector2, this is stationary. 

 var dir_difference : float = Vector3.Angle(Vector3(loc.x, loc.y), Vector3(endpoint.x, endpoint.y));

I want dir_difference at this point to be 0-360, but it's instead returning 6-9, passing the endpoint without changing very much.

The rest of my code should work well enough, since it's splitting it down to a 0-7 int which I use to move the pixel.

//dir = last direction the pixel moved in. 0-7
dir_difference = Mathf.Repeat  (Mathf.Round(dir_difference / 45 ) , 8); //should return 0-7
dir_2 = dir_difference;
dir_difference = Mathf.Abs(dir - dir_difference); //total difference between angles.  Tells me if it needs to move left or right, and how soon

EDIT: To clarify, I'm looking for something that would return something akin to

225 270 315
180 xxx 000
135 090 045
more ▼

asked Jul 23 '10 at 04:34 PM

windexglow gravatar image

windexglow
13 6 6 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The arguments to Vector3.Angle are themselves directions; you seem to be passing in positions.

Try subtracting the two positions to get the vector: vec = endpoint - loc

The length of this vector is the distance between the two points; the "slope" of the vector is the direction to travel. You can then convert this into an angle using the Vector3.Angle method. But, you need a starting angle. For example, you might choose the X-axis as your starting angle:

var angle : float = Vector3.Angle(Vector3.right, vec);

You can then use this angle to assign an integer (0-7) to the direction as you had planned to originally. (Although, that code may also need tweaking. What's dir_2 for? Is dir the last calculated direction?)

more ▼

answered Jul 23 '10 at 05:29 PM

Bampf gravatar image

Bampf
5.1k 8 20 49

I don't quite understand where to go with that - - my math is embarrassingly rusty.
225 270 315 180 xxx 000 135 090 045

Jul 24 '10 at 09:26 PM windexglow

You originally asked for integers 0-7 but in your new comment it looks like you are rounding to the nearest 45 degree angle? Also, it's not clear whether you wanted 2 or 3 dimensions- you omit the z argument a few times. Can you clarify? Maybe explain what you'll be doing with these numbers. Is this for movement around a grid?

Jul 28 '10 at 10:49 AM Bampf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2244
x599
x300
x38

asked: Jul 23 '10 at 04:34 PM

Seen: 3483 times

Last Updated: Jul 18 '11 at 09:20 PM