-
[RESOLVED] how to calculate x & y in a sine wave?
Hello,
I have the following initial code:
Code:
double objectAngle = -90.0f;
double objectSpeed = 3.0f; // meters per second
double objectDistance = 300.0f;
double objectTime = 0.0f;
double objectHeading = 90.0f;
double elapsedTimePerCycle = 1.0f;
My coordinate system looks like this:
Code:
90 y
|
|
|
+-180 -------------------- 0 x
|
|
|
-90
I want to calculate a new x,y such that it follows a sine wave like pattern until it gets to point (0,0). I want the width of the arc to go out to about -45 degrees, and the same on the other side to about -135 degrees.
How can I compute these new x,y coordinates in C++?
Thanks!
Alex
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
aseminov
I want to calculate a new x,y such that it follows a sine wave like pattern until it gets to point (0,0). I want the width of the arc to go out to about -45 degrees, and the same on the other side to about -135 degrees.
I don't understand.
You want the formula that gives you the function you describe? The function you describe is y = -sin(x), so just do that.
sin can be found in <math.h> or <cmath>. Just remember that all C trig functions operate on radians.
-
Re: how to calculate x & y in a sine wave?
Yes, but doesn't distance and play a key in the equation? Do I decrement the distance each calculation? How can I extend the x coordinate so it's around + or - 45 degrees from the y axis?
Can you provide some simple example C++ code so I can learn how this works?
Thanks,
Alex
-
Re: how to calculate x & y in a sine wave?
You posted code, but what you are trying to do is not very clear.
If I understood correctly, you are trying to find the equation of a plot?
This plot describes the motion of an object?
And you want this object to cross the (0,0) coordinate at a given angle?
Please try to give a bit more background to what you are trying to do, and we'll have an easier time with the how.
-
Re: how to calculate x & y in a sine wave?
Hello Monarch_Dodra,
I am trying to calculate the x and y position of the object ever second. Sorry if I was not clear on that. I would also like to calculate the new distance based on the speed. I want the object to move toward point (0,0) in a sine wave pattern.
Thanks,
Alex
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
aseminov
Hello,
I have the following initial code:
Code:
double objectAngle = -90.0f;
double objectSpeed = 3.0f; // meters per second
double objectDistance = 300.0f;
double objectTime = 0.0f;
double objectHeading = 90.0f;
double elapsedTimePerCycle = 1.0f;
My coordinate system looks like this:
Code:
90 y
|
|
|
+-180 -------------------- 0 x
|
|
|
-90
I want to calculate a new x,y such that it follows a sine wave like pattern until it gets to point (0,0). I want the width of the arc to go out to about -45 degrees, and the same on the other side to about -135 degrees.
How can I compute these new x,y coordinates in C++?
Thanks!
Alex
Alex, what you are asking does not match the data you are supplying.
For example, the following data
Code:
double objectAngle = -90.0f;
double objectSpeed = 3.0f; // meters per second
double objectDistance = 300.0f;
double objectTime = 0.0f;
double objectHeading = 90.0f;
double elapsedTimePerCycle = 1.0f;
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.
Therefore the data suggests that the points that you should plot are going to be
Code:
x = 0, y = -300
x = 0, y = -297
x = 0, y = -294
...
x = 0, y = 0
x = 0, y = 3
x = 0, y = 6
...
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 behaviour. 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?
-
Re: how to calculate x & y in a sine wave?
Hello PredicateNormative,
I just want to be able to generate the x/y pairs in a sin wave pattern.
-
Re: how to calculate x & y in a sine wave?
Does that mean that your data can be modified? e.g:
Code:
double objectAngle = -90.0f;
double objectSpeed = 30.0f; // meters per second
double objectDistance = 0.0f;
double objectTime = 0.0f;
double objectHeading = 90.0f;
double elapsedTimePerCycle = 1.0f;
double accelerationDueToGravity = 3.0; //meters per second squared
double centreOfGravityX= 0.0;
double centerOfGravityY = 0.0;
The above modifications would cause the object to oscillate around point (0,0) in a sine wave pattern.
-
Re: how to calculate x & y in a sine wave?
Yes, but, you must understand that "I just want to be able to generate the x/y pairs in a sin wave pattern" doesn't clearly describe what you want to do.
The best guess I can deduce from what you've posted thus far is that you want to construct a parametric curve that will increase or decrease it's distance from a fixed circle based on the angle between the radius and the x-axis, ranging in (-180, +180)?
This would produce a flower-like pattern.
Am I right?
P.S. Please don't double post.
P.P.S. As I said there - a simple sketch of the end result would help us understand; you can attach it here, or link to some online image repository.
-
1 Attachment(s)
Re: how to calculate x & y in a sine wave?
ok attached is the drawing of what I want to do.
Thanks!
-
Re: how to calculate x & y in a sine wave?
Ok.
Take a look at the sine function graph:
http://image.tutorvista.com/contenti...ges/img668.gif
It has a period of 2Pi, (0-360 deg), so this just repeats left and right.
In your case, the easiest thing to do is to think of your curve being on a transformed coordinate system, and calculating x=f(y), instead of y=f(x).
By this, I don't mean an inverse function, because the curve you described is not a function (by definition, for each x, f(x) must have no more than one value in order to qualify as a function - that's why arcsin has a limited codomain.)
I'm saying to think of y as input, and x as output.
I rotated your sketch:
http://farm8.staticflickr.com/7179/7...be8e1aeebc.jpg
Note that the x-axis is oriented downwards. This means this is basically a sligtly modified x= -sin(y) function.
If you count, there are 2.5 periods from y1 to y2.
So you need to map [-2.5Pi, 0] range to the [y1, y2] range.
EDIT: by using the two point form for a linear map:
t = y*(y2-y1)/(2.5Pi) + y2 // ERROR - confused the axes and corresponding points...
EDIT1: Sry, made a mistake:
http://farm9.staticflickr.com/8168/6...fcf0564b1e.jpg
t = y * 2.5Pi/(-y1)
EDIT2: Sry, made a mistake, again - I said 2.5 periods, and the period is 2Pi, so:
t = y * (2.5 * 2 * Pi) / (-y1)
Then you find x = -sin(t).
The sine function values range from -1 to 1, so to get the "peaks" at x2 and "valleys" at x1, you symply need to multiply the result to x2 (remember, it's upside-down).
-
Re: how to calculate x & y in a sine wave?
ok I will take a look at this.. do you have any example code for each second? like:
new_x = ?
new_y = ?
Thanks!
Alex
-
Re: how to calculate x & y in a sine wave?
After re-reading your posts, it occurred to me that this better corresponds to what you're looking for, right?
http://farm8.staticflickr.com/7114/6...db1aef048d.jpg
If so, the principle is the same, you only need to scale the [-1,1] range returned by the sine function to the [-45, 45] range (just multiply by 45), and move the graph along the x-axis by subtracting -90 from the result.
Remember, the way I set it up, is that you're calculating x based on y.
So, x = f(y).
If you want to update per time step, then you can make your be a function of time t (note: this t (italic&blue) is not the same as the t (regular) in my previous post).
So, you now have:
- x = f(y), which decides how far to the side the object will go, and
- y = g(t), which tells where along the y-axis the object is;
Assuming that the y-speed is constant, you would calculate y like this:
y = y1 + v*t,
where v is y-speed, y1 is the starting position, and t is the time elapsed since the last step
Then you would calculate x, scaling the result by 45, and moving it along the x-axis by -90:
x = -sin( y * 2.5 * 2*Pi / -y1 ) * 45 - 90,
where y1 is your "start" (a negative value, see graph).
Note that this approach requires you to calculate y first.
P.S. With this setup, the object traveling at v of 1 unit per second will reach y = 0 when t reaches -y1.
You can modify this behavior by modifying the equation for y, or the value of v.
-
Re: how to calculate x & y in a sine wave?
Yes that is exactly what I want!
What is the 2.5 part in the calculation of x?
Thanks so much for your help!
Alex
-
Re: how to calculate x & y in a sine wave?
In my older post (the one where I rotated the image) I said that if you count the number of periods for the sine function you sketched, you get 2.5.
Sin(x) is a periodic function - the same values repeat for every 360 degs (or 2*Pi radians).
This is one period:
http://forums.codeguru.com/images/ie.../2012/04/1.gif
In your case, that image repeats itself 2.5 times - so that's what's 2.5 for.
-
Re: how to calculate x & y in a sine wave?
ok thanks so much. I will give you positive feedback!
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
PredicateNormative
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.
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
TheGreatCthulhu
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.
-
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.
-
Re: [RESOLVED] how to calculate x & y in a sine wave?
Quote:
Originally Posted by
aseminov
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).
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
TheGreatCthulhu
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.
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
superbonzo
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 :D).
Thanks.
-
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.
-
Re: how to calculate x & y in a sine wave?
Quote:
Originally Posted by
TheGreatCthulhu
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?
-
Re: [RESOLVED] how to calculate x & y in a sine wave?
Quote:
Originally Posted by
TheGreatCthulhu
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.
-
Re: [RESOLVED] how to calculate x & y in a sine wave?
Quote:
Originally Posted by
nuzzle
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.
-
Re: [RESOLVED] how to calculate x & y in a sine wave?
Quote:
Originally Posted by
TheGreatCthulhu
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.