Getting the derivative of a Bezier curve problem in Cocos2d - iphone

In cocos2d you can move a sprite in a Bezier path using ccBezierConfig. That's not my problem though, I have a missile and am trying to make it rotate perpendicular to that point on the curve. I couldn't figure it out for a while and then my friend told me about derivatives. Now I need to find the derivative of a bezier curve apparently. I searched on Google and found it on this page: http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html. So then I tried implementing rotating the missile with 3 methods here they are:
-(float)f:(int)x {
if (x == 0)
return 1.0f;
float result = 1;
while (x>0) {
result = result*x;
x--;
}
return result;
}
-(float)B:(float)u i:(int)i n:(int)n {
return ([self f:n])/([self f:i]*[self f:(n-i)])*pow(u, (float)i)*pow(1-u, (float)n-i);
}
-(void)rotateMissile:(float)delta {
//Get bezier derivative...
float y = [self B:missileP1.controlPoint_1.x i:0 n:2]+[self B:missileP1.controlPoint_2.x i:1 n:2]
*2*(missileP1.controlPoint_1.x - missileP1.controlPoint_2.x);
//Take the y and rotate it...
missile1.rotation = atanf(y);
}
The first method is for factorials, the second is supposed to find B in the equation derivative. The 3rd method is supposed to find the actual derivative and rotate the missile by converting slope to degrees using atanf.
The rotateMissile is being called continuously like such:
[self schedule:#selector(rotateMissile:)];
missileP1 is the ccBezierConfig object. missile1 is the missile I am trying to rotate. I'm just really confused about this whole derivative thing (in other words, I'm really lost and confused). I need help trying to figure out whats wrong... Sorry that the code is messy, the equations were long and I could figure out a way to make it less messy.

Actually I don't understand how are you taking a derivative and putting it into a float. That's because Bizier curve is two dimensional parametric curve (it has x and y components). It is not a function y(x). In cubic case it is:
x(t) = x0 + x1*t + x2*t*t + x3*t*t*t
y(t) = y0 + y1*t + y2*t*t + y3*t*t*t
Let's call it form1. So actually it's nothing more then two polynomials of third order. The traditional form of cubic Bezier curve is
Note, that B(t) here is a two dimensional vector (x(t), y(t)). So if you have a tradional way defined Bezier curve you can convert it to form1 by evaluating coefficients x0, x1 and son on.
If you now have your Bezier curve defined in form1 it is very easy to take the derivative:
x'(t) = x1 + 2*x2*t + 3*x3*t*t
y'(t) = y1 + 2*y2*t + 3*y3*t*t
Now the vector (x'(t), y'(t)) - is the velocity on your bezier curve. It also a tangent vector to your curve. The perpendicular vector will be (-y'(t), x'(t)) or ((y'(t), -(x'(t)).
Here are the coefficients:
For y coefficients the formula is totally identical. It just will py0, py1, py2, py3 instead of px0, ... .

Related

Cheapest way to find Vector magnitude from a given point and angle

I am trying to determine a players depth position on a plane, which defines the walkable ground in a 2D brawler game. The problem is depictured in the following drawing:
C represents the players current position. I need to find the magnitude of vector V. Since I am not strong on linear algebra, the one thing I can think of is: determining the intersection point P of L1 and L2, and then take the magnitude from AP. However, I get the feeling there must be an easier way to find V, since I already know the angle the vector should have, given by vector from AB.
Any input would be appreciated, since I am looking forward to step up my linear algebra game.
Edit: As it is unclear thanks to my lack of drawing skills: the geometry depicted above is a parallelogram. The vector V I am looking for is parallel to the left and right side of the parallelogram. Depth does not mean, that I am looking for the vector perpendicular to the top side, but it refers to the fake depth of a purely 2D game. The parallelogram is therefore used as a means for creating the feeling of walking along a z axis.
The depth of your player (length of V) as measured from the top line in your drawing, is just the difference between A.y and C.y. This is seperate from the slant in the parralelogram, as we're just looking at depth.
example:
float v;
Vector2 a = new Vector2(100, 100); //The point you're measuring from
Vector2 c = new Vector2(150, 150); //Your character position
v = c.y - a.y; // This is the length of V.
//In numbers: 50 = 150 - 100
Illustrated: image not to scale
This works for any coördinate in your plane.
Now if you'd want to get the length of AC is when you'd need to apply some pythagoras, which is a² + b² = c². In the example that would mean in code:
Vector2 a = new Vector2(100, 100);
Vector2 c = new Vector2(150, 150);
float ac1 = Mathf.Sqrt(Mathf.Pow(c.x - a.x, 2) + Mathf.Pow(c.y - a.y, 2));
Now that is quite a sore to have to type out every time, and looks quite scary. But Unity has you covered! There is a Vector method called Distance
float ac2 = Vector2.Distance(a, c);
Which both return 70.71068 which is the length of AC.
This works because for any point c in your area you can draw a right angled triangle from a to c.
Edit as per comment:
If you want your "depth" vector to be parallel with the sides of the paralellogram we can just create a triangle in the parallelogram of which we calculate the hypotenuse.
Since we want the new hypotenuse of our triangle to be parallel to the parallelogram we can use the same angle θ as point B has in your drawing (indicated by pink in mine), of which I understood you know the value.
We also know the length of the adjacent (indicated in blue) side of this new triangle, as that is the height we calculated earlier (c.y - a.y).
Using these two values we can use cosine to find the length of hypotenuse (indicated in red) of the triangle, which is equal to the vector V, in parallel with the parallelogram.
the formula for that is: hypotenuse = adjacent/cos(θ)
Now if we were to put some numbers in this, and for my example I took 55 for the angle θ. It would look like this
float v = 50/(cos(55));
image not to scale
Let's call the lower right vertex of the parallelogram D.
If the long sides of the parallelogram are horizontal, you can find magnitude of V vector by:
V.magnitude = (c.y - a.y) / sin(BAD)
Or if you prefer:
V.magnitude = AB.magnitude * (c.y - a.y)/(b.y - a.y)

Microsoft Q# : problems with rotate function R()

Recently I've been working on algorithms of quantum computing where I faced some problems.
As there are probabilities for qubits to be observed as |1> or |0>, the rotation function is supposed to change the status of each qubit so that there can be more or fewer chances for them to be measured in a specific status.
In my program, I applied Hadamard transformation to a qubit and then rotated it with an angle θ(90°) in the z-axis, which was to make an equal probability of |0> and |1> measurement and then the rotation made the probs of status |1> 100%, but it didn't work anyway. The probs for |1> will always be 50% which is not supposed to be.
Here is my code:
operation Testing () : (Int)
{
body
{
mutable result = -1;
using (qubit = Qubit[1])
{
H(qubit[0]);
R(PauliZ, PI() / -2.0, qubit[0]);
//assertprob is a function to help test the probabilities of qubits in specific status
AssertProb([PauliZ], qubit, One, 0.5,"Measuring in conjugate basis did not give 50/50 results.", 1e-5);
//AssertProb([PauliZ], qubit, Zero, 0.5,"Measuring in conjugate basis did not give 50/50 results.", 1e-5);
let value = M(qubit[0]);
if(value == One)
{
set result = 1;
} else {
set result = 0;
}
ResetAll(qubit);
}
return result;
}
}
A photo from Microsoft Q# documentation may help explain the sphere structure. After the Hadamard transformation, the qubit is in status [1/√2,1/√2] and then the rotation of -π/2 makes it to status [0,1].
(source: microsoft.com)
Applying a rotation around the Pauli Z axis doesn't change the measurement probabilities of Z-axis measurements. You can visualize it this way:
Your qubit's initial state is at +1 on the Z axis.
The H operation moves it onto +1 on the X axis.
Any rotation around the Z axis keeps the qubit's state in the X-Y plane, which is equally likely to measure as 0 or as 1 in the Z axis.
A pi/2 or 3pi/2 rotation around the Y axis will get the state back to the Z axis.

Calculating a spiral in MATLAB

We have these logarithmic spirals which are circling around the centre of the coordinate system:
x = ebθ cos(θ)
y = ebθ sin(θ)
where the ebθ is the distance between the point (which is on the spiral) and the centre; and the θ is the angle between the line connecting the point and the origin and the axis x.
Consider a spiral where the angle is θ ϵ <0,10π> and the parameter is b=0.1. By thickening points on the spirals (and the angle θ) calculate the circumference with the relative precision better than 1%. Draw the spiral!
I'm preparing for a (MATLAB) test and I'm stuck with this exercise. Please help, any hint is appreciated.
Start by computing a list of x,y for your range of theta and value of b. For more accurate results, have your theta increment in smaller steps (I chose 5000 arbitrarily). Then, its simply computing the distance for each pair of consecutive points and summing them up.
t = linspace(0,10*pi,5000);
b = 0.1;
x = exp(b*t).*cos(t);
y = exp(b*t).*sin(t);
result = sum(sqrt((x(2:end) - x(1:end-1)).^2 + (y(2:end)-y(1:end-1)).^2))

finding the intersection of a line with a non monotonic arbitrary surface?

I have a surface Z on a X-Y grid for which I want to find the intersection point with a line. I used so far this code for finding the intersection:
x_ray = x_source + t * x_dir
y_ray = y_source + t * y_dir
z_ray = z_source + t * z_dir
height_above_plane = #(t) z_source + t * z_dir - interp2(X, Y, Z, ...
x_source + t*x_dir, y_source + t*y_dir)
t_intercept = fzero(height_above_plane, 0);
my problem is that when my surface is "wiggly", the function has several zero crossing points, and I want to find the minimal out of them.
How can I do that?
Thanks
A possible approach is to project the ray onto the XY domain and draw the corresponding Bresenham line. As you go along this line, grid cell per grid cell, you will compute the Z altitudes along the ray and check if their range overlaps the range of altitudes of the surface (i.e. the min and max value in this cell).
If yes, you have to find the 3D intersection between the ray and the interpolating surface, an hyperbolic paraboloid. If the intersection does fall inside the grid cell considered, you are done. Otherwise, continue the march along the ray.
Convert the surface to matlab mesh, then use this code.

How can I correctly calculate the direction for a moving object?

I'm solving the following problem: I have an object and I know its position now and its position 300ms ago. I assume the object is moving. I have a point to which I want the object to get.
What I need is to get the angle from my current object to the destination point in such a format that I know whether to turn left or right.
The idea is to assume the current angle from the last known position and the current position.
I'm trying to solve this in MATLAB. I've tried using several variations with atan2 but either I get the wrong angle in some situations (like when my object is going in circles) or I get the wrong angle in all situations.
Examples of code that screws up:
a = new - old;
b = dest - new;
alpha = atan2(a(2) - b(2), a(1) - b(1);
where new is the current position (eg. x = 40; y = 60; new = [x y];), old is the 300ms old position and dest is the destination point.
Edit
Here's a picture to demonstrate the problem with a few examples:
In the above image there are a few points plotted and annotated. The black line indicates our estimated current facing of the object.
If the destination point is dest1 I would expect an angle of about 88°.
If the destination point is dest2 I would expect an angle of about 110°.
If the destination point is dest3 I would expect an angle of about -80°.
Firstly, you need to note the scale on the sample graph you show above. The x-axis ticks move in steps of 1, and the y-axis ticks move in steps of 20. The picture with the two axes appropriately scaled (like with the command axis equal) would be a lot narrower than you have, so the angles you expect to get are not right. The expected angles will be close to right angles, just a few degrees off from 90 degrees.
The equation Nathan derives is valid for column vector inputs a and b:
theta = acos(a'*b/(sqrt(a'*a) * sqrt(b'*b)));
If you want to change this equation to work with row vectors, you would have to switch the transpose operator in both the calculation of the dot product as well as the norms, like so:
theta = acos(a*b'/(sqrt(a*a') * sqrt(b*b')));
As an alternative, you could just use the functions DOT and NORM:
theta = acos(dot(a,b)/(norm(a)*norm(b)));
Finally, you have to account for the direction, i.e. whether the angle should be positive (turn clockwise) or negative (turn counter-clockwise). You can do this by computing the sign of the z component for the cross product of b and a. If it's positive, the angle should be positive. If it's negative, the angle should be negative. Using the function SIGN, our new equation becomes:
theta = sign(b(1)*a(2)-b(2)*a(1)) * acos(dot(a,b)/(norm(a)*norm(b)));
For your examples, the above equation gives an angle of 88.85, 92.15, and -88.57 for your three points dest1, dest2, and dest3.
NOTE: One special case you will need to be aware of is if your object is moving directly away from the destination point, i.e. if the angle between a and b is 180 degrees. In such a case you will have to pick an arbitrary turn direction (left or right) and a number of degrees to turn (180 would be ideal ;) ). Here's one way you could account for this condition using the function EPS:
theta = acos(dot(a,b)/(norm(a)*norm(b))); %# Compute theta
if abs(theta-pi) < eps %# Check if theta is within some tolerance of pi
%# Pick your own turn direction and amount here
else
theta = sign(b(1)*a(2)-b(2)*a(1))*theta; %# Find turn direction
end
You can try using the dot-product of the vectors.
Define the vectors 'a' and 'b' as:
a = new - old;
b = dest - new;
and use the fact that the dot product is:
a dot b = norm2(a) * norm2(b) * cos(theta)
where theta is the angle between two vectors, and you get:
cos(theta) = (a dot b)/ (norm2(a) * norm2(b))
The best way to calculate a dot b, assuming they are column vectors, is like this:
a_dot_b = a'*b;
and:
norm2(a) = sqrt(a'*a);
so you get:
cos(theta) = a'*b/(sqrt((a'*a)) * sqrt((b'*b)))
Depending on the sign of the cosine you either go left or right
Essentially you have a line defined by the points old and new and wish to determine if dest is on right or the left of that line? In which case have a look at this previous question.