- Home /
What is the best approach for a moving path curve?
Hello fellow developers,
I would like to know what is the best and easy solution for a situation like this.
Concept: In my game, 3 types of fruit will be thrown by the character from point A to Point B. Each fruit will follow a curve way, problem is the fruit will have the physics as the character will throw the fruit from slingshot and once after thrown, fruit will follow a path. Currently, I have implemented the slingshot system and the fruits are thrown.
Between, the character will move up and down so the fruit throw position will also change.
It is a 2D Game
Problem:
1.How to make the fruit seamlessly follow the specified curve, once thrown from the slingshot? as explained above, the player position will move up and down, so is it possible to update the fruit path with smooth looking force and physics?
Is it possible to use spline curve? If so how to maintain the gravity and force? It would be great if someone can show direction. Thanks in advance.

What I could think of:
Adjust the throw direction vector according to the movement of the balloon (adjust the y value of a vector3.right)
Instantiate the fruit and add a force to the rigidbody. Or set the velocity once.
When the fruit Object reaches the point where it should follow the predefined path, start an animation for the fruit that describes the path and runs on the fixed time step.
After the animation is finished, hand the object over to physics again.
You could use splines, but this way it's probably more managable
I'm not sure about which state the rigidbody has to be in for animation, but that should definitely work.
If you have to have the path absolutely specified, the method that follows wouldn't work, but a physics friendly approach, in my opinion, would be to look into joints. An empty gameObject would be thrown, but it would rotate - the projectile or what not, if attached with a joint, would make it move in a physics friendly path. Imagine throwing a ball with another ball attached by a string.
@hexagonius Thanks for your suggestion.
Since the player position [y] keep moving, how is it possible to animate the fruit? As, the fruits will be thrown from the slingshot and the slingshot moves up and down along with player. So for instance if the fruit is moved down the curve path will be changed.It is not possible to use animation as the fruit location will be changing dynamically.
@PhoenixBlackReal Thanks for your suggestion.
But how to make a curved path, is it possible in joints? As far as I have checked the unity joint it only has two joints, or may be am missing something.
Answer by fafase · Mar 05, 2015 at 04:54 AM
You should try Catmull Rom algorithm:
http://www.mvps.org/directx/articles/catmull/
Equation goes as such in 2D:
public static Vector2 CatmullRomTangent(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
{
return 0.5f * ((-p1 + p3) + 2f * (2f * p1 - 5f * p2 + 4f * p3 - p4) * t + 3f *
(-p1 + 3f * p2 - 3f * p3 + p4) * Mathf.Pow(t, 2f));
}
You define discrete points on your curve and it will define the movement naturally. The last parameter t is how accurate you want the curve. The higher t, the more expensive your curve but the more precise as well.
Since the equation takes A,B,C and D but it returns the path between B and C (look at explanation in the link) the first and last computation will get Vector2.zero for the first parameter (entry point) and for the last parameter (exit point).
For the rest you just pass all values in a array and use a for loop.
Great, Fafase. Thanks a ton. I will look in to this algorithm. But, I have few questions.
Am currently using spline curve and it pretty much gives the same output may be not precise but a nice curve. Problem is, am using rigidbody2D in order to throw the fruit which will have force applied to it. Fruit is following the curve but am not able to pass the rigidbody to the curve. I tried using colliders, so once the fruit enters the collider and it will start following the curve [spline curve] but it is not realistic it doesn't follow the physics rule, it looks fake.
Is there a way that I can use the gravity in the points? $$anonymous$$eaning, I want the fruit to have the physics property but not just some forward movement. As of now, I have created the spline curve and it moves through the points just with simple forward movement. How to enable physics in the curve?
Am I over thinking it? Or is there any other simple approach for it?
So you want the fruit to follow the curve and fall down because of gravity, so it means the fruit will not follow the curve but just be affected by it?
Then you may not need any algorithm for curve, more likely you take points on the curve as discrete sampling. Then you get the direction between each point you gathered and use the direction between each point. This direction is used to add some force to your fruit.
Finding the right amount of force, you get your fruit to be affected but still the gravity is till there as is the original force.
Is that what you are after?
Good point of that as your player goes in the game, he could gain skills so that the force applied gets stronger and the fruit gets to follow the path more accurately. (You can also charge IAP for that, ¥€$)
Yeah Exactly. I want the fruit to follow the curve path with the physics applied to it. So once the fruit is thrown from the player using the slingshot, the fruit will follow the curve but need to have physics.
As you suggested, I need to use discrete points for sampling and use the direction between the points to use as a force, am not sure how to implement this. If you don't $$anonymous$$d can you explain it with an example? So I can learn from it. Thanks.
As, I explained earlier the fruit is thrown from the slingshot. So am quiet confused how to keep the same force and follow the curve.
But is the fruit meant to cover the exact path or just be influenced by that line? In first case, Gravity can be removed when you start following the path, in second case, it is whatI explained more or less.
Your answer