Sign in to follow this  

Bezier Curve to Triangle Strip

This topic is 2488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I don't normally do this but since it's been about 3 hours since I posted my other thread and no one has even LOOKED at it (has 0 views), I decided to repost my question here.

[url="http://www.gamedev.net/topic/595833-adding-thickness-to-bezier-curve/"]http://www.gamedev.net/topic/595833-adding-thickness-to-bezier-curve/[/url]

Basically, I have a vector2 array of points that are currently being drawn as a line list. But I want to add thickness to them. The only way I see myself being able to do this is creating a curve "parallel" to the first curve in which each point is equidistant from the opposing point. Then, I'd convert the 2 curves into a triangle list by alternating through the points.

My problem is in calculating a parallel curve.

So does anyone know how to do this OR do you know how to add thickness to a line in XNA? Thanks in advance. ^^

Share this post


Link to post
Share on other sites
So I think I got a way to do it... since a curve is technically a bunch of straight lines, you could add thickness by adding points above and bellow a point along a perpendicular line. My function isn't working correctly. Anyone know why?

[code]public static Vector2 perpendicularPoint(Vector2 p1, Vector2 p2)
{
float m = (p1.Y - p2.Y) / (p1.X - p2.Y);
float b = p1.Y - ((1 / m) * p1.X);

Vector2 point = new Vector2(0, B);

if (point == p1)
{
point = new Vector2(-1, b + (-1 / m));
}
return point;
}
///And this function is used to calculate the exact points. You plug in a point, a point on the perpendicular line, and a distance to travel.

public static Vector2 distanceToPoint(Vector2 MoveFrom, Vector2 Reference, int toTravel)
{
//get distance
//sqrt( (X2 - X1)^2 + (y2 - y1)^2 ) = d
float d = (int)Math.Sqrt((Math.Pow((MoveFrom.X - Reference.X), 2) + Math.Pow((MoveFrom.Y - Reference.Y), 2)));

//Get X and Y differences
float difX = MoveFrom.X - Reference.X;
float difY = MoveFrom.Y - Reference.Y;

//Get Ratio
float ratio = ((float)toTravel / d);

//Apply ratio to differences
difX = difX * ratio;
difY = difY * ratio;

//Return new Vector
return new Vector2(MoveFrom.X + difX, MoveFrom.Y + difY);
}


[/code]

Share this post


Link to post
Share on other sites
Cross posting is not a good idea. I posted in your other thread.

And I said that "isn't working correctly" doesn't mean ANYTHING (a sticky should be made on this thing, it's pretty annoying to always ask for more info)

Anyways: post an image to make our task easier.

Share this post


Link to post
Share on other sites
[quote name='szecs' timestamp='1298100706' post='4776237']
Cross posting is not a good idea. I posted in your other thread.

And I said that "isn't working correctly" doesn't mean ANYTHING (a sticky should be made on this thing, it's pretty annoying to always ask for more info)

Anyways: post an image to make our task easier.
[/quote]

[img]http://i1100.photobucket.com/albums/g401/gamersjd2002/curve.png[/img]


As the curve approaches the middle of the screen, the thickness decreases.

Share this post


Link to post
Share on other sites
[quote name='szecs' timestamp='1298196852' post='4776620']
perpendicular vector:

xp = y
yp = -x
[/quote]

That doesn't work. It doesn't even incorporate the second point. How can you calculate a perpendicular point with just one point o.O

Share this post


Link to post
Share on other sites
[quote name='szecs' timestamp='1298215675' post='4776689']
I wrote vector, not point. I meant direction.
[/quote]

Even so, you can't calculate direction with one point.

Here, what is xp ,yp, x, and y?

Cause the way I see it is xp, yp are the x and y direction of the vector and x and y are 1 points coords...

Share this post


Link to post
Share on other sites
[quote name='slynk' timestamp='1298218986' post='4776705']
[quote name='szecs' timestamp='1298215675' post='4776689']
I wrote vector, not point. I meant direction.
[/quote]

Even so, you can't calculate direction with one point.

Here, what is xp ,yp, x, and y?

Cause the way I see it is xp, yp are the x and y direction of the vector and x and y are 1 points coords...
[/quote]
szecs is right about using the perpendicular vector. The vector that this vector is perpendicular to would be the unit-length direction (tangent) vector at the specified point on the spline, which can be computed using some simple vector math if the spline is represented as a series of line segments, or can be computed directly from the spline representation.

Share this post


Link to post
Share on other sites
[quote name='jyk' timestamp='1298234377' post='4776781']
[quote name='slynk' timestamp='1298218986' post='4776705']
[quote name='szecs' timestamp='1298215675' post='4776689']
I wrote vector, not point. I meant direction.
[/quote]

Even so, you can't calculate direction with one point.

Here, what is xp ,yp, x, and y?

Cause the way I see it is xp, yp are the x and y direction of the vector and x and y are 1 points coords...
[/quote]
szecs is right about using the perpendicular vector. The vector that this vector is perpendicular to would be the unit-length direction (tangent) vector at the specified point on the spline, which can be computed using some simple vector math if the spline is represented as a series of line segments, or can be computed directly from the spline representation.
[/quote]

Ohhhhhhh.... is X, Y supposed to be the direction of the line? And xp, yp is the perpendicular direction? That would be where my confusion arose. I could get how you can calculate a perpendicular direction without two points representing the line XD.

Also, does my distance function look right?

I was trying to make a function that give two points and a distance, would move from the starting point a certain distance. I guess I'll switch it to a point and a directional vector instead.

Share this post


Link to post
Share on other sites
Fixed it, the directional vector worked. Thanks so much ^^

Anyone who comes upon this, here's some working code to create a bezier curve and then convert it into a trianglestrip that is "thickness" wide. ^^

[code] public const int SUBDIVISIONS = 30;
public const int THICKNESS = 30;
public static Color UNLIT = Color.Black;

/*
* Given 2 points and 2 control points, this function will calculate a curve
*/

public static VertexPositionColor[] bezierCurve(Vector2 start, Vector2 end, Vector2 c1, Vector2 c2)
{
VertexPositionColor[] points = new VertexPositionColor[SUBDIVISIONS + 2];

float fraction;

for (int i = 0; i < SUBDIVISIONS + 2; i++)
{
fraction = i * (1f / (float)SUBDIVISIONS);
points[i] = new VertexPositionColor(new Vector3((float)((start.X * Math.Pow((1 - fraction), 3))
+(c1.X * 3 * fraction * Math.Pow(1-fraction, 2))
+(c2.X * 3 * Math.Pow(fraction,2) * (1-fraction))
+(end.X * Math.Pow(fraction,3))),

(float)((start.Y * Math.Pow((1 - fraction), 3))
+ (c1.Y * 3 * fraction * Math.Pow(1 - fraction, 2))
+ (c2.Y * 3 * Math.Pow(fraction, 2) * (1 - fraction))
+ (end.Y * Math.Pow(fraction, 3))), 0), UNLIT);
}

return points;
}

/*
* This function treats the curve as a serious of straight lines and calculates points on a line perpendicular to each point, resulting in two points THICKNESS appart.
* Requires THICKNESS to be set
*/
public static VertexPositionColor[] curveToStrip(VertexPositionColor[] curve)
{
VertexPositionColor[] strip = new VertexPositionColor[curve.Length * 2];
VertexPositionColor[] new1 = new VertexPositionColor[curve.Length];
VertexPositionColor[] new2 = new VertexPositionColor[curve.Length];

for (int i = 0; i < curve.Length; i++)
{
if (i < curve.Length-1)
{
Vector2 p1 = new Vector2(curve[i].Position.X, curve[i].Position.Y);
Vector2 p2 = new Vector2(curve[i + 1].Position.X, curve[i + 1].Position.Y);
Vector2 perpPoint = perpendicularPoint(p1, p2);

new1[i] = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, THICKNESS / 2), 0), UNLIT);
new2[i] = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, -1 * THICKNESS / 2), 0), UNLIT);
}
else
{
Vector2 p1 = new Vector2(curve[i].Position.X, curve[i].Position.Y);
Vector2 p2 = new Vector2(curve[i - 1].Position.X, curve[i - 1].Position.Y);
Vector2 perpPoint = perpendicularPoint(p1, p2);

new1[i] = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, -1 * THICKNESS / 2), 0), UNLIT);
new2[i] = new VertexPositionColor(new Vector3(distanceToPoint(p1, perpPoint, THICKNESS / 2), 0), UNLIT);
}
}


//strip[0] = new1[0];
//strip[1] = new2[0];

for (int i = 0; i < curve.Length; i++)
{
strip[i * 2] = new2[i];
strip[(i * 2) + 1] = new1[i];
}

return strip;
}

public static Vector2 perpendicularPoint(Vector2 p1, Vector2 p2)
{
return new Vector2(p2.Y - p1.Y, -1 * (p2.X - p1.X));
}

public static Vector2 distanceToPoint(Vector2 MoveFrom, Vector2 Direction, int toTravel)
{
//Make a point
Vector2 Reference = new Vector2(MoveFrom.X + Direction.X, MoveFrom.Y + Direction.Y);

//get distance
//sqrt( (X2 - X1)^2 + (y2 - y1)^2 ) = d
float d = (int)Math.Sqrt((Math.Pow((MoveFrom.X - Reference.X), 2) + Math.Pow((MoveFrom.Y - Reference.Y), 2)));

//Get X and Y differences
float difX = MoveFrom.X - Reference.X;
float difY = MoveFrom.Y - Reference.Y;

//Get Ratio
float ratio = -1 *((float)toTravel / d);

//Apply ratio to differences
difX = difX * ratio;
difY = difY * ratio;

//Return new Vector
return new Vector2(MoveFrom.X + difX, MoveFrom.Y + difY);
} }[/code]

Share this post


Link to post
Share on other sites

This topic is 2488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this