How can we implement programming logic for a Threejs Particle System that includes connecting lines between particles

I recently inquired about connecting nearby particles in ThreeJS and managed to create lines between them. However, due to the logic of the particle system, the lines are drawn twice. This behavior is similar to how the original 2D particle system functions: Link

This redundancy occurs because a line is drawn for each pair of particles connected. This could impact performance negatively. How can I ensure that each line is drawn only once for every pair of connecting points?

For reference, here is the desired effect I aim to achieve but struggle with the underlying code logic: Link

I have shared my progress via this jsfiddle.


// JavaScript code present
// Omitted for brevity 

Although I am close to achieving the desired result, I'm concerned about optimization issues. There seems to be flickering lines and instances where a line remains stationary. My current approach involves updating line geometry vertices to match relevant particle points.

Regarding line management, I assign a unique ID by using the indexes of the two points, such as '8-2' for instance.

Answer №1

The challenge you're facing is that as you loop through your list of points, you are encountering duplicate matches.

For instance:

Imagine a set of points: [X, Y, Z]. Each point is tested against all others for proximity. Let's say that only points X and Z meet the criteria of being nearby.

When comparing each point, if X to Z warrants a line on the first pass, but then when evaluating Z, you also discover that X qualifies, resulting in an unnecessary duplicate line.

Solving the issue:

The fix is straightforward: Do not revisit nodes that have already been assessed. This is because the distance between X and Z is identical to Z and X.

To address this, adjust the indexing method within your checking loop:

// (Please note: This is illustrative code and may require modification.)
for(var check = 0, checkLength = nodes.length; check < checkLength; ++check){
    for(var against = check + 1, against < checkLength; ++against){
        if(nodes[check].distanceTo(nodes[against]) < delta){
            buildThatLine(nodes[check], nodes[against]);
        }
    }
}

In the nested loop, the indexing strategy ensures:

  1. Omitting the current node
  2. Skipping nodes preceding the current one

This is achieved by setting the inner index to the outer index + 1.

Note:

This approach assumes that lines are removed entirely in each frame. While effective, it might not be the most efficient method. Enhancing efficiency is a task you can explore further.

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

obtain hyperlinks on a website

Is it possible to retrieve links from a web page without actually loading it? Essentially, I would like to allow a user to input a URL and then extract all the available links within that webpage. Do you know of any method for accomplishing this task? ...

``There seems to be an issue with JQuery.Ajax not properly displaying on Samsung Smart

I attempted to use JQuery.Ajax to interact with my webservice Below is the code snippet: Main.onLoad = function() { // Enable key event processing this.enableKeys(); widgetAPI.sendReadyEvent(); //$("#h2Test").html("Change On Text"); ...

Having trouble getting jQuery scrollTop to function with an "else if" statement?

Looking to dynamically add or remove classes from a div based on scrolling position using jQuery. Defined two variables: var mainHeight = $('#main-image').height()/1.10; var footHeight = $('body').height() - $('footer').heig ...

Retrieve data from HTML Form arrays using JavaScript

I have a situation in my forms where arrays are being sent back in the following format: <input class="checkbox-service" name="services['electricity']" type="checkbox"> <input class="checkbox-service" name="services['water'] ...

The error message "Property navigate is undefined" appears when trying to open a full-screen modal in React Native navigation

Having an issue with rendering a full-screen modal in React Native when clicking the 'Account' button in the header. I keep getting this error: Cannot read property navigate of undefined Can anyone point out where my mistake might be? Here is ...

Performing an Ajax MySQL query utilizing the text from a link as a reference, across a page

I have a page with several links. When a user clicks on a link, the text from the link is used in the WHERE clause of a MySQL query, and the result is displayed on the page using ajax. I'm trying to run different queries based on multiple ids or clas ...

Executing PHP script within a Node.js socket server program

I have a JavaScript file running a normal TCP socket using node.js. Before sending a response back to the client, I need to validate the data using some PHP (MySQL) code. Can someone guide me on executing this PHP code inside the JavaScript file? Most of t ...

jQuery floating sidebar that dynamically adjusts position based on content overflow

I've come across an interesting scenario with a fiddle that I'm working on: http://jsfiddle.net/yFjtt/1/ The main concept is to allow users to scroll past the header and have the sidebar 'stick' in place as they continue scrolling down ...

Is it possible to execute a function within an HTML page simply by clicking on it?

As someone who is new to the world of HTML, CSS, and JS, I'm currently facing a challenge: On my website's page1.html, I have implemented a feature that allows users to sort different articles on the page by selecting a sub-menu from the navigat ...

Tips for deactivating dates in the jquery datepicker that correspond to entries in a MySQL database

Is there a way to disable dates directly from a MySQL database in jQuery Datepicker? It seems tedious to constantly update dates in JavaScript, so I'm exploring the option of storing future disable dates in the MySQL database and displaying them in th ...

Unresolved promise rejection on Repl.it

I decided to add a basic leaderboard feature to my game on Repl.it, so I set up a node.js backend for it. Here's the code snippet for the backend: const express = require('express'); const Client = require('@replit/database'); cons ...

Implementing CSS counter-increment with jQuery

Is there a way to use jQuery to set the CSS counter-increment attribute on ".demo:before" even though jQuery cannot access pseudo elements directly? I recall seeing a suggestion on Stack Overflow about using a data attribute and then referencing that value ...

Converting a PHP object array to a JavaScript associative array

My current challenge involves manipulating a PHP object array like so: array(223) { [0]=> object(stdClass)#5 (9) { ["id"]=> string(2) "10" ["name"]=> string(10) "Cyclops" ["address"]=> string(13) "GSDG" } [1]=&g ...

Utilizing memcache in conjunction with PHP and Node.js

Can JavaScript objects and PHP associative arrays be shared with memcache? Alternatively, is it necessary to convert the data into a string before sharing them? ...

What is the best way to transfer data from an AJAX GET request to a Node.js GET route?

Here is an example of an ajax request being made $.ajax({ url: '/reload', type: 'GET', contentType: "application/json", data: { Name: $("#inputName").val(), Link: $("#inputLink").val() }, succe ...

Is there a specific event in Angular.js that triggers when the $scope digest cycle is completed or when the view is refreshed?

Currently, I am making an AJAX request to retrieve data that is needed in the view to generate a list. My goal is to determine when the $scope has been updated and when the view has finished rendering after receiving a successful response. This will allow ...

The browser's back-forward buttons are acting strangely and not functioning as expected. Could it be due to

Currently, I am facing an issue with a <select> tag. When the selected item is changed, AJAX is used to update certain <div>s through the functions: update_div1, update_div2, update_div3. The script for managing this process is functioning corr ...

Ace Code Editor: Turn off highlighted line beneath cursor

I am currently utilizing the Ace editor and would like to remove the shadowed line where the cursor is located. Here's an example Even after experimenting with different themes provided by Ace Mode Creator, the shadowed line still persists. Any sug ...

Tips on creating a horizontal scrolling effect using the mouse

Is there a way to enable horizontal scrolling by holding down the mouse click, instead of relying on the horizontal scroll bar? And if possible, can the scroll bar be hidden? In essence, I am looking to replicate the functionality of the horizontal scroll ...

animation glitches in safari and chrome when using jquery

Hey there! I'm currently working on a form with multiple steps, where I animate the step number for each level of registration. The animation functions perfectly in Firefox, but unfortunately, it's not supported in Chrome and Safari due to an iss ...