CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Jan 2012
    Posts
    49

    Re: how to calculate x & y in a sine wave?

    ok thanks so much. I will give you positive feedback!

  2. #17
    Join Date
    Jan 2010
    Posts
    1,133

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by PredicateNormative View Post
    For example, the following data
    [...]
    suggests that at t=0, the object is lying on the y axis at x=0, y=-300, and that the object is travelling on the y axis in a positive direction at a constant speed of 3m/s.

    [...]

    Newtons first law of motion states that an object will continue at a constant velocity, unless acted on by an external force.

    There are no accelerations mentioned, or anything else in the data that would suggest that the object should oscillate around a point or have wave like behavior. This information is missing from your description and is required in order to help you come up with a solution to your, as yet, unbound problem. Please can you explain why you think that the object should be moving in a sine wave pattern?
    Nice catch, among us all, you were the most careful when reading the OP's posts. Anyway, we managed to establish that the OP wanted to simulate sinusoidal movement along x=-90 deg (see image in one of my posts), without being necessarily physically correct about every detail.

    Now, although the thread has been marked resolved, if you have some suggestions how to model this in terms of forces, I'd love to read them.

  3. #18
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by TheGreatCthulhu View Post
    Now, although the thread has been marked resolved, if you have some suggestions how to model this in terms of forces, I'd love to read them.
    A sine motion is usually the result of an elastic force (a spring): The further you are from the origin, the stronger the force.

    So, if you put a solid on a spring, on a cart, the cart moving along the x axis at constant speed (and no external force), you'd have that motion.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #19
    Join Date
    Jan 2010
    Posts
    1,133

    Re: [RESOLVED] how to calculate x & y in a sine wave?

    It has crossed my mind, but at the first moment I got a bit confused by thinking that a moving source of a spring force would eventually cause the spring force to be at an angle to the x-axis - but, of course, you're right; now that I look at it afresh, it only takes for the object to (1) initially have a non-zero x-velocity component (since it starts at the rest position - but not in the rest state), and (2) have the same y-velocity component as that of the spring force source.

  5. #20
    Join Date
    May 2009
    Posts
    2,413

    Re: [RESOLVED] how to calculate x & y in a sine wave?

    Quote Originally Posted by aseminov View Post
    ok attached is the drawing of what I want to do.
    So you want to fit a sine curve between two points on a line and then you want an object to trace the curve path at constant speed, right?

    First consider an "ordinary" sine curve along the x-axis oscillating in the y-direction. In its most general form it looks like this,

    y = A*sin(B*x + C)

    where A is the amplitude, B is the frequency and C is the phase.

    But for simplicity set A=B=1 and C=0 and get,

    y = sin(x)

    That's the curve the object follows from some point on the x-axis where y=0. (the x-value must be a multiple of PI). The curve is followed in the negative x-direction until x=0 (and y=0).

    If the object is somewhere on the x-axis, say at X, the corresponding y-value is at Y = sin(X). Now say the object moves a small step R away from X,Y along the curve path, what are the new x and y values,

    new_x = ?
    new_y = ?

    Well what's the direction the object must follow from its current position at X,Y to go a length R along the curve? You get an approximation by derivation,

    y = sin(x)

    dy/dx = y´= cos(x)

    So

    dy = cos(x) * dx

    So say the object goes a length R in the dy/dx direction, what's the relation between those? Well according to Pythagoras it's,

    R^2 = dx^2 + dy^2

    Inserting dy gives,

    R^2 = dx^2 + (cos(x) * dx)^2

    and solving for dx gives,

    dx = R / sqrt(1 + cos(x)^2)

    Since the object goes in the negative x-direction you have,

    new_x = X - dx

    and with dx inserted,

    new_x = X - R / sqrt(1 + cos(X)^2)

    To get the new_y it's best to simply use new_x like

    new_y = sin(new_x)

    because this gives you an exact y-value on the sine curve.

    -----

    That's the principle now it's just to adapt it to your problem.

    The A, B and C parameters are used to exactly describe the sine shaped "road" the object should follow. The formulas must be derived anew to include these parameters. Then also the coordinate system must be rotated so the sine "road" gets the wanted "angle". So you must set up a rotations matrix to transform the x and y coordinates to the rotated sine curve. Alternatively you derive the formulas for the rotated coordinate system directly.

    You must also get a grip on the R constant. It's the length the object goes in a certain period of time so it's speed related. But it must be small because it's really a dr (since it's related to dx and dy).
    Last edited by nuzzle; April 25th, 2012 at 06:01 PM.

  6. #21
    Join Date
    Oct 2008
    Posts
    1,456

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by TheGreatCthulhu View Post
    Now, although the thread has been marked resolved, if you have some suggestions how to model this in terms of forces, I'd love to read them.
    alternatively, one could simply use a time dependent sinusoidal force orthogonal to the "main" direction of motion.

  7. #22
    Join Date
    Jan 2010
    Posts
    1,133

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by superbonzo View Post
    alternatively, one could simply use a time dependent sinusoidal force orthogonal to the "main" direction of motion.
    Right - like, say, a side-wind blowing and changing direction with time (in an unusually regular manner ).
    Thanks.

  8. #23
    Join Date
    Jan 2010
    Posts
    1,133

    Re: [RESOLVED] how to calculate x & y in a sine wave?

    @OP:

    Definitely check out nuzzle's post:

    The solution I proposed, assumes that there is a constant component of the object's velocity (y-velocity in your setup), and calculates (x, y) as a function of time. This means that, along the path the object will slow down on curved sections, and accelerate on the approximately straight parts of the path (since they are angled to the y-axis).

    On the other hand, nuzzle's approach is based on the current location of the object, and the distance R the object incrementally travels with each step. If you treat this distance as so small that the motion can be considered linear, then you can, theoretically, make your object move at constant speed along the sinusoidal path.

  9. #24
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to calculate x & y in a sine wave?

    Quote Originally Posted by TheGreatCthulhu View Post
    Nice catch, among us all, you were the most careful when reading the OP's posts. Anyway, we managed to establish that the OP wanted to simulate sinusoidal movement along x=-90 deg (see image in one of my posts), without being necessarily physically correct about every detail.

    Now, although the thread has been marked resolved, if you have some suggestions how to model this in terms of forces, I'd love to read them.
    I've been away from codeguru for a few days, and so missed your post... sorry about that.

    Unfortunately, I don't really have much time at the moment, but if I get the chance in the near future, I'll write you something that will model something reasonable.

    The difficulty with the OP's post is that there are few things that behave in the manner that the OP is requesting. Perhaps looking at a birds-eye view of the path of the weight on a pendulum in a vacuum, mounted on a platform perpendicular to the direction of travel of the platform (the platform traveling at 3m/s). But then more questions appear... What frequency should the pendulum have? What amplitude should the pendulum have?

    I guess my real question to the OP is what is the reason for the oscillation that he wants to model?

  10. #25
    Join Date
    May 2009
    Posts
    2,413

    Re: [RESOLVED] how to calculate x & y in a sine wave?

    Quote Originally Posted by TheGreatCthulhu View Post
    On the other hand, nuzzle's approach
    One can ask - why the sine curve?

    Is it a path the object follows of its own free will very much like a car travelling along a road, or is it a trajectory the object follows because it's subject to some kind of external force field?

    I assumed the former and took a differential geometry approach. At any point (x,y) the object has access to a direction vector (dx, dy) as it travels and if it adjusts its direction often enougth it will follow the prescribed sine shaped path from start to goal.

    In fact it's possible to know how often it must adjust directions. It must do that at least twice for each period of the sine curve it follows. The Sampling Theorem states that. But the more often it adjusts the closer it will follow a perfect sine of course.
    Last edited by nuzzle; April 27th, 2012 at 03:33 AM.

  11. #26
    Join Date
    Jan 2010
    Posts
    1,133

    Re: [RESOLVED] how to calculate x & y in a sine wave?

    Quote Originally Posted by nuzzle View Post
    One can ask - why the sine curve?

    Is it a path the object follows of its own free will very much like a car travelling along a road [...]
    I initially assumed something along those lines too, however, given this thread, and one another by the OP, I'm guessing that the OP is just experimenting with programming and trigonometry.

  12. #27
    Join Date
    May 2009
    Posts
    2,413

    Re: [RESOLVED] how to calculate x & y in a sine wave?

    Quote Originally Posted by TheGreatCthulhu View Post
    I initially assumed something along those lines too, however, given this thread, and one another by the OP, I'm guessing that the OP is just experimenting with programming and trigonometry.
    Sure but regardless of the OP it's an important distinction. Most seem to want to consider the sinusoidal movement of the object to be caused by some external force but the object may as well simply be following a path that happens to have that shape.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured