Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Elisvaldo · Nov 09, 2015 at 03:22 PM · performancephysics2djointspathfollowingbezier

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MelvMay ♦♦ · Nov 10, 2015 at 07:20 AM 0
Share

$$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?

avatar image ijidau · Nov 10, 2015 at 07:45 AM 0
Share

I posted an answer, just waiting for moderator approval :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Elisvaldo · Nov 11, 2015 at 02:30 PM 0
Share

Looks great! Will implement this and see how it goes. Thank you!

avatar image MelvMay ♦♦ · Nov 11, 2015 at 05:34 PM 0
Share

Note that if you want to use point-forces then you can use the PointEffector2D.

http://docs.unity3d.com/ScriptReference/PointEffector2D.html

avatar image Elisvaldo · Nov 11, 2015 at 05:58 PM 0
Share

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.

avatar image
0

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);
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges