Edit 2 - clarification
Just to clarify, the issue I have at the moment is how I specify a time (in seconds) for a complete revolution to occur. My delta is fixed and is 1/ticks per second (1/60). As you can see with my linear sprite movement, I have a 'time' variable which is where I specify (in seconds) how long the sprite should take to travel the length (or height) of the screen. I'm looking at how I do this for circular movement.
Edit 1 - Results from suggestions
See at end of question.
When moving my sprites along a straight path, I'm using delta so I can get the same rate of relative motion on all devices like so:
spriteGridX = 240/480; //I start out with a virtual 'Grid' so I can scale position to all resolutions etc so 240/480 would place the sprite at the center of the screen (X) spriteScreenX = spriteGridX * width; //Scale to physical screen where width is the width of the current devices screen (viewport) spriteXTime = 8f; //8 being the amount of time in seconds that this sprite will take to traverse the entire width of the screen spriteXVel = 1/spriteXTime; //Velocity that will be used to move the sprite along the virtual grid position
So, when I need to move the sprite, I simply do the following:
spriteXGrid+= (spriteXVel * Delta) //+= for right -=for left. I'm using a fixed delta time spriteScreenX = spriteGridX * width; //And again, convert to usable screen position for current device drawsprite(spriteScreenX, SpriteScreenY); //Draw the sprite (drawSprite is just a method I've written to draw openGL quads and it takes the screen X and Y as coordinates - spriteScreenY not shown here but essentially same as X with height instead of width.
Now, I need to have a sprite move along a circular path. So far, I have a test up and running which directly acts on the 'screen' coordinates of the sprite directly like so:
int rad = 25; float angle = 50; spriteScreenX-=Math.round(rad*Math.cos(angle)); spriteScreenY+=Math.round(rad*Math.sin(angle)); angle+=.1;
Now, the above does work, but the sprite completes it's circular path way too quickly (it's doing approximately 1 revolution per second).
So my question is:
- Am I doing this correctly and if so..........
- How do I slow this down? I won't particularly need to keep changing the speed, I just want to be able to set it initially.
Would really appreciate some pointers.
When I put the following the code above I get the following result:
If I put the following:
spriteScreenX-=Math.round(rad*Math.cos(angle*Math.PI/180*delta)); spriteScreenY+=Math.round(rad*Math.sin(angle*Math.PI/180*delta)); angle+=.1; Or: spriteScreenX-=Math.round(rad*Math.cos(angle*delta)); spriteScreenY+=Math.round(rad*Math.sin(angle*delta)); angle+=.1;
Something like this happens: