2
\$\begingroup\$

I'm not a pro in LibGDX and physics but I know basics, vectors, actor's actions and so.

I'm trying to achieve a movement like on the gif below:

enter image description here

For now, let's say that I have a stage and a central actor that the moon and comet will rotate around.

The problem is here that I have no idea if this is achievable with 2D. Should I implement some sort of 3D in LibGDX?

Also, is there a way so these objects move in a 'random' path, not the straight line like the comet below? (Moon is a little bit randomized)

I know that this question is too broad, but I really didn't know where to ask about this. I'm here to learn cause I always wanted to build a thing like this. I personally think that this is achievable with LibGDX's actions cause I saw something similiar somewhere.

If someone could point me the way I would be grateful :) Thanks!

\$\endgroup\$
4
\$\begingroup\$

Well its a type of circular motion but with planets or satellite circular path is changed into elliptical path. Look at the picture below.

enter image description here

It is easy to implement all you have to do is:-

Add a continuous force on satellite or any object you want to move around. And The Direction of force should be towards the center of Planet

Suppose p1 is the position of satellite and p2 is the position of planet then direction will be p2-p1.

its implementation in unity3D:-

using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to that object around which another object will rotate
/// </summary>
public class Spin : MonoBehaviour {
    public float maxGravitationalFieldDistance;
    public Transform satellite; // This is refrence of our satalite
    public float Fnet;      // 
    public float velocity;

    private float radius;

    void Start()
    {
        radius = Vector3.Distance(transform.position, satellite.position);
    }

    // Update is called once per frame
    void Update () {

        MoveSatellite();
        ApplyForce();
    }

    void MoveSatellite() {
        // satellite will move 
        satellite.transform.Translate(SetSatelliteMoveDirection(false) * velocity * Time.deltaTime);

    }
    void ApplyForce()
    {
        if (radius <= maxGravitationalFieldDistance)
        {
            //p2 - p1 (direction of force is always towards the center of planet)
            Vector3 forceDir = transform.position - satellite.position; 
            forceDir = forceDir.normalized; 

            //Apply force (it depend upon the mass of satellite so adjust mass of satellite carefully)
            satellite.GetComponent<Rigidbody>().AddForce(forceDir * Fnet * Time.deltaTime); // constant force
        }
    }

   Vector3 SetSatelliteMoveDirection(bool tengintial = true)
    {
        if (tengintial)
        {
            //cross product tengintial direction 
            Vector3 Direction = (Vector3.Cross((transform.position - satellite.position), Vector3.forward)).normalized;
            return Direction;
        }
        return Vector3.right;
    }

}
\$\endgroup\$
  • \$\begingroup\$ i actually like this answer, probably the best answer youl get. \$\endgroup\$ – Ryan white Aug 17 '16 at 14:34
0
\$\begingroup\$

You could try to rotate it from the exact position of the actor you want it to rotate around and set the offset.

In the matrix multiplication you just have to transform it after rotating it (so inversed matrix multiplication).

I hope this was helpful. :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.