# Random movement

**URL:** <https://discourse.processing.org/t/random-movement/26464>\
**Category:** Coding Questions\
**Created:** [December 23, 2020, 11:16am UTC](https://discourse.processing.org/t/random-movement/26464 "2020-12-23T11:16:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hullah](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hullah](https://discourse.processing.org/u/hullah)\
**Post date:** [December 23, 2020, 11:16am UTC](https://discourse.processing.org/t/random-movement/26464/1 "2020-12-23T11:16:58Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 23, 2020, 11:56am UTC](https://discourse.processing.org/t/random-movement/26464/2 "2020-12-23T11:56:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 23, 2020, 1:59pm UTC](https://discourse.processing.org/t/random-movement/26464/3 "2020-12-23T13:59:51Z")

</div>

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.

 ![Randomness45](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/55632254f90f6b193d96e97c6003aa7410946de9.png) ![Randomness 90](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c3cdc225e3f4efeeab2440acb2e3da08fc2f3673.png)

---

<div class="post-metadata">

**Author:** ![hullah](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hullah](https://discourse.processing.org/u/hullah)\
**Post date:** [December 23, 2020, 2:24pm UTC](https://discourse.processing.org/t/random-movement/26464/4 "2020-12-23T14:24:25Z")

</div>

i ll try that out! thank you 🙂

---

<div class="post-metadata">

**Author:** ![hullah](https://avatars.discourse-cdn.com/v4/letter/h/ee59a6/32.png) [@hullah](https://discourse.processing.org/u/hullah)\
**Post date:** [December 23, 2020, 2:33pm UTC](https://discourse.processing.org/t/random-movement/26464/5 "2020-12-23T14:33:06Z")

</div>

//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.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 23, 2020, 2:38pm UTC](https://discourse.processing.org/t/random-movement/26464/6 "2020-12-23T14:38:34Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 23, 2020, 4:12pm UTC](https://discourse.processing.org/t/random-movement/26464/7 "2020-12-23T16:12:56Z")

</div>

That is the function for rotation.

> **[rotate() \\ Language (API) \\ Processing 3+](https://processing.org/reference/rotate_.html)**

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.

> **[noise() \\ Language (API) \\ Processing 3+](https://processing.org/reference/noise_.html)**
