Log In  
Log In  

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.

P#48360 2018-01-19 12:33 ( Edited 2018-01-22 06:30)

:: Felice

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"...

or

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.

P#48364 2018-01-19 12:45 ( Edited 2018-01-19 17:52)

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.

P#48369 2018-01-19 15:06 ( Edited 2018-01-19 20:06)
:: Felice

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:

https://en.wikipedia.org/wiki/Projectile_motion

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:

Cart [#48416#] | Code | 2018-01-20 | No License | Embed

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.

P#48382 2018-01-20 02:05 ( Edited 2018-01-20 22:22)

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.

Trajectory stuff.
https://www.lexaloffle.com/bbs/?tid=28276

P#48403 2018-01-20 12:11 ( Edited 2018-01-20 17:11)
:: Felice

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.)

P#48417 2018-01-20 17:22 ( Edited 2018-01-20 22:22)

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 :)

P#48427 2018-01-21 14:14 ( Edited 2018-01-21 19:14)
:: Felice

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.

P#48431 2018-01-21 15:41 ( Edited 2018-01-21 20:45)

is that the princess from gotta protectors?

P#48442 2018-01-22 01:30 ( Edited 2018-01-22 06:30)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-05-20 02:23 | 0.022s | 4194k | Q:36