Sign in to follow this  
gareth_mcdaid

Sine Waves Travel At Any Angle Through 2D Space

Recommended Posts

gareth_mcdaid    157

The sine wave generator is using a time, elapsed_time in milliseconds, or a time stamp milliseconds, to get the position of the sine wave.

The problem is I need to make the sine waves travel at any angle through 2D space, any help?

The sine wave generator is part of a separate projectile system, 10 is the maximum frequency, elapsed_time is required because this is to be used in a client and server game, where timing is crucial.

 

2ptncs5.jpg

Create event

set=0;
lifetime=3000
frequency=1
amplitude=50*1.0
pixels[0,0]=0
pixel_num=0
xx=0

Draw event

if set=0
{
start_time=current_time
set=1
}

elapsed_time =current_time-start_time

if pixel_num<60*(lifetime/1000)
{
pixels[pixel_num,1]=(sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) 
}

xx=0
pad=1

draw_set_color(c_red)
for(i=0;i<pixel_num;i+=1)
{
  
  if i>0
  {
    draw_line(xx+100,100+pixels[i-1,1],xx+100+pad, 100+pixels[i,1]);
  }
  xx+=pad;
  
}

if pixel_num<60*(lifetime/1000)
{
pixel_num+=1
}

The projectiles move independent of the frame rate, using the elapsed time, the elapsed time is used to position the projectiles, it’s required to keep the server and client synchronized at all times.

 

Distance is calculated by using constants and the elapsed time, the constants are based of the server tick rate and the target fps, and there is a speed constant for each projectile used to calculate the number of pixels to move the projectile per millisecond of time passed

 

The below code as the desired effect but for only for four directions, I need 360 degrees of movement for this to be of use in my projectile

system..

angle=0
x=startx + (cos( degtorad(angle) )*distance)
y=starty - (sin( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )

anlge=90
x=startx + (cos( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )
y=starty - (sin( degtorad(angle) )*distance)

angle=180
x=startx + (cos( degtorad(angle) )*distance)
y=starty - (sin( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )

angle=270
x=startx + (cos( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )
y=starty - (sin( degtorad(angle) )*distance)
Edited by gareth_mcdaid

Share this post


Link to post
Share on other sites
HappyCoder    5052
You can rotate points in 2D using the following matrix.
 
| cos(a)  sin(a)|
|-sin(a)  cos(a)|
 
By multiplying the matrix with a point you get the rotated point.
 
| cos(a)  sin(a)| * |x| = |x *  cos(a) + y * sin(a)|
|-sin(a)  cos(a)|   |y|   |x * -sin(a) + y * cos(a)|
'a' is the angle you wish to rotate by.
 
In your case
x = time * speed
y = amplitude * sin(10 * frequency * time / lifetime)
 
Plug x and y into the equation above and you should get an equation for calculating the point along the wave. Then take the result of that and add it to (startx, starty). Another method would to to calculate x,y unrotated then rotate the point using the matrix above. After you rotate you add the (startx, starty). I would recommend the second method. Using matrices like that gives you a lot of flexibility over the transformation of points.

Share this post


Link to post
Share on other sites
jjd    2140

 

The sine wave generator is using a time, elapsed_time in milliseconds, or a time stamp milliseconds, to get the position of the sine wave.

The problem is I need to make the sine waves travel at any angle through 2D space, any help?

The sine wave generator is part of a separate projectile system, 10 is the maximum frequency, elapsed_time is required because this is to be used in a client and server game, where timing is crucial.

 

2ptncs5.jpg

Create event

set=0;
lifetime=3000
frequency=1
amplitude=50*1.0
pixels[0,0]=0
pixel_num=0
xx=0

Draw event

if set=0
{
start_time=current_time
set=1
}

elapsed_time =current_time-start_time

if pixel_num<60*(lifetime/1000)
{
pixels[pixel_num,1]=(sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) 
}

xx=0
pad=1

draw_set_color(c_red)
for(i=0;i<pixel_num;i+=1)
{
  
  if i>0
  {
    draw_line(xx+100,100+pixels[i-1,1],xx+100+pad, 100+pixels[i,1]);
  }
  xx+=pad;
  
}

if pixel_num<60*(lifetime/1000)
{
pixel_num+=1
}

The projectiles move independent of the frame rate, using the elapsed time, the elapsed time is used to position the projectiles, it’s required to keep the server and client synchronized at all times.

 

Distance is calculated by using constants and the elapsed time, the constants are based of the server tick rate and the target fps, and there is a speed constant for each projectile used to calculate the number of pixels to move the projectile per millisecond of time passed

 

The below code as the desired effect but for only for four directions, I need 360 degrees of movement for this to be of use in my projectile

system..

angle=0
x=startx + (cos( degtorad(angle) )*distance)
y=starty - (sin( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )

anlge=90
x=startx + (cos( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )
y=starty - (sin( degtorad(angle) )*distance)

angle=180
x=startx + (cos( degtorad(angle) )*distance)
y=starty - (sin( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )

angle=270
x=startx + (cos( degtorad(angle) )*distance)- ((sin( (elapsed_time/lifetime)*(10*frequency) )*amplitude) )
y=starty - (sin( degtorad(angle) )*distance)

 

I think what you are looking for is called a wave vector

 

-Josh

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this