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.
Related
Background
We are supplied with some AIXM data (an XML based superset of GML) which describes polygon areas on a map as a mix of GeodesicStrings (a list of coordinates) and ArcByCentrePoints (a centre point coordinate with a radius, start bearing and end bearing). We are taking this data and converting it into a simple list of coordinates that we then display using a Google maps polyline.
Problem
When we plot a shape with an arc, the start and end points of the arc usually don't match up with the end point of the preceding line and the start point of the subsequent line. It looks as if the radial distance is out by an amount which doesn't appear to be proportional to the radius. See screenshot: interestingly the smaller arc at the top seems fine but the larger arc is inset.
We're pretty sure the data is correct because it looks fine when we use a third party tool to visualise it, so we're doing something wrong.
Implementation
We are using the turf.js library to convert the arc description into a set of points using their lineArc function. Internally this utilises their destination function which "uses the Haversine formula to account for global curvature". We combine these generated points, in the correct sequence, with the points taken directly from the preceding and subsequent GeodesicString elements to give us our final polygon.
Data
Input: Fragment of AIXM (GML) describing polygon
Output: Resulting list of points
Help!
I'm aware this question is light on code but I hope I've described the problem adequately and that some kind person with more GIS knowledge than me (>0) might be able to point me in the right direction. Thanks :)
I've given a couple of presentations on debugging and one of the things I say is that you should keep an open mind and shouldn't get too fixated on a possible cause of a bug because you can waste a lot of time tracking down a false lead.
Sadly in this case I didn't take my own advice. I was so obsessed with the idea that the problem arose from a complex cause, such as issues with the implementation of the Haversine formula, that I overlooked the far simpler answer. My code was taking a string representation of the radius, including the units (e.g. nautical miles or meters) and converting it into kilometres. Sadly I was using parseInt rather than parseFloat as part of this and so instantly losing precision. It was a simple as that - a schoolboy error.
Big thanks to Stefano Borghi, a maintainer of Turf JS, for all his help with this and for helping me see the wood for the trees.
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 find a way to smooth out the points for sketching to get a cleaner result. I've found this example, and the result looks quite good
I've found some ways that use cubic curves to smooth out the edginess, but all of them requires the line to be redrawn from the beginning since they work with multiple points.
My initial idea was to "draw" the final "part" of the line when there are enough points to calculate the bezier (simply 2 frames after points are taken) to overcome the need of "redrawing" every time.
Any ideas/workarounds about this will be appreciated.
Thanks!
Ok, i'm not sure how to explain it correctly, hopefully it makes sens. What i'm trying to achieve is link 2 points but with an organic curve. I can link two points in AS3 without any problem but i'm looking for a cooler graphical way (a bit like nurbs curves for the 3d enthusiasts out there) instead of a straight line.
I have found an answer in here about cubic hermite spline which linked to http://wonderfl.net/c/pTgv , the things is, i don't know the control points, all i know is the coordinates of my two points and as my points are draggable, they can change at anytime.
So the question is, is there any algorithm out there to have an organic curve between two points ??
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?