Organic Motion with Sound

My latest project involves creating an object that moves randomly in a natural way using noise, which is working perfectly:

However, when the objects collide and their trajectory changes to a straight line, I want them to return to moving randomly as before. If I use the same function, the object jumps back to its last location before the trajectory change.

thisObject.x = _world.width * (noise(thisObject.t));
thisObject.y = _world.height * (noise(thisObject.t+5));
thisObject.t += 0.001;
let vx = this.acquiredFood[0] - this.x;
let vy = this.acquiredFood[1] -  this.y;

let f = (this.genes.speed + 10) / Math.sqrt(vx*vx+vy*vy);

vx = vx * f;
vy = vy * f;

let newX = this.x + vx;
let newY = this.y + vy;

I am looking for a solution to get the object to move as it did initially from a specific starting position.

Edit: code snippet can be found here:

Answer №1

The issue lies in your approach of using a factor between 0 and 1 generated by noise, along with an incremented seed, to determine position based on direct multiplication with world dimensions. This method results in losing track of current position when approaching food as incrementing the seed doesn't guarantee landing at the exact spot where movement towards food ends (no inverse function exists for noise to derive seed from return value).

A better strategy is to utilize noise for incremental adjustments in coordinates rather than directly multiplying by world dimensions. By doing so, regardless of seed location, ongoing position stability is ensured.

I made several fixes to the code including corrections for syntax errors which I cannot include here due to copyright concerns as the entirety of the code wasn't shared:

MAIN CORRECTION:

  • Introduced variable found within forEach callback to ensure proper exit from findFood function upon locating food. Prevents unnecessary loop iterations and halts further movement once food is found.
  • Applied noise to a value of 4 subtracted by 2 for both x and y coordinates, resulting in a -2 to 2 range for each. Adjustments were also made to seed increments to prevent slow variation.
(pre/css block with modified code)

SYNTAX ERRORS:

(presented corrected syntax errors)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing the conversion of integers to strings when I make a POST request using jquery.ajax to a PHP script

I've been struggling for the past hour trying to solve this issue with no success. Despite reading numerous posts on Stack Overflow related to jQuery and ajax(), I haven't found a solution that addresses my specific problem. Here is an overview ...

What is the best method for iterating through an array and generating a glossary list organized by categories?

I have an array filled with definitions. How can I use Vue.js to iterate through this array and create a glossary list organized by letters? Desired Output: A Aterm: A definition of aterm B Bterm: A definition of bterm C Cterm: A definition of cterm ...

Binding the Ionic v3 page with Ionic framework

I need to bind an ionic input component to an html page from a ts file, but it is only displaying text. Here is my html code: <div class="one" [innerHtml]="htmlToAdd"></div> This is the code in my ts file: constructor(public sanitizer: DomS ...

Tips for accessing <Field> values in redux-form version 7.0.0

class CustomForm extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); } handleClick() { const { Add, noteList } = this.props; Add('this is title value' , 'this is ...

Conceal any errors and warnings from appearing in the console

Many programming languages, such as PHP, provide methods to suppress error and warning messages. Is there a similar approach in JavaScript or jQuery to stop errors and warnings from appearing in the console log? ...

Using regular expressions to extract substrings from URL

I have different URL routes, such as var url = '/product' and '/product/create'. Is there a way to use Regex in order to extract only the route name like 'product'? ...

Angular Swiper Integration: Maintaining Scope Inside Configuration Callback

Trying to grasp the concept of working with callbacks to maintain scope can be a bit tricky, as I've been experiencing. Specifically, I've been struggling with a callback in the Swiper library for Angular, where I am unable to keep my desired sco ...

Using dangerouslySetInnerHTML in React within a Fragment

In my current project, I have a specific requirement where I need to format text in React and also include HTML rendering. Here's an example of what I'm trying to accomplish: import React, {Fragment} from "react"; import {renderToString} from " ...

Alert: It is not possible to update the React state on a component that is already unmounted after updating the context and switching to a different page

I am facing a challenge that requires your assistance In the /editpage, there is a changeProfile function that should be triggered upon clicking const appContext = useContext(AppContext); const [userLogin, setUserLogin] = appContext.userLogin; const [use ...

Guide on securely submitting a form to a Django site from an external source

My Django application needs to accept POST requests from another website using HTML and JavaScript. For example, I have mydjangosite.com and smallhtmlsite.com. What I'd like to achieve is: When a user visits smallhtmlsite.com and fills out a form, th ...

Remove the input form from the angular app in WordPress after reloading or submitting

Presently, I am encountering an issue with my form: <table class="widefat" ng-app="myApp" ng-controller="MyCtrl"> <tr> <td><label for="name_pokemon"><?php _e( "Name", ...

Guide on deleting an item from an object array in AngularJS

I have an array of objects and need to delete a specific object from it: var objectArray = [{"id":"John Doe","label":"John Doe","shape":"image","image":"app/data/img/user_icon.png","color":{"background":"#db630d","border":"#7c3400"},"level":0},{"id":"Java ...

Graphic selectors: a new take on radio buttons

I've been attempting to make this work, but it's not functioning correctly. Below is the CSS code: .input_hidden { position: absolute; left: -9999px; } .selected { background-color: #000000; } #carte label { display: inline-bl ...

What is the process for transferring a Python variable to JavaScript when the Python code is a cgi script?

I am currently working on a JavaScript file where I am attempting to assign a variable based on data received from a Python CGI script. My approach involves using the Ajax GET method to retrieve the data and set the variable within that method. Below is a ...

A step-by-step guide on sending a fetch request to TinyURL

I have been attempting to send a post request using fetch to tinyURL in order to shorten a URL that is generated on my website. The following code shows how I am currently writing the script, however, it seems like it's not returning the shortened URL ...

VueJS / v-html is displaying a blank page (Bokeh-html)

Especially as a newcomer to VueJS, I am currently in the process of trying to showcase a local HTML file within the Vue Application. The method I'm using to fetch the HTML file involves Axios, here's how it goes: <template> <div> ...

React JS: The final input is not updated in the state until after all other

Apologies if I struggle to articulate my issue effectively as English is not my primary language. I have developed a form component (coded in ES6) similar to the following: class Form extends React.Component { constructor(...args) { super(args); ...

Ensure that the browsing history is cleared whenever a user logs out of the AngularJS application

I'm a newbie when it comes to AngularJS, currently working on a web-based project using this framework. One issue I've encountered is that when a user logs out of the application or clicks the browser back button, the application goes to the pre ...

When setting up columns in a MUI DataGrid, it's important to remember that each field must have a unique name to avoid any conflicts. Having

I am currently working on a DataGrid project where I aim to display the values of ready_by and name. Here is an image for reference: https://i.stack.imgur.com/3qZGa.png In my code configuration, the setup looks like this: (specifically focusing on the la ...

Analyze Javascript code and monitor every variable alongside their corresponding values

After watching Bret Victor's engaging talk "Inventing on Principle" the other night, I was inspired to create a real-time JavaScript editor similar to the one he showcased. You can see a glimpse of it in action at 18:05 when he demonstrates binary sea ...