Best method for circular motion?
Moderator: Moderators
Best method for circular motion?
Trying to do some orbital motion type stuff. Was wondering what you guys have used as a best method. I've tried a Horizontal + Vertical velocity type approach...it's kinda neat but definitely isn't consistent around a moving centerpoint (like an object moving around a player). Also, tried some look up tables, but wasn't really happy with the results there either it seemed to cause lag (though I'd have to comb though to be absolutely sure that's where the lag was coming from, as I get the impression that should've been quite fast).
Was wondering if any of you had some approaches you could suggest. Thanks!
Was wondering if any of you had some approaches you could suggest. Thanks!
- rainwarrior
- Posts: 8062
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Best method for circular motion?
If the radius of the circle is fixed, a lookup sine table usually works pretty well for me.
If it doesn't have to be a perfect circle, but just curved motion, you can just use an object with acceleration that flips direction, i.e.
1. Accelerate left+up for 1 second.
2. Accelerate right+up for 1 second.
3. Accelerate right+down for 1 second.
4. Accelerate left+down for 1 second.
5. Repeat.
Basically, if you look at it in one axis, the acceleration function looks like a square wave, but as it integrates into velocity, the velocity looks like a triangle wave, and as that integrates into position, the position looks nice and curvy, almost like a sine wave. A circle is just a sine wave in both axes at once (with a 90 degree phase difference). The starting velocity is important to achieve this phase difference, to get a circle instead of just a diagonal wobble.
You see this all the time in games for enemies that just need to bob up and down (e.g. castlevania medusa heads) using flip-flop acceleration you get something that's probably as good as a sine wave for this purpose. This 2D variation makes a faux-circle instead.
The speed and radius is a little bit tricky to adjust, because it's dependent on both the acceleration constant, and the interval time. Acceleration and interval are both directly related to speed and radius, but in different ways. You can write yourself a calculator that simulates a full circle and gives you the right values, or you can just play with them until you like how they look.
If it doesn't have to be a perfect circle, but just curved motion, you can just use an object with acceleration that flips direction, i.e.
1. Accelerate left+up for 1 second.
2. Accelerate right+up for 1 second.
3. Accelerate right+down for 1 second.
4. Accelerate left+down for 1 second.
5. Repeat.
Basically, if you look at it in one axis, the acceleration function looks like a square wave, but as it integrates into velocity, the velocity looks like a triangle wave, and as that integrates into position, the position looks nice and curvy, almost like a sine wave. A circle is just a sine wave in both axes at once (with a 90 degree phase difference). The starting velocity is important to achieve this phase difference, to get a circle instead of just a diagonal wobble.
You see this all the time in games for enemies that just need to bob up and down (e.g. castlevania medusa heads) using flip-flop acceleration you get something that's probably as good as a sine wave for this purpose. This 2D variation makes a faux-circle instead.
The speed and radius is a little bit tricky to adjust, because it's dependent on both the acceleration constant, and the interval time. Acceleration and interval are both directly related to speed and radius, but in different ways. You can write yourself a calculator that simulates a full circle and gives you the right values, or you can just play with them until you like how they look.
Re: Best method for circular motion?
The homebrew NES game Thwaite determines the approximate unit vector by taking an arctangent (some reflections, a division, a table lookup, and compensating reflections), followed by a sine and cosine (table lookups). Then it determines the distance by taking the dot product of the displacement with the unit vector (two multiplications and one addition). This unit vector also gives the direction along which you must accelerate
Wrecking Ball Boy, an unfinished NES-style game written in Python with essentially 8-bit physics, uses the dot product technique to keep the character at the correct distance along the rope.
Wrecking Ball Boy, an unfinished NES-style game written in Python with essentially 8-bit physics, uses the dot product technique to keep the character at the correct distance along the rope.
Re: Best method for circular motion?
Rainwarrior - essentially, I've tried both of those methods but didn't really get the results I wanted. I think I'll end up using the organic movement sort of nature of messing with accelerations around a moving point for other things, but yeah...this particular usage should be pretty evidently a circular path.
With the LUT - do you essentially have a top-left corner point defined, and then the lookup table is used to add values to that point in the x direction and then in the y direction, run a variable offset to cycle through the table values?
With the LUT - do you essentially have a top-left corner point defined, and then the lookup table is used to add values to that point in the x direction and then in the y direction, run a variable offset to cycle through the table values?
- rainwarrior
- Posts: 8062
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Best method for circular motion?
There are a thousand ways to create and apply a lookup table for circular motion. A few things you might try:
- It could be a table of signed or unsigned offsets.
- You might scale a fetched value with bit shifts to get circles of different sizes.
- The table could be a sine specifying a fixed offset from a locus point.
- The table could be a table of deltas specifying the distance to move each frame.
- You could look up two successive sine positions, and subtract to procedurally calculate a motion delta.
- You could use your sine table for velocity, rather than position.
Re: Best method for circular motion?
Please be more specific about what kind of "circular motion" you're looking for. Is it static motion along an arc of a circle, or dynamic (you talked about orbital motion?).
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
- rainwarrior
- Posts: 8062
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Best method for circular motion?
Also, kind of a funky way to do the other method, have a target point, and just always accelerate toward that point, i.e. if X > target X, accelerate left. This basically makes a spring out of the target point.
From there, to make it circle you just have to give it an appropriate starting velocity and position (or you can add some rules that adjust acceleration in anti-orbit cases to automatically establish a clockwise orbit). You can move the target around, and it will continue to orbit dynamically. Clamping to a maximum velocity would also keep it from ever springing too far.
Hmm. I'm going to put a creature like this in my game, now that I'm thinking about it. Ha ha. Thanks for inspiring the idea.
From there, to make it circle you just have to give it an appropriate starting velocity and position (or you can add some rules that adjust acceleration in anti-orbit cases to automatically establish a clockwise orbit). You can move the target around, and it will continue to orbit dynamically. Clamping to a maximum velocity would also keep it from ever springing too far.
Hmm. I'm going to put a creature like this in my game, now that I'm thinking about it. Ha ha. Thanks for inspiring the idea.
Re: Best method for circular motion?
Yeah, the target point method is essentially what I'm working with. I can get oscillation in either axis just fine, I just can't get a dependable circular motion that way...or even close, no matter how i populate the starting values or starting velocities (LOTS of trial and error, too). I mean, it's a cool organic sort of movement, but certainly not orbiting 'around' the orbit object.
TheFox - just simple orbiting around an object. I wanted something a little more organic than strict table read (so if center point was moving, it would drift to it and continue its orbit). But I can't even make a dependable initial circle using an acceleration/target method.
Rainwarrior - any tips on better control over this?
TheFox - just simple orbiting around an object. I wanted something a little more organic than strict table read (so if center point was moving, it would drift to it and continue its orbit). But I can't even make a dependable initial circle using an acceleration/target method.
Rainwarrior - any tips on better control over this?
- Drew Sebastino
- Formerly Espozo
- Posts: 3496
- Joined: Mon Sep 15, 2014 4:35 pm
- Location: Richmond, Virginia
Re: Best method for circular motion?
I think the only real convincing way of doing this would be to somehow calculate the distance from the center for every angle measure, like directly above the canter could be (0,6) and directly to the side would be (6,0). I don't know how you'd do this though.
Re: Best method for circular motion?
That's called sin(x) and cos(x) ...
- Drew Sebastino
- Formerly Espozo
- Posts: 3496
- Joined: Mon Sep 15, 2014 4:35 pm
- Location: Richmond, Virginia
Re: Best method for circular motion?
Sorry I'm not a genius like you are.lidnariq wrote:That's called sin(x) and cos(x) ...
This idea could work though, right?
Re: Best method for circular motion?
Right, lidnariq - it seems the quickest method would be to just do the sin/cos math computation externally and populate a data table with the values...but that makes it complete static. That was one of my approaches. The other one is similar to what Rainwarrior is describing with accerleating around a centerpoint, but that one is a little too wild and not quite predictable enough for this purpose. So I was just seeing if there were methods I hadn't thought of. 
This is a minor thing in the idea for the game anyway...I just hate getting stumped. haha
This is a minor thing in the idea for the game anyway...I just hate getting stumped. haha
Re: Best method for circular motion?
This is the idea: http://www.helixsoft.nl/articles/circle/sincos.htm (See the section "Drawing a circle".)Espozo wrote:Sorry I'm not a genius like you are.lidnariq wrote:That's called sin(x) and cos(x) ...(Kidding!) All I know is that sin and cos can help you find the side measures of right triangles. (Would you believe that I'm in the advanced math class?)
This idea could work though, right?
Last edited by Movax12 on Sun May 24, 2015 1:14 pm, edited 1 time in total.
Re: Best method for circular motion?
Espozo, don't go just using the formulas teachers give you without understanding what they do. As a programmer, you have the obligation to understand how you reach the results you do.
By definition, sine and cosine are the vertical and horizontal distances from the center to the edge of a circle of radius 1, which is pretty much what you described. This means that by multiplying the sine and the cosine by other values you can increase and decrease the radius of the circle.
By definition, sine and cosine are the vertical and horizontal distances from the center to the edge of a circle of radius 1, which is pretty much what you described. This means that by multiplying the sine and the cosine by other values you can increase and decrease the radius of the circle.
- Drew Sebastino
- Formerly Espozo
- Posts: 3496
- Joined: Mon Sep 15, 2014 4:35 pm
- Location: Richmond, Virginia
Re: Best method for circular motion?
So you can only find the length radius and not the distance relative to the center of the circle? E.g. (5,3) (Wait a minute, you said you find the coordinates. You have to know what the radius is to do anything...) How are the positions of multijointed objects calculated? I thought it would have been nice if you had it to where you plugged in what you want the radius to be and the angle measure and it gives you the x and y coordinates, assuming the center is 0 I guess. (These new numbers would be added to the position of the center of the circle for the final value.)tokumaru wrote:By definition, sine and cosine are the vertical and horizontal distances from the center to the edge of a circle of radius 1, which is pretty much what you described. This means that by multiplying the sine and the cosine by other values you can increase and decrease the radius of the circle.
Edit: Never mind this. In theory though, you could just convert a formula into code and it would work just fine?
Yeah, it's making sense to me now. The teacher said nothing about its relationship to a circle strangely.
