I have a Bezier curve specified by 4 points. I need to know if a point is on the left side or right side of the Bezier curve. Can you suggest me an algorithm?
Edit: I'm sure that the way I generate the Bezier curve would not form loops.
Later edit I realized that my initial problem could be solved without using relative position. When I posted this question I was thinking that there is a mathematical formula for relative position similarly with checking if a point is in the interior of a circle. It seems that this is not possible. So I will accept the answer which will suggest a time efficient solution.
You can determine the closest point on the bezier curve with a pretty straightforward algorithm (related to k-subdivision. DeCastleju's Algorithm.) Look at the graphics gems if you need specifics.
At that point, even with loops, you can determine side-ness by determining if the vector to your tested point from the closest point is on the left of right hand of the vector that goes along the curve (velocity? - not sure of the correct term here...) of the bezier at the closest point you determined.
You can get -that- by cross product of the two vectors. Negative or positive will determine the handedness and which side of the line you are on.
Of course, in a loop the sideness will be defined as if you were a car driving down the line, would you be looking out the right or left window at the point as you go by... Not if you are to the right or left of the whole bezier squiggle. So it depends on how you define "sideness"
Sorry if my terms are off. Its been awhile since I had to do anything with Bezier's
It would be easier to draw a picture ;)
If you just want your object to follow the curve (as you say in your comment), why don't you just move your object with the parametric equation ? See this article
Here is math for cubic and quadratic Bezier curve implicitization.
I cannot remember the math at this late hour, but you'd probably want to use a subdivision algorithm for the curve to progressively refine it until the segments are 'straight' enough that you can treat them as line segments for the purposes of your determination.
You may be able to get a quicker answer by using the bounding polyhedra of the curve refinements to determine at which point your 'point' is outside all of the polyhedra, and then immediately flatten to line segments.
Assuming the point constrained to the curve, you must define one of the anchors as the start and the other as the end, then calculate a point that belongs to the curve and is in the middle (length's half)... that way you can say if the point is between the start and the middle or the middle and the end.
Is that what you want or am I totally lost?
Related
The short question: Is there any simple way in Nape to calculate the points of tangency with a Nape body object or shape given a point outside that body?
What I'm trying to do is create Worms-style rope physics. It basically works as an extendable line/distance joint that automatically breaks into segments when it comes in contact with the level geometry. I do this by raycasting from the most recent pivot point; if there is a collision I offset from the collision point by a couple of pixels, create a new rope segment, and make that point the new pivot. In case my character is swinging around a sharp corner, I then recast from that point, looping as necessary, until I'm clear of the level geometry.
It works amazingly well given my lack of experience, but there's one little cosmetic glitch. The rope won't wrap "tightly" around a horn-shaped protrusion. It's pretty easy to see why this is happening. Refer to the figure below.
I cast a ray each time I step the Nape world at 60 frames/second. Figure 1 shows the difference between two example raycasts. The character (not pictured) is at the end of the line, and he's fallen past the cliff "edge" in relation to the pivot in one step, so the collision point falls short of the desired point of tangency.
Figure 2 is what I end up with. The wraparound logic still works, by offsetting from the surface and recasting, but it doesn't appear "taut."
What I want is something like Figure 3, which corrects the angle to find the actual point of tangency with the body and creates the new pivot from that.
My planned fallback is to offset the angle of the raycast by small increments and recast until I no longer strike the level geometry, then back up one and use that as the collision point. Even that will probably require fewer computations than "curving" around like in Figure 2, but I'm still wondering: is there an even simpler way?
Excuse me for not commenting, but I don't have needed points for that :)
I've used something similar before (not exactly the same) and I think the way to go is to save the points of each cast, get the one with highest difference from the starting point, based on the y axis (if the rope goes up, then you get the point with smallest y and vice versa (rope going down from starting point)).
Then you can fix the angle to point to this specific point, marked as an "edge". Later you can continue with the common pattern, as the rope will go in the other direction (exactly like the edge of a cliff).
I'm trying to draw multiple shapes in the same Sprite.graphics scope, and can't seem to find any reasonable solution to my problem.
Please keep in minde that I've been using the drawPath() method for a performance problem : I could use multiple shapes with blendmodes, but I'd like to avoid that and keep performances cost to the minimum.
I've been experimenting with the winding parameter of the drawPath() method, but one thing that I cannot understand, is how the winding direction is defined by Flash, so here is a first question before actually coming to the real problem :
Are points coordinates taken in account ? Or is it the angle between the produces lines that define the direction ?
That being said, here is my actual problem :
I want to draw a shape that is a projection from a rectangle on a line - think of a window and the light that passes through it that goes hit the floor.
To achieve that, I must take into account that the lightsource position can vary and have that kind of results :
Here on that second picture you can already see the problem I'm facing.
To draw my shape, I've been separately "drawing" (understand : placing the numbers in my coordinates vector) the different parts of my figure : the actual rectangle I want to project, the light projected from its left side, the light projected from its bottom side, and the light projected from its right side.
I've been trying to carefully keep the winding direction the same in every section, beginning from the top-left corner, but it seems something is wrong in my reasoning, since every time the center part overlaps with any side part, the shape is emptied there, and every time the two sides parts overlaps, the same happens.
As I'm writing that here, some revelation suddenly strike me, and now I guess that maybe ALL my points in my coordinates vector must be sorted in the same winding direction for my shape to work ?... (and not only the small parts I'm separately drawing in my mind ^^)
If I'm right (please correct me if I'm not, or if I've understood anything wrong ?...), that means I must either :
sort my points to be placed in the correct winding direction (thing that might be complicated and could result in some strange drawed shape once provided to the drawing API ?...)
only draw the shape from the most external points, depending on the shape's actual shape (thing that might be more complicated that I currently expect).
Could anyone here confirm or infirm my last suppositions, and give me a clue on what could be going on here and how to solve it ?...
Thanks a lot :-)
You want to have one shape composed out of three projector lines? Okay, you have determined the positions of two lower points, and you have 4 points of your window. You then construct a list like so:
Two topmost points are always in the list, as you write that your light will always fall down out of the window. So, put 0 in command, window's upper left point coordinates into path, 1 into command (lineTo), window's upper right point into path.
Now, if both of your floor points have X less than lower right angle of the window, you add the window's point into the path!
Then you add rightmost floor point, then leftmost.
Then, if both of your floor points have X greater than lower LEFT corner of the window, you add it to the list.
You're done. And you will no longer need 3 projections, you calculate only the bottommost one (it'll give you both points on the floor), and make your list. Should do. Please comment.
Can somebody walk me through how this madness works:
http://www.youtube.com/watch?v=KL8QLLmUvbg
Specifically I'm interested in equally distributing a given number of squares along a path. I'm also wondering if this would work with multiple line segments-- this is one curved segment and I need a solution to distribute objects across one big line with multiple curves in it.
Basically I'm trying to make a tail that realistically follows a character.
Thanks
First a Bezier spline is a curve parametrized by t. However t is not arc-length along the curve. So the procedure is this.
Calculate the length of the bezier curve.
Find the t values that divide the curve into N equal length segments.
However these two steps are tricky.
The first has a closed form solution only for quadratic Beziers. (You can find the solution here )
Otherwise you use a subdivide and approximate approach, or a numerical integration approach (and in some sense these are equivalent - I'd go the numerical integration approach as this has better provable behavior at the cost of slightly trickier implementation, but you may or may not care about that.)
The second is basically a guess a t value, and improve approach (using the same style of calculation at each step as step 1). I'd implement this using a secant style search, as I suspect the derivatives required to use a Newton's method search would be too expensive to calculate.
Once you've got the positions of the objects, you need to use the curve tangent and cotangent to create a local reference frame for the object. This allows the objects to sit nicely in the path of the curve, rather than all having the same orientation. Note that this only works nicely in 2D - in 3D you can still get some weird behavior with object orientation.
You can start by looking into how a bezier curve is calculated. Wikipedia has some nice animations with the explanation and this link has some as3 code.
but if you're trying to create a tail, there are simpler ways of doing that, like using following behaviour or a physics library
I ended up creating a following behavior system like Daniel recommended for simplicities sake. But to elaborate on Michael's awesome answer I stumbled onto this tutorial which details the the spline technique.
http://gamedev.tutsplus.com/tutorials/implementation/create-a-glowing-flowing-lava-river-using-bezier-curves-and-shaders/
I currently develop an application that creates polygons from lines and I experience a small problem:
I have a set of points, representing a line. I would like to create a polygon that displays the line with a specific width (e.g. for a street). I have several ideas how to calculate the outer polygon points, but I think they are too complicated...
My best idea was the one pictured below: Every point of the line must be projected to at least two points: Both points must be 90° to the following line segment and have a distance half of the preferred polygon width.
This works good, as you can see at the end and start points of the pictured polygon. Now the complicated part: With this method, at a corner, each point gets four points. But these points are not correct for the outer polygon, because they are in the shape. The lines intersected and created an ugly polygon.
How can I find the correct points for such a polygon? I think my method is far too complicated for solving this problem.
Can anybody help me with this (propably very common) problem?
Info: I tagged this with openstreetmap because renderer like Mapnik have this problem, too.
What you are looking for is a polygon (or line) offsetting algorithm. This is not necessarily an easy problem to solve, by the way: An algorithm for inflating/deflating (offsetting, buffering) polygons.
For the last couple of weeks I've been working on a line offsetting algorithm for Maperitive. In my case I only needed to offset the line so I wasn't looking for a solution to create a buffered polygon around it, but I guess the algorithm could be extended further in the future:
Basic flow (roughly, but the devil is in the details):
For each polyline point find a point that has an L distance from the original point and lies on a line that's orthogonal to the original line and goes through the original point.
Now draw an offset line through that new point. The line must be parallel to the original line.
For corner angles you must extend the two neighbouring offset lines and find the intersection point, which will be the next point of the offset line.
Some things to observe:
Notice the miter limit applied on concave angles to the right of the picture.
Before calculating the offset line you need to simplify the original polyline to exclude segments that are too small to hold the offset (the results can be seen at the center left of the picture).
I only implemented support for miter joins, but a good algorithm should be able to render round joins, too (using arcs).
Hi stackoverflow's users !
First i want to say its my first post and i've been diging straight up gold from this site and i love it and everyone out there smart enough to give out solutions.
So basically am writing a game and i have bezier curve that controls where an actor moves. Am using only 3 points (start, control1, end) and everything is fine.
Now i want to create a bezier curve that passes through a certain point (r1) at t = 0.5 . The problem being that i have my start point, my end point and my point (r1) at t=0.5, and i need to find the control point.
Sorry if i explained it badly i hope everyone can understand.
Thank you for any replies : )
Look into the documentation of Asymptote (here) which has a geometrical interpretation of the a spline. For the mid point (t=-.5) the point lies in the point m_5 below. If you are using one control point them c_0 and c_1 are coincident simplifying the math. If at (t=0.5) your y values is y_c and the beginning and ending values are respectively z_0 and z_1 then the control point is at
c = (8*y_c-z_0-z_1)/6
Do the same for the x values.
Check out the question I asked a couple of days ago: Given f(x) linear function, how to obtain a Quadratic Bezier control point. I think it's exactly what you need.