| |
SIDWAVE Account closed
Registered: Apr 2002 Posts: 2238 |
Making smooth curve movements
K, so i want to make some kinda smooth curves to move sprites along.
I know I could use specially tailored sinus data, but then i still have to code something to do the movement exactly as i want it.
So i consider to make a small movement recorder, where i move a sprite with a joystick, and record the path.
So, now comes the hard part: if i record some (as perfect as can be) movement (a path), then how could i flatten out the recorded data afterwards, so my imprefect drawn move, becomes super smooth ?
This is some math i know nothing about, anyone ? |
|
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
interpolation using splines comes into mind.
a simple thing you can try is running a lowpass filter over your recorded data, that might already make it good enough. |
| |
Mr. SID
Registered: Jan 2003 Posts: 424 |
You can find lots of information about splines on the Internet. Try Wikipedia.
There are many different kinds of splines, depending on how you want your curve to look.
In all of these, you basically use control points to define the shape, and optionally vectors at the points to define the tangent of the curve at the control points, you might know that from vector graphics apps such as Illustrator or Corel Draw.
Some algorithms will make sure that the curve touches each control point, and others will only approximate the control point, but never really reach them.
I would start with a simple 4 point bezier curve:
http://en.wikipedia.org/wiki/Bezier_curve
If you get that, then you can do more advanced stuff... |
| |
Pantaloon
Registered: Aug 2003 Posts: 124 |
some kickassembler macros for doing interpolation.
.function linear(a,b,t)
{
.return [a*[1.0-t]] + [b*t]
}
.function cosine(a,b,t)
{
.var ft = t * PI
.var f = [1.0 - cos(ft)] * 0.5
.return [a*[1.0-f]] + [b*f]
}
.function catrom(a,b,da,db,t)
{
.var t2 = t * t
.var t3 = t * t2
.return a * [2.0 * t3 - 3.0 * t2 + 1.0] +
b * [3.0 * t2 - 2.0 * t3] +
da * [t3 - 2.0 * t2 + t] +
db * [t3 - t2]
}
.function cubic(a,b,t)
{
.var t2 = t * t
.var t3 = t * t2
.return a * [2.0 * t3 - 3.0 * t2 + 1.0] + b * [3.0 * t2 - 2.0 * t3]
}
|
| |
Devia
Registered: Oct 2004 Posts: 401 |
Splines in C=Hacking Issue #20
I guess you could also draw your path in any 3D prg or even MS Paint using splines, and export/convert the data to usable sprite coords.
Or you could go get something like this and just plot in your known coords in Excel and have it generate a complete coordinate table for you.
...which makes me wonder why Excel doesn't have functions for this already, as it clearly uses similar algorithms for displaying various types of graphs. |
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
Yet another way is to work with acceleration variables. So... You have a table (one for x-movement and one for y-movement) that specifies. "Add XX to movement speed x-wise for YY frames". If XX is negative, it will be a deacceleration and if it results in "negative" speed, then it becomes a movement in the other direaction. Even though you would have to tweak some values to get it exactly the way you want to, it will at least be quite easy to do so, since everything is specified by simple tables.
Since the movement will not just be "on/off", but a matter of gradual acceleration and deacceleration, the movements will be curved and smooth.
Just a thought... |
| |
Devia
Registered: Oct 2004 Posts: 401 |
yes but... it's quite early so maybe i'm a bit daft.. how do you calculate anything that would give you tables for this purpose? (or am I missing the point?)
|
| |
Hein
Registered: Apr 2004 Posts: 954 |
Quote: Yet another way is to work with acceleration variables. So... You have a table (one for x-movement and one for y-movement) that specifies. "Add XX to movement speed x-wise for YY frames". If XX is negative, it will be a deacceleration and if it results in "negative" speed, then it becomes a movement in the other direaction. Even though you would have to tweak some values to get it exactly the way you want to, it will at least be quite easy to do so, since everything is specified by simple tables.
Since the movement will not just be "on/off", but a matter of gradual acceleration and deacceleration, the movements will be curved and smooth.
Just a thought...
I find that a good approach for realtime curves:
See here |
| |
Slammer
Registered: Feb 2004 Posts: 416 |
Working with accelerations can give you movements of a second degree polynomial. If you spice it up a bit and use acceleration on the acceleration you can have third degree polynomials which makes it possible to do Bezier/Hermite spline movements in realtime without doing one single multiplication and without having to use large memory areas for tables.
|
| |
Devia
Registered: Oct 2004 Posts: 401 |
yes, but again: how would you calculate those tables? |
| |
Slammer
Registered: Feb 2004 Posts: 416 |
Siggraph have some material on splines, but if you are not familiar with the subject I guess this stuff can be hard to read (try searching for better pages).
http://www.siggraph.org/education/materials/HyperGraph/modeling..
|
| |
Soren
Registered: Dec 2001 Posts: 547 |
Funny thing... I used to make curves for smooth movements, manually... :-D
|
| |
Mace
Registered: May 2002 Posts: 1799 |
For those smart enough, I sense the opportunity to write a stunning tool that lets you create splines in hires to create sprite move tables. |
| |
SIDWAVE Account closed
Registered: Apr 2002 Posts: 2238 |
Thanks all for the input.
There is enough here to get going on.
|
| |
Martin Piper
Registered: Nov 2007 Posts: 722 |
If you don't want to use splines or other similar code then what you can do is have the joystick alter the velocity, not the position, of a sprite. Think of it like flying a thrust ship but instead of rotation you control the thrust vectors instead. Using the C64 if you use sub pixel accurate coordinates and velocity you can manually control your smooth movement. Getting the data to loop is a simple matter of getting close enough to your ending position and then triggering a lerp over several frames to close the loop. |
| |
Radiant
Registered: Sep 2004 Posts: 639 |
Another option if you don't want to use splines would be to use GNU Octave, input pivots into a matrix, and use the polyfit() function go get a polynomial approximation of the curve. |
| |
Perplex
Registered: Feb 2009 Posts: 255 |
If you want to create a smooth curve through manually set key points, one of the common solutions is to use Catmull-Rom Splines (see Pantaloon's post for math functions).
A good introduction can be found here: http://www.cs.cmu.edu/~462/projects/assn2/assn2/catmullRom.pdf
|
| |
MacGyver Account closed
Registered: Dec 2001 Posts: 149 |
Was a test, plz delete. |
| |
SIDWAVE Account closed
Registered: Apr 2002 Posts: 2238 |
all i really want is, that when i move a sprite to record the path, the outcome becomes smooth. nomatter what kind amove i do.
if you have knowledge to code that on c64, then do.
f.ex i move diagonal up right, then hard down right, a triangle. then the tip of the triangle shouldnt be hard, it should become a small smooth curve.
get it ? |
| |
Martin Piper
Registered: Nov 2007 Posts: 722 |
The method I described would fit what you need. |
| |
Hein
Registered: Apr 2004 Posts: 954 |
Quote: all i really want is, that when i move a sprite to record the path, the outcome becomes smooth. nomatter what kind amove i do.
if you have knowledge to code that on c64, then do.
f.ex i move diagonal up right, then hard down right, a triangle. then the tip of the triangle shouldnt be hard, it should become a small smooth curve.
get it ?
erm, first you say thanks, now you say fuck you.. wassup?
You can't expect people to do the code for you, else there's nothing to explore. I think there's enough options in this thread to choose from. |
| |
SIDWAVE Account closed
Registered: Apr 2002 Posts: 2238 |
get it ? = fuck you ?
if i hadnt complained before why csdb forums are utter shit, i do now, but it will be the last time. |
| |
Hein
Registered: Apr 2004 Posts: 954 |
Quote: get it ? = fuck you ?
if i hadnt complained before why csdb forums are utter shit, i do now, but it will be the last time.
No help is good enough for you, is it? |
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
@Rambones:
You move your sprite, and you record a series of coordinates. You can make this curve smoother by averaging values. For each value N, you add it together with the value before, and the value after and then you divide the sum you get with 3, and store the result into a new table. Now each adjacent value will "influence" the other adjacent values in a way that makes the overall curve smoother. In order to make the division simpler to implement (i.e. division with 4 instead of 3) then you could always add value N two times to the sum, which would also give a weighting in favor of the actual value obtained at each point.
In case this will turn out too rough (depends on how rough your input data is), you can add the current value + two previous and two subsequent values. If this is too rough, then add values further back/ahead into each sum. A good idea is to always add the values together like a "pyramid". For example, for value 5 in the series (N5) and a "scope" of two values ahead/behind, you could add values like this:
N5
N4 N5 N6
N3 N4 N5 N6 N7
This table means nothing else than N3+N4+N4+N5+N5+N5+N6+N6+N7 = X
Then you divide X with the number of values, X/9, to get the "smoothed" value for N5. Let's say you prefer to divide with 8 instead (if you do this on the C64 instead of pre-calcing it). Just remove one of these values and divide with 8 instead. It might very well look alright.
An alternative, in case your new table is still too rough, is to just run the same sort of calculation again in a second pass, to smooth the curve further.
A special case is when you deal with values in the beginning/end of the table, where you can't pick values ahead/behind because the table stops there.... You can figure out yourself how to deal with that in some way that looks alright.
Note that this approach will reduce all "peaks" in the data, so it will result in a path of movement which is more straight than the actual input. That is, it will not actually result in a curve which is "fitted" to the data as good as possible. For that you would have to do some curve fitting the way RadiantX suggested for example. Nevertheless, I suspect that you will find the approach I described here easier to understand/implement.
Note that most of the posts above were actually good and relevant answers to your question. What I suggest here is a worse approach than many of them, in terms of accuracy, but I guess it may work for your purposes. |
| |
Partz Account closed
Registered: Jun 2008 Posts: 17 |
I've been toying with the idea of writing an editor to do just this. The idea is that you will initially click on a canvas representing a visible screen space (say 320*200). Each click will add a vertex and between each vertex ALL points will be stored as calculated by a simple line drawing DDA. An option will then exist to smooth the points. There seem to be a fair few algorithms out there for this that don't call for the polynomial curves - much like the sliding window averaging approach mentioned above which looks like a simplified McMasters routine. The intention would be that vertex data could be saved off as either a dc.b text file for 64 usage or a static c# class/seriazable binary file for use in XNA.
I quite like the ideas regarding velocity discussed above - multiple objects look more natural bunching up and decelerating around a tight curve. Maybe if each vertex a target velocity for the end point so data is stored in x,y,v?. Might be a Christmas project.
Have you tried getting any of these ideas into a simple basic program yet, Rambones? |
| |
Mace
Registered: May 2002 Posts: 1799 |
This tool you are talking about... will it be some 'other platform tool' or something that will actually run on a C64?
I prefer (by far) the second option :-) |
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
Quote:if you have knowledge to code that on c64, then do.
so you don't actually want to know how this works, but you want someone to code it for you?
Quote:f.ex i move diagonal up right, then hard down right, a triangle. then the tip of the triangle shouldnt be hard, it should become a small smooth curve.
and thats a typical usecase for splines, as various people told you already. |
| |
Mace
Registered: May 2002 Posts: 1799 |
I think you should give Rambones a break, you are seeing things that aren't there...
|
| |
Partz Account closed
Registered: Jun 2008 Posts: 17 |
Quote: This tool you are talking about... will it be some 'other platform tool' or something that will actually run on a C64?
I prefer (by far) the second option :-)
It will be "other platform". The intent is to do it in C# under windows. The output won't be C64 specific, it will provide output thats useable in my XNA too. |