Page 1 of 1

[Mostly Solved] Using love.physic's setLinearVelocity to move towards a target following a cubic in eased curve

Posted: Sun Jan 01, 2017 8:33 pm
by Khranos
Messy title aside, I'm looking for a way to send a love.physics body from one specific position to another following an easeInCubic curve (http://easings.net/). I am using the flux tweening library.

I'm trying to get the physics objects to sync up to an image that I'm sending from point A (2300) to B (3400) using flux to emulate said curve, as shown below:

Code: Select all

flux.to(positionHoldingTable[currentObject], 12, {x = 3400}):ease("cubicin")
This is moving an image using the cubic in curve, and is working very nicely. However, I've spent the past few hours trying to emulate the same behavior using love.physics objects. Of course, :setX() (which works perfectly visually) does not allow collisions with other objects which is vital in my situation.

I cannot figure out what I could be doing to emulate the same behavior using setLinearVelocity (which is the only behavior I can use to allow proper collisions in my situation). I've toyed around with finding the percentage that the above flux-modified image has moved towards its goal and applying that to the linear velocity (which I assume is the correct approach), but I'm unable to find the correct values.

If it'll help, my current (and rather un-optimized/not fully working) code is here: http://pastebin.com/qeTfSM4G . If something should be defined, just let me know and I'll get back to you as soon as possible. (one notice, "movement" is not defined in the included paste)

Edit: Here's a gif of the situation. The white partial-box should be synced to the truck in the same position. The truck is an image, the boxes are a physics object which have the fill debug thing on:
The physics movement is using flux.to(2300intable, 12, {x = 3400}):ease("cubicin") which obviously doesn't work.
The physics movement is using flux.to(2300intable, 12, {x = 3400}):ease("cubicin") which obviously doesn't work.
crateSync.gif (886.92 KiB) Viewed 2511 times
Thanks for reading, and I appreciate any help that can be offered!

Re: Using love.physic's setLinearVelocity to move towards a target following a cubic in eased curve

Posted: Sun Jan 01, 2017 10:01 pm
by airstruck
I think you should be able to use the derivative of the easing function to determine what the linear velocity should be at any point in time. If the easing function is x³ (where x is a "normalized time value" between 0 and 1), then the derivative is 3x² (you can derive functions using wolfram alpha or an algebra program). You should be able to plug in x to find the linear velocity at a particular point in time. I think.

Of course this would require backtracking through calculations the tweening library has already done (to get a normalized time value, etc.). Wouldn't it be easier to draw the image based on where the physics object is, maybe apply a force instead of setting linear velocity, and forget the tweening library?

Re: Using love.physic's setLinearVelocity to move towards a target following a cubic in eased curve

Posted: Sun Jan 01, 2017 11:05 pm
by Khranos
airstruck wrote:I think you should be able to use the derivative of the easing function to determine what the linear velocity should be at any point in time. If the easing function is x³ (where x is a "normalized time value" between 0 and 1), then the derivative is 3x² (you can derive functions using wolfram alpha or an algebra program). You should be able to plug in x to find the linear velocity at a particular point in time. I think.

Of course this would require backtracking through calculations the tweening library has already done (to get a normalized time value, etc.). Wouldn't it be easier to draw the image based on where the physics object is, maybe apply a force instead of setting linear velocity, and forget the tweening library?
Huh, it's been forever since I worked with derivatives... never was too good with them.

I appreciate the suggestion to just sync the image to the physics object -- that honestly sounds like the easiest option to both code and fine-tune to my liking. I'll probably do something more toward that route, thanks for the help.


Edit: I went ahead and switched the order (move object, sync image rather than the opposite), took all of 20 minutes and it's now working great. Seems there's always a simple solution lurking right around the corner of a complex problem.