- Home /
Bezier Path considering external forces
I'm currently trying to make an AI spacecraft (2D project) move through a bezier curve. But I would also like to have external forces actuate to that spacecraft (i.e. a black hole).
My first thought was to simply set the .position/.velocity, but then I quickly learn how that overrides all external forces acting upon the spaceship.
My second idea was to make an invisible GO going through that path and attach it to the spacecraft with a spring joint. However, this yields some unpleasant rotation of the spacecraft, since the spring is free to rotate as it wishes.
My last idea was to create to two bezier curves, parallel to each other, on which two anchors will travel. I've put the spacecraft in the middle and attach it to the anchors with the spring joints.
It seems extremely functional and exactly what I wished. However, I do feel I'm "reinventing the wheel" and having two springs per spacecraft should be a considerable resource drain for physical calculations for a single spacecraft, specially when I intend to have dozens of them in the screen at the same time.
Does anyone have any good idea to approach this problem? Am I missing something simple and overcomplicating the solution?
Thank you very much!
$$anonymous$$aybe you could use Rigidbody2D.$$anonymous$$ovePosition, feeding the positions along the line to the Rigidbody2D each fixed-update.
http://docs.unity3d.com/ScriptReference/Rigidbody2D.$$anonymous$$ovePosition.html
EDIT: Ah sorry, I re-read and realize you want external forces to move the spacecraft. In the case where it is 'off-course' from the path, you'd want it to slowly veer back on-course?
I posted an answer, just waiting for moderator approval :)
Answer by ijidau · Nov 11, 2015 at 01:04 AM
Try modelling the problem with attract forces. Imagine a target position moving along the bezier curve is really just like a black hole attractor. With this model, multiple attractors (even with different strengths and proximity) create a compound force and therefore movement. The nice thing is that this is closest to the underlying Physics model.
Proximity to an attractor can affect the strength of the force, just like local gravity. See Add Gravitational Attraction to RigidBody2D and Creating Local Gravity To Attract Specific GameObjects
You may also find some inspiration from a similar concept of compound forces, but a slightly different model: Path Following steering behavior from the seminal work of Craig Reynolds - Steering Behaviors For Autonomous Characters (He coined the term 'boids'). This approach has been developed into a Unity toolkit: UnitySteer
Looks great! Will implement this and see how it goes. Thank you!
Note that if you want to use point-forces then you can use the PointEffector2D.
http://docs.unity3d.com/ScriptReference/PointEffector2D.html
Seems to be working great! Some parameter fine tuning might be necessary in the future as it will probably have some stability issues. Also had to make a couple of changes, such as subtract the previously set force and give a certain gain, dependent on how far it is.
Answer below on how it ended looking like.
Answer by Elisvaldo · Nov 11, 2015 at 06:00 PM
void FixedUpdate()
{
_time += Time.fixedDeltaTime / 5;
var target = _bezierPath.GetPosition(_time);
ApplyForce(target);
}
private void ApplyForce(Vector2 target)
{
Vector2 offset = target - (Vector2)transform.position;
const float strenght = 10f;
var magsqr = Vector2.SqrMagnitude(offset);
var force = (offset*strenght*magsqr*magsqr);
_rigidbody2D.AddForce(force - _previousForce*GetPreviousForceGain(magsqr), ForceMode2D.Force);
_previousForce = force;
}
private float GetPreviousForceGain(float magsqr)
{
const int magsqrThreshold = 1;
const float maxGain = 0.5f;
return Mathf.Clamp(magsqr/magsqrThreshold, 0, maxGain);
}
Your answer
Follow this Question
Related Questions
Drag Player Object using Physics 2D 0 Answers
Why is my OnJointBreak() not working? 0 Answers
2D Platforme Rope 1 Answer
How to detect when a 2D joint is broken? 0 Answers
Dynamic wire using multiple DistanceJoint2D have a problem 0 Answers