Advertisement

Circle movement

Started by April 19, 2023 11:32 PM
6 comments, last by kwikc 1 year, 6 months ago

Hi guys, how are u?

I'm trying to create a circular movement, in my game (kinda like WoW), I have W or S to move forward or backwards. And A or D to rotate left or right. Now I want to create a function that when I press W + A it will move in a circle of, let's say 5m radius, with my current Position and Forward vectors. What I used to do, is just rotate the vector by x angle depending on rotation_speed * dt and then update position just doing pos += dir * dt (with the current dir), even tho it looks correct, I was trying somestuff, and for example if I have a dt of 0.5, the circle willn't be a circle anymore, because it first rotate 0.5 degrees and then move again, will be a linear movement.

Anyone has any idea how to achieve this? Thanks!

Are you familiar with the parametric equation of the circle?

Google for: parametric equation of a circle. You will find that x and y are defined by cos and sin.

Advertisement

@taby I guess u mean something like this

pos.x = center.x + radius * std::cos( angle );
pos.z = center.z + radius * std::sin( angle );

Issue with that is that I need to update angle over time with a rotation speed, but, how do I determine my current angle given my forward direction?

Relative to what?

An angle is a difference in direction between 2 elements. So far you have mentioned only one element, your thing that moves. What's the base you compare against?

In traditional geometry education, the standard XY plane is often used. So you have an origin, you have an X axis in a known direction, and you have a turning direction (positive is counter-clockwise). In that context, something like “at (2, 6) at a 38 degrees angle” is well-defined.

You may not need a coordinate system like in my XY plane example, but you definitely need to define the direction you see as a 0 angle, and you need to define the turning direction(s). You can pick anything you like, depending on your choices the computation and the answer will change.

As soon as you have made clear what your base direction is, and what direction of turning you use. your problem is deciding the angle between two vectors (ie your movement direction and the direction of the base). That is a solved problem. I don't know the answer as I never do such stuff, but a random Internet search will give you plenty of solutions I am sure.

Rewaz said:

, I was trying somestuff, and for example if I have a dt of 0.5, the circle willn't be a circle anymore, because it first rotate 0.5 degrees and then move again, will be a linear movement.

I'm not sure why this is important. I'm assuming you are updating every frame so the step should be small. The next frame you will have a bit of turn, and the next frame a bit more. Do you need it to track exactly in a specific circle? It typically won't do that anyway unless you are on perfectly flat terrain. Any kind of collision is going to throw you off your circle. In addition, most capsule collision routines work in a piecewise linear way so unless you want to do something really fancy, it won't track an exact circle unless there are no collisions.

I can think of a way to get it to do this on flat terrain. It's still doing piecewise linear, but since there are no collisions, at the end of each frame you will end up in the right place pointing the right direction. For this I would pick your circle. The center would be either a point on the exact left or right. depending on which way you want to turn. The distance of the center from your avatar would be the turn radius. Then when you know your time step you can calculate where on the circle you should end up, if you travel the needed distance along the perimeter. From there you just set your direction, so you are pointing tangent to the circle for the next frame.

If you want to account for collisions, make sure to set your direction based on the final resting place of your avatar relative to the center you selected, and not the place you were targeting on the circle. Also make sure to reset your center point every frame in case you get pushed off your original circle.

This is just off the top of my head so take it with a grain of salt. I do my controls with just having a turn rate and a speed and it seems to work OK for me, but I'm not really trying to get an exact path. I just want reasonable controls.

The circle is the solution to some differential equation, and your code is just something like using Euler integration to solve it. I think you are doing what you are supposed to be doing. Make dt small enough that things look smooth and don't be obsessive about following the circle too precisely: It's not needed for a game.

Advertisement

Provided the question is still there, this link should be helpful, https://academo.org/demos/rotation-about-point/.

None

This topic is closed to new replies.

Advertisement