I'm looking for some help or snippet on how to create a path for an object that gives an arc between 2 coordinates.
I have code/examples for waves and orbiting in a circle, but I haven't needed to make an arc between 2 explicit points before. I'm not making an Angry Birds game but need that type of physics arc created that then allows a object to follow that path.
Ideally, something that lets you increase the gravity/friction/whatever that will slow the object down the further it gets away from the source point.
Although...I guess I could just trial-n-error it from the source point, messing with the variables until the thing lands where I need it. But having a 2-point method would probably be more helpful in the long run (for everyone).
Even if you have a cart that has something similar, I'm happy to code surf for things and learn.
Do you want to be able to specify the two points and a time frame, and simply say, each frame, "move to the point on the arc corresponding to this point in time"...
Do you want to be able to apply an initial force to an object, which is then handled each frame by an incremental physics engine of sorts, and have it land at a fairly specific location?
The former is much easier, but also very different from the latter.
I think the latter is what's in my head, but if it's easier to do the former but have it follow a path that looks like that latter, that'd be great, especially if it's easier to code and support.
In my case, I need to show an item getting launched from one x/y and going in an arc to another x/y, but it's going to be the same every time - there's no input from the user that impacts it - it's just an animation, essentially...more like a tween, I guess.
I don't doubt the physics model type thing would be more useful and reusable but if it's not entirely necessary, I'm all for taking the cheaper route.
Well, if you want to go the latter route, wikipedia has a surprisingly thorough set of lessons on how projectiles work, with and without drag:
Specifically, the Trajectory of a projectile with air resistance section.
Warning: much math involved.
If you just want a reasonably believable curve that approximates the true parabola you'd get, you could probably just use a quadratic bezier curve, with the endpoints at the source and destination, and the single midpoint chosen to look about right.
You might need to diddle the time value to get the apparent horizontal speed about right, but I did a quick test to see how it'd look, and it's really not bad:
As noted in the source, the trick seems to be to pick your middle control point so that the slopes of the two line segments between points on either side mirror each other. The vertical position obviously controls the height.
Edit: The code uses a recursive, n-dimensional, open-ended, generalized bezier solution, but I just updated it to include some functions that assume 2D points and linear, quadratic, or cubic curves. They are much smaller and simpler.
Woah sweet! That's plenty for what I need. I'll jam it into my game and see how well it works. Super thanks!
I had also found a thread from a while ago that is about trajectory and stuff. I managed to get it into my game but it was a bunch of fiddling to get it right. Plus I'm guessing it's extra tokens and such that I might not need.
Oof, I just looked back at this and realized I left some code in the general-purpose function that I was just playing around with. I replaced the app above with a clean version, in case you used the general-purpose stuff instead of the boiled-down 2D functions.
(I'd left some stuff in that allowed me to use a different fraction at different levels of the curve evaluation. I was just mucking around; it's not proper bezier stuff. Basically, the "frac2,3,4" code shouldn't have been there.)
It's working great! I a function in there that's doing the trick wonderfully. Just had a get my coordinates figured out and I was golden. Thanks again!
See here...just food items getting flung into buckets...same arc over and over. I'm running with it until it tells me otherwise :)
Boingy, boingy. :) Seems to work. Glad to hear it!
Cute characters btw. :)
As an aside, I played with my code a bit more, just because I like how bezier curves work. If you ever need something more complex, I found that a cubic curve is much more useful for simulating a trajectory, due to its extra tweakability. I was able to mimic the appearance of drag just by putting the third control point (of four) closer to the destination. No matter what we do, it won't really be the same, but it can come believably close.
[Please log in to post a comment]