Orbiting center point image maths question
Monkey Forums/Monkey Programming/Orbiting center point image maths question
| ||
| I found some AS code and trying to adapt it to monkey. My aim is to simply have an image rotate around a center point, like it's orbiting a planet. I have tried a few things and at the moment my image simply stays put. Here is the code:
Class GameScreen Extends Screen
Field imageGraphic:GameImage
Field xCenter:Float = SCREEN_WIDTH2
Field yCenter:Float = SCREEN_HEIGHT2
Field range:Float = 100
Field angle:Float = 20
Field speed:Float = 1.5
Field imagex:Float
Field imagey:Float
Method New()
name = "Game"
End
Method Start:Void()
imageGraphic = game.images.Find("image")
End
Method Render:Void()
Cls
imageGraphic.Draw(imagex,imagey)
End
Method Update:Void()
imagex = xCenter + Cos(angle*PI/180) * range;
imagey = yCenter + Sin(angle*PI/180) * range;
angle+=speed;
End
End
Can anyone spot anything wrong with this 'orbit code'. It's probably me making a simple error or maths issue. Or if im trying to over complicate the problem let me know. Thanks for any help. |
| ||
| Where are imagex and imagey defined? Perhaps they're meant to be something like imageGraphic.x (or imageGraphic.imagex)? |
| ||
| Have you tried increasing the range? It seems to be only one pixel. Perhaps it is orbiting by that much and you haven't noticed! |
| ||
| Sorry guys had some typos in the initial post. imagex and imagey now defined at the top. Tried increasing the range this is also reflected in my original post. The graphic starts at a different position now but still no oribiting. Strange one. Is there any debug that could help? |
| ||
| I did it, speed was to low! increased it to 20 and its slowly orbiting. Thanks for your suggestions as alwalys sharing helps! |
| ||
| Get rid of PI/180 thats for radians and Monkey is doing Degrees already. |
| ||
| Yeah! As jesse states there's no need for PI/180. Just have 'angle' and increase its value. When over 359.0 deduct 360 from it. |
| ||
| Will do, thanks all. |
| ||
| Tried changing it to angle and I now have two graphics appearing opposite each other spinning around the center. I presume the imagex/y is being calculated and creating two different positions which is flicking through (its not actually two images) Does that make sense? Do I have to separate angles for x and y? (tried that same thing) New code:
Method Render:Void()
Cls
imageGraphic.Draw(imagex,imagey)
End
Method Update:Void()
imagex = xCenter + Cos(angle) * range;
imagey = yCenter + Sin(angle) * range;
angle+=speed;
if (angle > 359.0) angle -= 360
End
|
| ||
| try reducing the speed variable to something smaller. |
| ||
| Might be worth putting it into a little runnable example and posting that. |
| ||
Something for you to play around with so you can see what happens with different values. You'll have to supply your own images for the sun and earth.Strict
Import mojo
Function Main:Int()
New CGame
Return 0
End Function
Class CGame Extends App
Field orbitAngle:Float 'Objects current orbits angle
Field orbitX:Int 'Object thats orbiting positions
Field orbitY:Int 'Object thats orbiting positions
Field orbitSpeed:Float 'Objects current orbit speed
Field orbitDiameter:Float 'Objects orbits diameter
Field orbitImage:Image 'Orbiting objects image
Field poleX:Int 'Central X axis
Field poleY:Int 'Central y axis
Field poleImage:Image 'Central image
Method OnCreate:Int()
SetUpdateRate(30)
Self.orbitImage = LoadImage("earth.png", 1, Image.MidHandle)
Self.poleImage = LoadImage("sun.png", 1, Image.MidHandle)
Self.poleX = DeviceWidth() / 2
Self.poleY = DeviceHeight() / 2
Self.orbitDiameter = 100.0
Return 0
End Method
Method OnUpdate:Int()
Self.orbitX = Self.poleX + Cos(Self.orbitAngle) * Self.orbitDiameter
Self.orbitY = Self.poleY + Sin(Self.orbitAngle) * Self.orbitDiameter
Self.orbitAngle += orbitSpeed
If Self.orbitAngle > 360 Then Self.orbitAngle -= 360
If Self.orbitAngle < 0 Then Self.orbitAngle += 360
If KeyDown(KEY_A) Then Self.orbitSpeed -= 0.1
If KeyDown(KEY_Q) Then Self.orbitSpeed += 0.1
If KeyDown(KEY_W) Then Self.orbitDiameter += 1
If KeyDown(KEY_S) And Self.orbitDiameter > 0 Then Self.orbitDiameter -= 1
If KeyDown(KEY_LEFT) Then Self.poleX -= 4
If KeyDown(KEY_RIGHT) Then Self.poleX += 4
If KeyDown(KEY_UP) Then Self.poleY -= 4
If KeyDown(KEY_DOWN) Then Self.poleY += 4
Return 0
End Method
Method OnRender:Int()
Cls
DrawImage(Self.poleImage, Self.poleX, Self.poleY)
DrawImage(Self.orbitImage, Self.orbitX, Self.orbitY)
DrawText("Q and A Keys to Change Orbit speed", 0, 0)
DrawText("W and S Keys to Change Orbit diameter", 0, 12)
DrawText("Arrows Keys to move Orbit pole", 0, 24)
DrawText("OrbitAngle: " + Self.orbitAngle, 0, 36)
DrawText("OrbitSpeed: " + Self.orbitSpeed, 0, 48)
DrawText("OrbitDiameter: " + Self.orbitDiameter, 0, 60)
Return 0
End Method
End Class
|
| ||
| Thanks for the brilliant help (again) - as mentioned the speed I was running the orbiting at was way too fast. |
| ||
| If you prefer radians, there are also radian versions of all the trig functions, e.g. Cosr(), ACosr() etc. They may also be a little faster if you are doing heavy trig. |
| ||
| Here is the result of me asking about the orbiting thing. As you can see nothing complex! DeathSkip - http://panicbarn.com/deathskip/deathskip.html |
| ||
| LOL, funny game :-D Great example of a really simple game that is also fun. |
| ||
| That was cool! :) |