Random movement

hey folks,

I would like to code a game in which you see the ocean from top view (so far no problems) . On the ocean there should be a boat (imported png) that changes direction inside the window randomly. Which function would you recommend? ( noise, brownian or random?)

Another problem: when the boat changes the direction the peak (bow) of the boat should turn towards the direction of the movement. Otherwise it would look very ridiculous. is there any solution for that ?

Thanks for your help! :slight_smile:

for turning the boat you should use translate() and rotate() to turn it. For changing the direction it is pointing I recommend you don’t set it to anything you just set float turnSpeed = noise(frameCount); dir += turnSpeed; turnSpeed *= 0.9. I have no idea about noise function but from what I remember it returns organic random values. It should be better than the plain random function

I would suggest that you use a constant speed and you create your randomness through the angle. This way the angle defines the boat’s direction (through rotate as mentioned above) and you can decide how far the boat can turn in each move by limiting the range of the angle the random number is created in, so that your boat’s movements don’t look too unboatlike.
In the pictures you can see, how the boat would move for the three cases:
Blue: random(1);
Green: noise(x coordinate);
Red: randomGaussian(); which is Brownian motion

For one pictures I limited the range of angle was -90 to 90 (lower picture) for the other -45 to 45 (upper picture).
What you can see, Brownian motion will let your boat just circle around the same spot, the random number generation, gives you good results in more restricted cases (for -45 to 45) and makes it look realistic, however with loose restrictions, the boats movements would become more erratic/random. The noise function gives you better results with loose restrictions (-90 to 90), however it gives you odd results, when it is restricted to much, because then you get repetitions, because the input is more similar.

Hope that makes sense.

i ll try that out! thank you :slight_smile:

//ship
float shipX = 400;
float shipY = 400;

float shipWidth = 60;
float shipHeight = 20;
//ship speed
float shipVX = 50;
float shipVY = 25;

void setup () {
size (800, 800);
}

void draw () {

background(125);

drawShip();
shipDrive();
}

//function that moves the ship
void shipDrive () {

shipX = shipX+shipVX/frameRate;

shipY = shipY+shipVY/frameRate;

}

//function that displays the ship
void drawShip () {

rect (shipX, shipY, shipWidth, shipHeight);

}

this is my code so far. I am a bloody beginner so please have mercy on me.
i set the constant speed but where can I set the function for the random angle of the boat ( rectangle) and the noise() for the movement ? the green one with noise looks very good.

shapes are annoying to rotate. I suggest you either use 4 lines serving as borders or just replace it with a line with high strokeWeight();

To move in a certain direction do something like this:

float dir = 0; //direction in radians
float speed = 10; //move for 10 pixels
x += cos(dir)*speed;
y += cos(dir)*speed

That is the function for rotation.

It might be easier to use rotate, when you use rectMode(CENTER). This way the coordinate of the rectangle is in the center of the rectangle and not on the left corner.

CodeMasterX’s code you can use to include the angle to speed calculation.
All you need then is to get new dir variables by using noise.