Foundations of simulating physical phenomena

Imagine a virtual solar system in 3D.

This is how my simulation operates:

[Calculate new positions] -> [Render] -> [Calculate new positions] -> [Render] etc....

The loop maintains a steady pace of 25 frames per second.

One of the planets in my simulation is hurtling towards the sun at high speed, almost on a collision course. Let's dive into the details of the simulation loop:

  1. Positions are calculated for the planet and the sun (they have not collided yet, but are dangerously close).
  2. The scene is rendered.
  3. Due to finite FPS, there is a slight pause before the next calculation.
  4. New positions are calculated for the planet and sun - and here lies the issue - they did not collide because during the 'pause,' the planet passed through the sun and now their positions are significantly far apart. The collision detection algorithm fails to register any collision.

Here are some possible solutions that I'm considering:

  • Increase the FPS
  • Decrease the speeds of the planets (reduce gravitational constant maybe?)
  • Implement a separate simulation loop that runs concurrently with the rendering loop, but at a faster rate

For those curious, I am utilizing three.js.

Answer №1

In shooter games, dealing with projectiles can be tricky. One workaround is to have the movement stage project a line segment ahead to detect potential collisions before the next frame update. This approach assumes only one object is moving quickly. Alternatively, you could trace a segment between the previous and current positions, although I personally haven't tried this method. Another effective strategy is extending the collider definition forward into an elliptical shape to cover any uncovered areas.

Answer №2

To determine the potential collision of two objects, analyze the change in angle between them. If this change is close to 180 degrees or pi radians, a collision is likely. Utilize linear algebra concepts, specifically the dot product method, to calculate this change:

dx0 = object[i].x - object[j].x;
dy0 = object[i].y - object[j].y;
dz0 = object[i].z - object[j].z;
dx1 = object[i].x + timeStep * object[i].u - object[j].x - timeStep * object[j].u;
dy1 = object[i].y + timeStep * object[i].v - object[j].y - timeStep * object[j].v;
dz1 = object[i].z + timeStep * object[i].w - object[j].z - timeStep * object[j].w;
dangle = acos((dx0 * dx1 + dy0 * dy1 + dz0 * dz1) / (sqrt(dx0 * dx0 + dy0 * dy0 + dz0 * dz0) * sqrt(dx1 * dx1 + dy1 * dy1 + dz1 * dz1)));

Avoid calculating the arc cosine as cos(dangle) approaching -1 indicates a near-180 degree change.

It's vital to experiment with different values to accurately detect collisions. This method should not be the only one used; consider accounting for the objects' constant speed and linear movement during the timestep. Solving a quadratic equation will reveal potential collision times, although it might return more than one solution. To simplify, use the discriminant to confirm a possible collision without pinpointing the exact timing.

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

Error in Python: A scalar index can only be created from integer scalar arrays

I encountered an issue when compiling a program in Python that resulted in a TypeError: only integer scalar arrays can be converted to a scalar index. Despite what seems like a straightforward program, I am struggling to resolve this error. Frequency im ...

A guide on advancing the border like a progress bar in react.js

My goal is to progressively fill a border every minute, reaching full capacity after 60 minutes and then starting over. This task is part of my practice with react.js. I attempted to create a variable using .getMinutes(); to track the current number of mi ...

What mistakes am I making in my usage of React hooks in this situation?

As part of my journey through the FullstackOpen course at the University of Helsinki, I am working on creating a simple Phonebook application. In troubleshooting my code, I've noticed that my 'filterBy' state is consistently one step behind, ...

JQuery and Ajax - Callback function not being triggered

Struggling to solve this issue, I have experimented with various methods but can't seem to find a solution. The code I am using seems to be functioning correctly as it successfully inserts data into the database via the connected script. However, the ...

Ways to set the input=text field as read-only in JSP when receiving information from the backend

For a project I am working on, I need to implement a feature where users can view and edit their personal details on a JSP page. If a user is logged in, their information should be fetched from the session and displayed automatically. However, even if they ...

Unlock the tab element within a modal using ng-bootstrap

I am currently encountering an issue with ng-bootstrap in Angular 2. I am unable to access the #tabs variable from my component using @ViewChild. This problem arises only when I utilize the tab directive within the modal directive. Here is a snippet of m ...

Changing JSON into an array with typescript

I have encountered a JSON structure that is causing me some trouble: const help = [ { "en": [ { "test2": [ { "title": "Naslov1", &quo ...

Generate precise data formats

function modifyMd5(begin,end) { console.log("Provided Begin: "+begin); // When encrypting the provided begin, an incorrect encryption is generated console.log('Using hexMD5 function on begin: (Incorrect)'); var encrypted = md5.h ...

Retrieve all offspring from a JSON dataset containing parent-child relationships

I am working with parent-child JSON data and I need to retrieve all nested children under a selected parent. Let's say we have the following JSON data: [ { "id": 1, "parent": 0, "name": "Parent" }, { "id": 2, "parent": ...

nighttime surveillance struggled to locate the element based on its identifier

On my page, I have the following HTML DOM structure and I am using Nightwatch to write test cases. <td class="GPNWDJGHV" id="gwt-debug-MenuItem/mongo-true">Mongo Extension</td> When trying to select the element using its ID with the '#&a ...

Calculating Vertex Normals in Three.js Framework

I've been exploring the webgl_loader_obj.html example to import an .obj file from blender into three.js Everything is working smoothly and the model is displaying as expected. I wanted to experiment with material.shading = THREE.SmoothShading. Howev ...

Creating JSON from identical user interface components

I have created a form similar to this one: https://jsfiddle.net/6vocc2yn/ that generates a JSON output like below: { "List": [ { "Id": 10, "Name": "SDB_SOLOCHALLENGE_CHALLENGE_DESC_10", "email": "<a href="/cdn-cgi/l/email-pr ...

Difficulty with event listener in React Material UI Autocomplete

My goal is to configure Material UI's Autocomplete component in a way that allows for automatic selection of the closest match when the tab key is pressed. I also need to capture the e.target.value based on the input. However, I have encountered an is ...

Error message: "jQuery is not defined and occurs exclusively in Chrome."

I've been using the following code to asynchronously load my JavaScript in the head section: <script type='text/javascript'> // Add a script element as a child of the body function downloadJSAtOnload() { var element4= document.creat ...

verify prime numbers in a multidimensional array

I have been searching, but I couldn't find any information on what I am about to ask. As a beginner and student learning JavaScript, I have a question. I am familiar with how to check if a number is prime or not. Here is my current challenge: numbers ...

Issue with jQuery .ajax() not triggering success callback function when using JSONP

My Facebook iframe application is attempting to make a cross-domain request to my server for JSONP data. The client-side code I am using is as follows: jQuery.ajax({ url: '***', type: 'post', ...

Implementing a technique to synchronize table updates with only new or modified data using jQuery and ajax

Just diving into the world of jQuery! Currently, I have a table set up to load data from the server using ajax\jQuery every x seconds. The data is being received in JSON format. My main challenge right now is figuring out how to update only specific ...

Is there a way to retrieve the data instead of receiving an undefined result when making an asynchronous call?

subject in user.subjects = subjects below is undefined instead of being an array of subjects. Context: I am working with three database tables - users, subjects, and a relationship table between users and subjects known as users_subjects. The objective i ...

Combining multiple events into one function using jQuery

I am searching for the opposite of what everyone else is seeking. I have an anonymous jQuery function that I want to keep as it is, but I need to attach multiple event handlers to it on different occasions (specifically two events). When the text inside ...

Refreshing an iframe located on disparate domains

There is a webpage called "main.jsp" within the domain "domain1". This page contains an iframe that loads content from another domain known as "domain2". Essentially, "main.jsp" serves as a common content platform, with the iframe displaying content from v ...