Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by svope · Jan 26, 2016 at 10:54 AM · movementphysics2daddforceplanetcircular

Circular movement using AddForce()

Hello,

I'm trying to make "moon rotating around planet like" scene in Unity using physics 2D.

alt text Let's call white circle "moon" and the black one "planet". The purple/pink trail is made by Trail Renderer.

The code is:

 void FixedUpdate ()
     {
         // transform.position is position of "planet"
         Vector2 force = transform.position - moon.position;
         // velocity vector which is perpendicular to force vector
         Vector2 vel = new Vector2( force.y,-force.x);
 
         Rigidbody2D moon_rigidbody = col_tran.GetComponent<Rigidbody2D>();
         // velocity_param is just float to adjust velocity
         moon_rigidbody .velocity = vel.normalized * velocity_param;
         
         moon_rigidbody .AddForce(  force.normalized * moon_rigidbody .velocity.sqrMagnitude / force.magnitude );
         
     }  

The equations in code are based on this .

My problem is that the moon is getting closer and closer to the planet. Why is that? I though that the moon will be moving on the same circle hole time. What am I missing?

unity.jpg (22.9 kB)
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 brunocoimbra · Jan 26, 2016 at 11:59 AM 0
Share

if you want an uniform circular motion, you should NOT use AddForce, as it will break the uniform movement.

avatar image gfaraj · Nov 10, 2017 at 08:31 PM 0
Share

Did you end up figuring out how to do this with physics 2D?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by agmcorp · Aug 09, 2020 at 05:25 PM

@svope I think it's due to the precision of the calculations. I solved like this:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AddForce2 : MonoBehaviour
 {
 public float speed = 2.0f;
 public GameObject center;

 private Rigidbody2D _rigidbody;
 private Transform _transform;
 private float _radius;

 void Awake()
 {
     _rigidbody = GetComponent<Rigidbody2D>();
     _transform = GetComponent<Transform>();
     _radius = GetDistance(center.transform.position);
     AddDistanceJoint2D();
 }

 void FixedUpdate()
 {
     Vector2 centripetalForceDirection = (center.transform.position - _transform.position).normalized;
     Vector2 tangentVelocityDirection = GetPerPendicularDirection(centripetalForceDirection);
     _rigidbody.velocity = tangentVelocityDirection * speed;
 }

 private float GetDistance(Vector2 center)
 {
     float x_d = center.x - _transform.position.x;
     float y_d = center.y - _transform.position.y;
     return (float) Math.Sqrt(x_d * x_d + y_d * y_d);
 }

 private Vector2 GetPerPendicularDirection(Vector2 v)
 {
     return (new Vector2(v.y, -v.x)).normalized;
 }

 private void AddDistanceJoint2D()
 {
     DistanceJoint2D distanceJoint = gameObject.AddComponent<DistanceJoint2D>();
     distanceJoint.connectedBody = center.GetComponent<Rigidbody2D>();
     distanceJoint.autoConfigureDistance = true;
     distanceJoint.maxDistanceOnly = true;
 }
 }
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

38 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 avatar image avatar image

Related Questions

Circular movement of gameobject with rigidbody 1 Answer

Simulate perfect circular motion with addforce 0 Answers

AddForce/ForceMode.Impulse 1 Answer

how to move an object in a curved way 4 Answers

Is it ok to use Linear Drag and/or Mass to make an object immovable? 2 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