how to calculate velocity of leg using kinect - c#

I have group of skeleton joints, for example 4 joints of leg. Each joint have (x, y, z) coordinates. So I want to find the combine speed or velocity of these group of joints because i believe I need to calculate velocity of each four joints of leg in order to find out the velocity of leg, please correct me If I'm wrong.
So what would be best possible way to calculate?
I'm using this formula
velocity= Sqrt((x[n]−x[n − 1] )2 + (y[n]−y[n − 1] )2 + (z[n]−z[n − 1] )2)

There is a simpler equation: Average Velocity = (Delta Distance)/(Delta Time)
If you want to know the average velocity at some point in the leg, calculate the location before it moved & the location on impact. Divide the magnitude of the vector of the difference between the 2 points by the amount of time it took to move between those points.

Related

Calculating the speed a sprite changes size based on the distance it needs to travel and speed it is travelling

I am doing some sprite scaling and have a sprite starting at scale 0.2 on the x+y axis it travels at a constant speed (10) between its start point and end point increasing in scale as it travels.
I need it to scale evenly across that distance so that when it reaches its end point it is at its final scale (0.2 - 1).
Now because the distance changes based on where it spawns the time that it takes to scale in size must also change so that it can scale evenly across the distance, what I need is the calculation to work out the 'scale speed' based on the distance it is traveling and speed the item is moving.
I'm getting the distance between the two points with Vector3.Distance
distance = Vector2.Distance(transform.position, centerScreen);
This scales the object over time
if (transform.localScale.x < 1 && transform.localScale.y < 1)
{
transform.localScale = new Vector3(transform.localScale.x + scaleSpeed * Time.deltaTime, transform.localScale.y + scaleSpeed * Time.deltaTime, 1);
}
and the object moves at a speed of 10
My question is, How do I calculate the scale speed based on the distance and speed the object is traveling?
In Unity, there is a function called InverseLerp, which creates a point from 0 - 1 based on how far the value v is between the values a and b. It is the opposite of the Lerp function. (https://docs.unity3d.com/ScriptReference/Mathf.InverseLerp.html).
Here is example code for your situation: float sizeVal = Mathf.InverseLerp(0, yourSetDistance, distance) * 0.8f + 0.2f;
This changes the range of the inverse lerp to between 0.2 and 1. Use the sizeVal in the size vector. If this isn't what you wanted, please explain in more detail or restate your question.

Finding circle from a normal vector c# [duplicate]

This question already exists:
circle in three dimensional coordiantes [closed]
Closed 1 year ago.
I want to draw a circle in three dimensional coordiantes, i'm given a vector, the angle where vector's intersects with the circle is 90 degrees, the intersection point is the centre of the circle. The radius can be parametrized. EDIT: I am programming a server plugin for minecraft. At this point I have made a sword that can be thrown. I want to add some decor. I want that after the sword there was a trace in the form of a circle. But I don't understand how to draw a circle in 3D coordinates so that the angle of intersection of the sword throw vector with the center of the circle is 90 degrees. The radius can be arbitrary, and the vector can enter the center of the circle. I thought I could just rotate the throw vector on 3 axes and get a circle, but nothing worked. I need an equation with which I can draw a given circle.
You have center C, normal vector N, radius R. Seems you want to get points at the circumference.
At first get some base vector in the circle plane.
Possible way:
Reveal normal component with the largest magnitude and with the second magnitude. For example, abs(N.X) is the largest, abs(N.Z) has the second magnitude, and abs(N.Y) is the smallest. Make the smallest component zero, exchange two larger ones, and negate the largest. For this example base vector will be:
A = (N.Z, 0, -N.X)
It is perpendicular to normal, hence lies in the circle plane.
Then get the next basis vector using vector product (B will be perpendicular both to A and to N, it lies in the plane too)
B = N x A
Now normalize vectors A and B (make them unit length)
A = A / len(A)
B = B / len(B)
and you can get any point at the circumeference with parametric equation where t changes in the range 0..2*Pi
P(t) = C + R * A * Cos(t) + R * B * Sin(t)
or in components:
P.X = C.X + R * A.X * Cos(t) + R * B.X * Sin(t)
and so on

Finding Distance from ray to 3D bezier-curve

I'm wondering if anyone knows how to find the smallest distance from a 3D line segment or ray to a 3D cubic-bezier curve or if there is anything built into the Unity game engine.
Calculate affine transformation that transform ray base point into (0,0,0) and direction becomes OX
If ray is defined by base point (rx0, ry0, rz0) and direction vector (dx, dy, dz) with length len (1 for normalized), then matrix of this transform is product of:
shift by (-rx0, -ry0, -rz0) -
then rotation around OZ axis by atan2(ry, rx)
then rotation around OY axis by acos(dz / len)
Apply this transform to Bezier curve contol points
Calculate minimal distance from curve to OX axis using zer0derivative approach (derivative is zero for max and min of function):
Write expression for squared distance depending of t:
SquaredDist(t) = by'(t)^2 + bz'(t)^2
Calculate derivative of SquaredDist by dt, make equation
SquaredDist' = 0
and solve it against t.
Equation is 5-th order polynomial equation, so no analytical, only numerical solution (or subdivision approach).
Check roots in 0..1 interval, also check Bezier curve ends and ray start point distance separately.

Getting rotation axis from initial and final rotated quaternions

I have to find the axis and angle of rotation of a camera with an UP and Direction vector(They both are perpendicular to each other). I have the initial and final positions of the UP and direction vectors of the camera that is rotated. I want to find the axis and angle of the rotation for the camera. I am using C# for my project. I am new to this 3D rotation. So pardon my questions if you find them silly.
From the direction (forward) vector f and up vector u you can get the side vector s by performing a vector cross product (s = f x u). All three vectors are now orthogonal. You should also make them orthonormal by normalizing each one of them. Taken together, these vectors form an orthonormal basis.
You now have two such basis: the one from your initial camera orientation and the one from your final camera orientation. Both basis can be represented as a rotation matrix. A rotation matrix is simply a 3x3 matrix where the 3 rows are respectively:
The forward vector
The up vector
The side vector
For example, the matrix:
[[1 0 0]
[0 1 0]
[0 0 1]]
could be your initial camera orientation at start-up with its forward vector, up vector and side vector pointing towards the positive x axis, y axis and z axis, respectively.
You can now convert these two basis (M1 and M2) to two unit quaternions (Q1 and Q2) using this algorithm which takes care about potential problems like divides by zero.
At this point, you have two unit quaternions representing your initial and final camera orientation. You must now find the quaternion qT that transforms Q1 into Q2, that is:
q2 = qT * q1
q2 * q1^-1 = qT * (q1 * q1^-1) = qT
=> qT = q2 * q1^-1
Knowing that the inverse of a unit quaternion is equal to its conjugate:
q1^-1 = q1* iif ||q1|| = 1
qT = q2 * q1^-1 = q2 * q1*
There is a single step left: extracting the axis and angle from quaternion qT:
angle = 2 * acos(qw)
x = qx / sqrt(1-qw*qw)
y = qy / sqrt(1-qw*qw)
z = qz / sqrt(1-qw*qw)
The angle is, of course, given in radian. Beware of the divide by zero when calculating x, y and z. This situation would happen when there is no rotation or a very small one, so you should test if angle > epsilon where you would choose epsilon to be quite small an angle (say 1/10 of a degree) and not calculate the vector if that is the case.

When to Normalize a Vector?

I am learning XNA and in almost all of the educational kits found on http://creators.xna.com/en-US/. I always see a call to Normalize() on a vector. I understand that normalize basically converts the vector into unit length, so all it gives is direction.
Now my question is When to normalize and what exactly does it help me in. I am doing 2D programming so please explain in 2D concepts and not 3D.
EDIT: Here is code in the XNA kit, so why is the Normalize being called?
if (currentKeyboardState.IsKeyDown(Keys.Left)
|| currentGamePadState.DPad.Left == ButtonState.Pressed)
{
catMovement.X -= 1.0f;
}
if (currentKeyboardState.IsKeyDown(Keys.Right)
|| currentGamePadState.DPad.Right == ButtonState.Pressed)
{
catMovement.X += 1.0f;
}
if (currentKeyboardState.IsKeyDown(Keys.Up)
|| currentGamePadState.DPad.Up == ButtonState.Pressed)
{
catMovement.Y -= 1.0f;
}
if (currentKeyboardState.IsKeyDown(Keys.Down)
|| currentGamePadState.DPad.Down == ButtonState.Pressed)
{
catMovement.Y += 1.0f;
}
float smoothStop = 1;
if (catMovement != Vector2.Zero)
{
catMovement.Normalize();
}
catPosition += catMovement * 10 * smoothStop;
In your example, the keyboard presses give you movement in X or Y, or both. In the case of both X and Y, as when you press right and down at the same time, your movement is diagonal. But where movement just in X or Y alone gives you a vector of length 1, the diagonal vector is longer than one. That is, about 1.4 (the square root of 2).
Without normalizing the movement vector, then diagonal movement would be faster than just X or Y movement. With normalizing, the speed is the same in all 8 directions, which I guess is what the game calls for.
One common use case of vector normalization when you need to move something by a number of units in a direction. For example, if you have a game where an entity A moves towards an entity B at a speed of 5 units/second, you'll get the vector from A to B (which is B - A), you'll normalize it so you only keep the direction toward the entity B from A's viewpoint, and then you'll multiply it by 5 units/second. The resulting vector will be the velocity of A and you can then simply multiply it by the elapsed time to get the displacement by which you can move the object.
It depends on what you're using the vector for, but if you're only using the vectors to give a direction then a number of algorithms and formulas are just simpler if your vectors are of unit length.
For example, angles: the angle theta between two unit vectors u and v is given by the formula cos(theta) = u.v (where . is the dot product). For non-unit vectors, you have to compute and divide out the lengths: cos(theta) = (u.v) / (|u| |v|).
A slightly more complicated example: projection of one vector onto another. If v is a unit vector, then the orthogonal projection of u onto v is given by (u.v) v, while if v is a non-unit vector then the formula is (u.v / v.v) v.
In other words: normalize if you know that all you need is the direction and if you're not certain the vector is a unit vector already. It helps you because you're probably going to end up dividing your direction vectors by their length all the time, so you might as well just do it once when you create the vector and get it over with.
EDIT: I assume that the reason Normalize is being called in your example is so that the direction of velocity can be distinguished from the speed. In the final line of the code, 10 * smoothStop is the speed of the object, which is handy to know. And to recover velocity from speed, you need to multiply by a unit direction vector.
You normalize any vector by dividing each component by the magnitude of the vector, which is given by the square root of the sum of squares of components. Each component then has a magnitude that varies between [-1, 1], and the magnitude is equal to one. This is the definition of a unit vector.

Categories

Resources