Domanda

I need to find the average Edit: total 2D velocity given multiple 2D velocities (speed and direction). A few examples:

Example 1
Velocity 1 is 90° at a speed of 10 pixels or units per second.
Velocity 2 is 270° at a speed of 5 pixels or units per second.

The average velocity is 90° at 5 pixels or units per second.

Example 2
Velocity 1 is 0° at a speed of 10 pixels or units per second
Velocity 2 is 180° at a speed of 10 pixels or units per second
Velocity 3 is 90° at a speed of 8 pixels or units per second

The average velocity is 90° at 8 pixels or units per second

Example 3
Velocity 1 is 0° at 10 pixels or units per second Velocity 2 is 90° at 10 pixels or units per second

The average velocity is 45° at 14.142 pixels or units per second

I am using JavaScript but it's mostly a language-independent question and I can convert it to JavaScript if necessary.

È stato utile?

Soluzione

If you're going to be using a bunch of angles, I would just calculate each speed,

vx = v * cos(theta), vy = v * sin(theta)

then sum the x velocities and the y velocities separately as vector components and divide by the total number of velocities,

sum(vx) / total v, sum(vy) / total v

and then finally calculate the final speed and direction with your final vx and vy. The magnitude of the speed can be found by a simple application of pythagorean theorem, and then final angle should just be tan-1(y/x).


Per example #3

vx = 10 * cos(90) + 10 * cos(0) = 10, vy = 10 * sin(90) + 10 * sin(0) = 10

so, tan-1(10/10) = tan-1(1) = 45

then a final magnitude of sqrt(10^2 + 10^2) = 14.142

Altri suggerimenti

These are vectors, and you should use vector addition to add them. So right and up are positive, while left and down are negative. Add your left-to-right vectors (x axis).

Example 1 = -10+5 = -5

Example 2 = -8 = -8

Example 3 = 10 = 10. (90 degrees is generally 90 degrees to the right)

Add you ups and downs similarly and you get these velocities, your left-to-right on the left in the brackets, and your up-to-down on the right. (-5, 0)

(-8,0)

(10, 10)

These vectors contain all the information you need to plot the motion of an object, you do not need to calculate angles to plot the motion of the object. If for some reason you would rather use speeds (similar to velocity, but different) and angles, then you must first calculate the vectors as above and then use the Pythagorean theorem to find the speed and simple trigonometry to get the angle. Something like this:

    var speed = Math.sqrt(x * x + y * y);
    var tangeant = y / x;
    var angleRadians = Math.atan(tangeant);
    var angleDegrees = angleRadians * (180 / Math.PI);

I'll warn you that you should probably talk to someone who know trigonometry and test this well. There is potential for misleading bugs in work like this.

From your examples it sounds like you want addition of 2-dimensional vectors, not averages.

E.g. example 2 can be represented as

(0,10) + (0,-10) + (-8, 0) = (-8,0)

The speed is then equal to the length of the vector:

sqrt(x^2+y^2)

To get average: add each speed, and then divide by the number of speeds.

10mph + 20mph / 2 = 15
12mph + 14mph + 13mph + 16mph / 4 = 14 (13,75)

This is not so much average as it is just basic vector addition. You're finding multiple "pixel vectors" and adding them together. If you have a velocity vector of 2 pixels to the right, and 1 up, and you add it to a velocity vector of 3 pixels to the left and 2 down, you will get a velocity vector of 1 pixel left, and 1 down.

So the speed is defined by

pij = pixels going up or (-)down

pii = pixels going right or (-)left

speedi = pii1 + pii2 = 2-3 = -1 (1 pixel left)

speedj = pij1 + pij2 = 1-2 = -1 (1 pixel down)

From there, you need to decide which directions are positive, and which are negative. I recommend that left is negative, and down is negative (like a mathematical graph).

The angle of the vector, would be the arctan(speedj/speedi)

arctan(-1/-1) = 45 degrees

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top