Cosine wave problem
BlitzMax Forums/BlitzMax Programming/Cosine wave problem
| ||
In my program, I'm having an object move back and forth between two points, going faster in the middle. I achieve this using cosine, however it flickers badly at times, and I'm not sure why this happens.Local st:Float = ((Cos((MilliSecs()/time) Mod 360))+1)/2 Local xdist:Int = x2 - x Local ydist:Int = y2 - y x = xdist*st y = ydist*st st ends up being a float between 0 and 1, and then I base the coordinates on that. I'm almost certain I've had this problem before, but I don't remember how I fixed it. The flickering is at its worst when I click the top of the window and release... not just because my use of Millisecs(), though -- it's as if it's drawing in two places at once. |
| ||
Flip 1 |
| ||
That doesn't seem to make a difference, but thanks! |
| ||
Had a quick bash at this:Graphics 800,600 x1:Float = 100 'Point 1 y1:Float = 500 x2:Float = 500 'Point 2 y2:Float = 200 xc:Float = (x1+x2)*.5 'Center yc:Float = (y1+y2)*.5 inc=0 While Not KeyHit(KEY_ESCAPE) px=xc+(Sin(inc))*(xc-x1) py=yc+(Sin(inc))*(yc-y1) Plot x1,y1 Plot x2,y2 Plot xc,yc Plot px,py Flip Cls inc=inc+1 Wend |
| ||
If your phase (the variable in the brackets of the cos bit) i.e: cos(phase) varies too quickly the value can literally 'rotate' from one side of the cos function to the other in a frame. This give the appearance of plotting in two places. |
| ||
Sledge: That seems to be precisely what I needed, thanks! Nomen: That sounds like exactly what I was seeing. Why does this happen, though? |