switz
switz

Reputation: 25188

Cocos2D Bezier curve around object as if by gravity

I'm trying to manipulate an object. When it gets near another object, let's say a globe, I want the globe to have a gravitational pull on the original object. I know I'm supposed to use CCBezierTo, so this isn't so much a programming question as it is a math question.

Mathematically, how could I figure out the three points of the bezier curve (1, 2, and end) and give it a weight depending on its distance (further away = less pull). I already have the distance mapped out in a variable.

Think of a spaceship slingshotting around the moon.

Code:

ccBezierConfig bezier;
bezier.controlPoint_1 = ccp(projectile.position.x + 10, projectile.position.y + 20);
bezier.controlPoint_2 = ccp(projectile.position.x + 20, projectile.position.y + 40);
bezier.endPosition = ccp(projectile.position.x + 30, projectile.position.y+60);
id bezierAction = [CCBezierTo actionWithDuration:1 bezier:bezier];
[projectile stopAllActions];
[projectile runAction: bezierAction];

Upvotes: 10

Views: 2461

Answers (1)

whoplisp
whoplisp

Reputation: 2518

The trajectory would be a conic section (line, hyperbola, parabola, ellipse or circle).

You can represent those as a rational Bezier curve. http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html and http://www.cs.unc.edu/~dm/UNC/COMP236/papers/farin.pdf.

If you insist on using quadratic Bezier sections, I would use a function like this http://www.netlib.org/minpack/lmder.f to find optimal positions of control points by least-squares minimization.

I think it would be easiest if you just calculate the conic sections and draw them as line loops.

Or you implement a verlet integrator and solve the equations of motions.

Upvotes: 1

Related Questions