random direction, constant speed
in
Programming Questions
•
29 days ago
Hi!
I have a pretty simple code for making an ellipse going in different directions with start in the centre everytime you click the mouse. the direction is controlled by random x and y speed. the problem is that ofcourse also the speed is changing.
but I want to create a ellipse go in different directions every time you click the mouse with constant speed. but cant figure out how to do this without changing the speed.
any suggestion?
I have a pretty simple code for making an ellipse going in different directions with start in the centre everytime you click the mouse. the direction is controlled by random x and y speed. the problem is that ofcourse also the speed is changing.
but I want to create a ellipse go in different directions every time you click the mouse with constant speed. but cant figure out how to do this without changing the speed.
any suggestion?
- Bla trala;
- void setup() {
- size(500, 500);
- background(255);
- trala = new Bla();
- }
- void draw() {
- trala.display();
- trala.move();
- trala.musk();
- }
- class Bla {
- float x = width/2;
- float y = height/2;
- float xspeed = 2;
- float yspeed = 2;
- Bla() {
- }
- void display() {
- fill(random(255), random(255), random(255));
- ellipse(x, y, 10, 10);
- }
- void move() {
- x = x + xspeed;
- y = y + yspeed;
- }
- void musk() {
- if (mousePressed == true) {
- x = width/2;
- y = height/2;
- xspeed = random(-2, 2);
- yspeed = random(-2, 2);
- println(x);
- }
- }
- }
1