最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Determine movement vector's direction from velocity - Stack Overflow

programmeradmin0浏览0评论

I'm kinda confused with this one.

I have an object and I know it's velocities on axis x and y. My problem is how to determine the angle at which it's moving.

function Object(){
    this.velocity = {x: 5, y: 1};
}

Basically I Know that a vector's direction is x_projectioncos(deg) + y_projectionsin(deg), but I don't know how to get those projections since I only have the velocity, as I said I'm really confused.

#EDIT:

in addition to the accepted answer, here's what I did to get a full 360 degree spectrum

var addDeg = 0;
if(obj.velocity.x<0)
    addDeg = obj.velocity.y>=0 ? 180 : 270;
else if(obj.velocity.y<=0) addDeg = 360;

deg = Math.abs(Math.abs(Math.atan(obj.velocity.y/obj.velocity.x)*180/Math.PI)-addDeg)

I'm kinda confused with this one.

I have an object and I know it's velocities on axis x and y. My problem is how to determine the angle at which it's moving.

function Object(){
    this.velocity = {x: 5, y: 1};
}

Basically I Know that a vector's direction is x_projectioncos(deg) + y_projectionsin(deg), but I don't know how to get those projections since I only have the velocity, as I said I'm really confused.

#EDIT:

in addition to the accepted answer, here's what I did to get a full 360 degree spectrum

var addDeg = 0;
if(obj.velocity.x<0)
    addDeg = obj.velocity.y>=0 ? 180 : 270;
else if(obj.velocity.y<=0) addDeg = 360;

deg = Math.abs(Math.abs(Math.atan(obj.velocity.y/obj.velocity.x)*180/Math.PI)-addDeg)
Share Improve this question edited Jan 2, 2023 at 13:27 SwissCodeMen 4,89510 gold badges28 silver badges36 bronze badges asked Mar 15, 2014 at 7:42 php_nub_qqphp_nub_qq 16.1k22 gold badges81 silver badges148 bronze badges 3
  • Whats your reference axis? – hawaii Commented Mar 15, 2014 at 7:48
  • I don't really know what that is. How I see it is just the object has 360 degrees of potential directions to move at, I want to get the one it's actually moving at. Like if the velocity is x:1, y:1 and the coordinate system starts from the top left corner and y increments downwards in a positive manner, then the direction would be 45 degrees – php_nub_qq Commented Mar 15, 2014 at 7:52
  • Like if you start drawing a circle from the center of the object counter-clockwise, that's how the degrees go – php_nub_qq Commented Mar 15, 2014 at 7:54
Add a ment  | 

3 Answers 3

Reset to default 5

I don't know how to get those projections since I only have the velocity

Actually, what you seem to be missing is that you already have the projections. That's what x and y are.

x is speed * cos(angle)

y is speed * sin(angle)

So y/x = sin(angle)/cos(angle) which is tan(angle) so angle=arctan(y/x).

That's the angle rotating anti-clockwise starting from the x axis (with x pointing right and y pointing up).

Find the angle between that vector and (1,0) (Right horizontal positive direction).

The math is:

A = (5,1) B = (1,0)

A.B = |A||B|cos(angle) -> angle = arccos((|A||B|)/(A.B))

Dot product, check geometric definition

Edit:

Another option is to use the cross product formula:

|AxB| = |A||B|sin(angle) -> angle = arcsin((|A||B|)/(|AxB|))

It will give you the angle you need.

There is an easier way to get full 360 degrees. What you're looking for, is Math.atan2:

deg = Math.atan2(obj.velocity.y,obj.velocity.x)*180/Math.PI;
发布评论

评论列表(0)

  1. 暂无评论