🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Simple trig problem driving me crazy

Started by
13 comments, last by Catch_0x16 5 years, 11 months ago

Assuming you want to rotate a point around another point, it is hard to tell from this snipped what you are doing: The problem with your math is that you are using the vector instead of the magnitude of the vector.

First, sin and cos depends on the rotation direction, so if you are using clockwise rotation then you were correct from the start. If not you need to reverse it.

RotationFlip.jpg.1e77fbec19bc0471c4146888c89e02ea.jpg

So this part:


	float angleRad = DEGREES_TO_RADIANS(rotationDegrees);
	float offsetX = sin(angleRad) * mOffset.x;
	float offsetY = cos(angleRad) * mOffset.y;

// This is what it should be
	float angleRad = DEGREES_TO_RADIANS(rotationDegrees);
	float offsetX = sin(angleRad) * mOffset.magnitude;
	float offsetY = cos(angleRad) * mOffset.magnitude;

 

To calculate magnitude you can use pythagoras theorem. https://en.wikipedia.org/wiki/Pythagorean_theorem It is just Root((A*A) + (B*B))

Note: @Michael LaserBlade Scoggin method should be better on performance if it works for you , because calculating root is slow-ish.

 

Tried and tested Unity script:

Spoiler

 



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateAround : MonoBehaviour {

    public GameObject Target;
    public float RotationInDegree = 30f;

    Vector3 Offset = Vector3.zero;
	// Use this for initialization
	void Start () {
        Offset = Target.transform.position - this.transform.position;//This should be captured once
	}
	
	// Update is called once per frame
	void Update () {
        this.transform.position =
            (new Vector3(Mathf.Sin(RotationInDegree*Mathf.Deg2Rad), Mathf.Cos(RotationInDegree * Mathf.Deg2Rad), 0f)* Offset.magnitude);
    }
}

 

 

 

 

Advertisement

Thanks all, as promised a working gif.

I ended up using transforms in the end, I didn't realize they were implemented so well in SFML (doh!)

Thanks also to @Scouting Ninja for a great response :)

2 hours ago, Guy Leonard Thomas said:

Thanks all, as promised a working gif.

I ended up using transforms in the end, I didn't realize they were implemented so well in SFML (doh!)

Thanks also to @Scouting Ninja for a great response :)

I'm glad it worked out for you.  Trig is one of those things that I have to draw out or I don't understand and if your head is not in that space often then it's easy to get things backwards or just forget how it works, causing issues like this that just leave you scratching your head.  Thankfully, this is where abstractions come in.

You're one step away from a transform hierarchy now, a general form of having "game object" with local positions, rotations and scales and a parent game object that has all of the same including a partent game object.  All the way up to a root node.  This allows you to just take entire parts of your scene and move them around naturally.

I agree, this is actually what I'm doing. I was previous passing down the position and rotation of the parent object, and moving the sub-objects around to suit the new position.

Having a proper transformation matrix makes this really easy now as I simply pass a reference to the parent transform, far fewer lines of code!

This topic is closed to new replies.

Advertisement