Cocos2D Bezier curve around object as if by gravity
Asked Answered
A

1

10

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];
Ahner answered 6/6, 2011 at 4:18 Comment(2)
Why don't you just apply a force vector to the projectile proportional to the square of the distance to the object? i.e. why not just "do gravity"? The path will be naturally curved, assuming the projectile isn't heading directly towards the centre of the object.Deluge
oreilly.com/catalog/9780596000066Sulfonate
O
1

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.

Operand answered 2/7, 2011 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.