Hey folks!
I’m despertly trying to archive the following:
There’s a Level which must be rotated in order to move the player. Sounds pretty easy and it was, at first :-/
The problem is: The greater the distance to the center of the level, the faster the objects move around the pivot point. Its totaly logic but how can I caluclate the needed velocity to rotate everthing the at the same pace?
Here are an example. At the beginning points p1 and p2 have the same Y coordinate, e.g. p1(1,1) and p2(20,1).
Now the player starts to rotate the level and after a certian time it would look like this:
e.g.: p1(2,0.5) and p2(18,4)
p2 moved way more on the Y-Axis than p1.
I tried to rotate a empty gameobject with the level and grap its Y-Coordinates but since the whole level is made of rigidbodys i cant just transform the objects, i need to calculate a vector :-/
another attemp was to reduce the speed dependand on the distance between the player and the level center (with some divisions, multiplications etc.), but sooner or later i will get a negativ value und the level rotates in the opposite direction
Anyone any idea?
Thanks in advance
So you have a single object (the player) that is stationary and the whole level is being rotated? That sounds very, very expensive. Why not have the player rotate along with the camera and leave the level as-is?
Anyway, helpful terms to search for are ‘angular velocity’ & ‘angular displacement’.
Assuming w = angular velocity i.e. angle/time and r = radius from the center-of-rotation then the velocity is simply r * w.
In other words, scale the velocity by the radius (distance to the center).
The follow might help too perhaps: 2D Rotation about a point | Academo.org - Free, interactive, education.
because its a 2D clone of Monkey Ball. You rotate the level and the player rolls physic based to the target.
I’m not quite sure if I understandyour approach.
The subjective rotation speed need to be same depending on how far the player is away from the center.
a simple linear function like
rotateDelta = rotateSpeed - distanceToCenter
wont work.
I tried different exp-functions but nothing generates a value that makes the level look to rotate at the same speed.
if I just rotate the camera I have to do all the calculations for the player velocity, rotation (its a ball) etc all by myself which exeeds my knowledge by far.
I worked as a web developer for the past 15 years so my math class lays way back. I’m currently working my way back to what i used to know but for now i just need this function to calculate the rotation speed ^^
I didn’t say use that function.
Take a movement of angle A for instance.
You’d use X = cos(a) and Y = sin(a) but this gives you a movement around a unit-circle i.e. radius = 1.
For a radius R you’d use:
X = R * cos(a) and Y = R * sin(a).
If A was the angular displacement in a specific time then this is what you describe i.e. a movement over a specific radius in a certain time.
Finally, the link I gave you (2D Rotation about a point | Academo.org - Free, interactive, education.) shows you how to rotate around a point.
thanks for you replies
I thnik the anwser in the link might work.
So i have to rotate the object around the players current x coordinate.
But i have no idea how to implement this.
currently i rotate the object using (simplyfied)
rigidbody2D.moveRotation(transform.rotation + rotateDelta * Time.FixedTimeDelta).
I guess know I have to calculate a vector and apply a force?
And R = distanceToCenter / 2 ?
Rigidbody2D.MoveRotation rotates the Rigidbody2D around its center of mass, not around an arbitrary point.
You’d need to calculate the new position to move to then use Rigidbody2D.MovePosition.
EDIT: Sorry, I guess you mean you know how to rotate the Rigidbody2D just not how to reposition it.
I still don’t see why you can’t just have the camera rotate! What you’re doing seems to be expensive.
Here’s an example of incrementally moving a Rigidbody2D per fixed-update with a specified angular-velocity (in radians) around a specified center-point.
Note that this can be optimized but it’s expanded here for clarity.
using UnityEngine;
public class RotationTest : MonoBehaviour
{
public float AngularVelocity = 1.0f;
public Vector2 Center = Vector2.zero;
Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
var newPosition = PointRotation (Center, rb.position, AngularVelocity * Time.fixedDeltaTime);
var newAngle = Mathf.Rad2Deg * AngleToPoint (Center, rb.position);
rb.MovePosition (newPosition);
rb.MoveRotation (newAngle);
}
Vector2 PointRotation (Vector2 center, Vector2 point, float angle)
{
var c = Mathf.Cos(angle);
var s = Mathf.Sin(angle);
point -= center;
point.x = point.x * c - point.y * s;
point.y = point.y * c + point.x * s;
point += center;
return point;
}
float AngleToPoint (Vector2 center, Vector2 point)
{
Vector2 offset = point - center;
return Mathf.Atan2 (offset.y, offset.x);
}
}
Hope this helps.
i dont want to reposition the rigidbody. The struckte looks like this.
GameObject (Level)
- wall_1
- wall_2
- Obstacles
-obstacle1
-obstacle2
and so on.
first theese things only had a collider, but this messes with the physics calculations since you could rotate them only by using transform. so i had to add a rigidedbody to the root game object and all children (all in Kinematic mode).
i tested this with around 1000 objects and could’t see a significant performance loss. But I#m always open to learn new things
So if I rotate the camera i have to do all the physic calculations by myself?
e.g. the camera rotates 50° to the left, then the ball should start to roll “down” the new slope. And if i rotate the camera to 0 the ball should slowly starts to stop. Since there is no actual slope i think i must calcalte a vector what the slope would look like and add it over time as a force to the player?
I have absolut no clue how to archive this. besides there are some power ups which accelerate the player in a direction, like a boost. i get this done by calculating a vector based on the rotation of the power up and apply that force.
(calculate velocity vector based on direction - Questions & Answers - Unity Discussions)
and what about jumping? At the moment I hit space and a force is applied to the player so the ball jumps, containing its current velocity.
is there really no such function that could calculate me a delta to lower the rotation speed depending on how far away i’m from the center?
i played with the anwser provided in the link but the results were… crazy
edit:
whoa!! Thanks i will try your code at once. I’m really really thankful for your support! :)))
If you’re rotating the camera and not the world but you want the gravity to change with the camera angle then just change the global gravity.
you are kidding me? simply change the gravity…
I will try both. Im not sure if your script works for me since i dont want to reposition the object but its pretty impressive you did it on the fly for a newbie like me.
thank you so much!
Here is a video of what i mean:
at the end part the ground rotates way to fast, even with FixedUpdate() the physic gets messy
@MelvMay how is the alpha build going hehehe
rotating the camera did the trick! Thanks again
1 Like
Had all the physics stuff in it ages ago. Just waiting for those that be to get the thing out in the wild. Supposed to be imminent though.
1 Like