Understanding cos & sin
BlitzMax Forums/BlitzMax Beginners Area/Understanding cos & sin
| ||
| Does anyone know how to make an object move along a horizontal wave, and perhaps another demo on how to move an object on a vertical wave? I've seen this done before, and although I've managed to do it once or twice I can never remember which args you put to make the object move really far from its "center point" or close. I would love if someone could post something on this subject as its great help to us newbie developers! :D |
| ||
Something like this?Const Vertical:Int = 0 Const Horizontal:Int = 1 Type TMover Field Speed:Float Field X# , Y# Field Angle:Int = 0 Field Mode:Int Function Create:TMover(X , Y , Speed:Float,Mode:Int = Horizontal) Local M:TMover = New TMover M.X = X M.Y = Y M.Speed = Speed M.Mode = Mode Return M End Function Method Update() Select Mode Case Horizontal Y:+ (Sin(Angle) * Speed) X:+ Speed Case Vertical X:+ (Cos(Angle) * Speed) Y:+ Speed End Select If X > GraphicsWidth() Then X:- GraphicsWidth() If X < 0 Then Y:+ GraphicsHeight() If Y > GraphicsHeight() Then Y:- GraphicsHeight() If Y < 0 Then Y:+GraphicsHeight() Angle:+ 1 If Angle > 359 Then Angle = 0 Draw() End Method Method Draw() DrawOval X , Y , 15 , 15 End Method End Type Graphics 800 , 600 , 0 , 60 Local M:TMover = TMover.Create(400 , 300 , 0.8 , Vertical) Local M2:TMover = TMover.Create(400 , 300 , 1.6 , Horizontal) While Not KeyHit(Key_Escape) Cls SetColor 0,255,0 M.Update() SetColor 255,0,0 M2.Update() Flip Wend |
| ||
SuperStrict
Global image:TImage = LoadImage("mouseloc.png")
'----------------------------------------------------------------------------------------
Global screenWidth:Int =800
Global screenHeight:Int =600
Graphics screenWidth,screenHeight',32',60
Global xpos:Double = 0
Global ypos:Double = 30
Global waveLength:Double = 10 ' bigger number smaller length
Global waveHeight:Double = 40.0 ' bigger number taller waves
' move horizontally
While Not KeyHit(KEY_ESCAPE)
Cls
xpos:+1
ypos = Sin(xpos*waveLength)*waveHeight
DrawImage image,xpos,ypos
Flip
Wend
xpos = 100
ypos = 0
' move vertically
While Not KeyHit(KEY_ESCAPE)
Cls
ypos:+1
xpos = Cos(ypos*waveLength)*waveHeight
DrawImage image,xpos,ypos
Flip
Wend
|