This site requires JavaScript, please enable it in your browser!
Greenfoot back
xoiam
xoiam wrote ...

2014/11/4

Wave

xoiam xoiam

2014/11/4

#
Need some help with a program. How can I get an actor to move like a wave(straight from left to right )? please help
danpost danpost

2014/11/4

#
Do you mean like following the pattern of a line produced by a sine wave?
xoiam xoiam

2014/11/4

#
yes
danpost danpost

2014/11/4

#
I came up with the following, where the frequency is around the number of cycles across a 600 pixel wide world:
1
2
3
4
int speed = 1;
double amplitude = 50.0;
double frequency = 3.0;
setLocation(getX()+speed, baseY+(int)(Math.sin((double)(getX()+speed)*frequency/95.5)*amplitude));
The first three lines can be instance fields instead of local variables (or even class constants, if desired).
You need to login to post a reply.