Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sine wave movement  (Read 3698 times)

0 Members and 1 Guest are viewing this topic.

vicer1234

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Sine wave movement
« on: June 07, 2011, 08:54:07 pm »
Hi,

           I want to move the sprite in a sine wave pattern. For that i am trying
is for example if the screen resolution is (800,600), i am taking :

X = 800 and then generate Y = Sin(X) and gradually decreasing the X--;

For the movement i am  doing

Code: [Select]

Sprite->Move(-X * GetFrameTime(), Y);


But the problem is that the movement is not smooth and the sprite flickers while moving.
How can i get a smooth sine wave movement with high steep along Y axis as for the present values the upward movement is very small.
I tried to scale the Y value with some factor like 10.0, but still its very jerky movement.

All suggestions are welcome

David

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Sine wave movement
« Reply #1 on: June 07, 2011, 09:13:21 pm »
Quote from: "vicer1234"
Hi,

           I want to move the sprite in a sine wave pattern. For that i am trying
is for example if the screen resolution is (800,600), i am taking :

X = 800 and then generate Y = Sin(X) and gradually decreasing the X--;

For the movement i am  doing

Code: [Select]

Sprite->Move(-X * GetFrameTime(), Y);


But the problem is that the movement is not smooth and the sprite flickers while moving.
How can i get a smooth sine wave movement with high steep along Y axis as for the present values the upward movement is very small.
I tried to scale the Y value with some factor like 10.0, but still its very jerky movement.

All suggestions are welcome


Did you float the variables?

Try putting X -= 1.f instead of X--. Same goes for Y.

Also, you didn't limit the Y variable to the fps

It's best if you show the minimum code

The steepness of the wave requires some basic trig knowledge, and I'm sure you'll catch on pretty quickly. You'll have to put a number in front of the Sin(X) or multiply them together if that doesn't work

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Sine wave movement
« Reply #2 on: June 09, 2011, 02:51:44 pm »
I think you probably need to put your delta time IN the sine calc. i.e. sin(y * dt).

However you'll probably find that it produces rather unpredictable results with a variable timestep -- try limiting your framerate and using a fixed update time. (Or implementing a "proper" fixed timestep game loop)

Kian

  • Newbie
  • *
  • Posts: 44
    • View Profile
Sine wave movement
« Reply #3 on: June 14, 2011, 08:01:48 pm »
Couple of things:

Are you passing x to the sine function as degrees, or radians? What does the function expect? If the function works with radians, and you are passing the x coordinate in the range 0-800, the sprite will advance about 120 waves (800 / 2pi) in the course of traversing the screen. This will lead to jerkiness as you grab it at different heights.

If it works with degrees, it should result in a more smooth result, as you'd only go through (800 / 360) 2 waves instead of 120.

Also, careful when multiplying by times; SFML uses ms, meaning at 60 frames per second you're multiplying by 16 (lower framerates provide a bigger multiplier).

Further, the result of sin(x) is going to be between -1 and 1. You'll need a scaling factor for this to even be evident (otherwise it's moving at most 1 pixel up or down). Multiplying by 10 only gets you up to a 20 pixel amplitude in the movement.

If you provide a more detailed sample we can help more.

 

anything