Hi, I'm trying to get the rotation of the points of a bezier curve and could do with some help please ..
The snippets of code below is what I'm using to draw the bezier curves and that fine, I fly my GO around it fine but would like to bank in and out of the curves.
//for first curve c is set to 0
c = 0.0;
arrayPosition = 0;
q0 = CalculateBezierPoint(c, start.position, handle1.position, handle2.position, end.position);
// we defined steps to draw the curve, 100
while(c <= 1)
{
// X amount of steps to draw each bezier curve. 0.01 will draw 100, (1 / 0.01 = 100)
c += 0.01;
oldC += 0.01;
q1 = CalculateBezierPoint(c, start.position, handle1.position, handle2.position, end.position);
Debug.DrawLine (q0, q1, Color.green, 200);
Debug.DrawLine (q0, (q0+Vector3.up*2), Color.red, 200);
var lookAt: Quaternion = Quaternion.LookRotation(start.position);
var normal: Vector3 = lookAt*Vector3.up;
Debug.DrawLine(q0, (q0+normal*3), Color.blue, 200);
// everytime c increases by X Instantiate a maker
if(oldC >= 0.01)
{
oldC = 0;
lookAtFlightPosition = (q1 - q0).normalized;
_rotation = Quaternion.LookRotation(lookAtFlightPosition);
markerClone = Instantiate(markerGreenForward, q1, _rotation);
markerClone.transform.localScale = Vector3(0.1,0.1,0.1);
}
q0 = q1;
yield;
}
And the bezier curve code ..
function CalculateBezierPoint ( t:float, p0:Vector3, p1:Vector3, p2:Vector3, p3:Vector3 ):Vector3
{
u = 1 - t;
uu = u * u;
uuu = uu * u;
tt = t * t;
ttt = tt * t;
p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * tt * p2;
p += ttt * p3;
return p;
}
I thought this snippet of code from the above would give me the visual rotation of the points ..
var lookAt: Quaternion = Quaternion.LookRotation(start.position);
var normal: Vector3 = lookAt*Vector3.up;
Debug.DrawLine(q0, (q0+normal*3), Color.blue, 200);
I'm generating a bezier curve a random amount of times 6/10 to make a flight path, then every time it regenerates the stat moves to the end and the end is randomly chosen in 3D space, then I check if the new end point is to the left or right of the start position then I place the handles with a random value of 10/20 to whatever side the end point is on.
When all the bezier curves are done and the flightpath is complete the jetpack starts to fly around it, as I'm using a rag doll (puppet Master) I use MovePosition and MoveRotation in FixedUpdate() else I get judder, flying around the flightpath is no problem and the jetpack stays orientated to the direction of the path with ..
lookAtFlightPosition = (waypointScript.waypointsPositions[i] - jetpack.position).normalized;
rotation = Quaternion.LookRotation(lookAtFlightPosition);
jetpackRigidbody.MoveRotation(rotation);
And that works fine, but I don't understand how to read the rotation of each of the 100 point making up each bezier curve, to some I'm sure this is simple, but to me I'm sorry to say it's not, programming is not my strongest point.
I now use the below script to get the tangent.
var q2 : Vector3 = CalculateTangent(t, start.position, handle1.position, handle2.position, end.position);
var lookAt: Quaternion = Quaternion.LookRotation(q2);
var normal: Vector3 = lookAt*Vector3.up;
Debug.DrawLine(q0, (q0+normal*1), Color.white, 200);
function CalculateTangent (t:float, p0:Vector3, h0:Vector3, h1:Vector3, p1:Vector3):Vector3
{
a = 1-t;
b = a*6*t;
a = a*a*3;
c = t*t*3;
return
- a * p0
+ a * h0
- b * h0
- c * h1
+ b * h1
+ c * p1;
}
And now you can see in the pictures the debug line is correct off the normals of the curves points.
So once again I stuck, so would appreciate any help.
Thanks.
![alt text][1]
![alt text][2]
[1]: /storage/temp/79631-screen-shot-00.jpg
[2]: /storage/temp/79632-screen-shot-02.jpg
↧