A model collision avoidance method based on Three.js

By combining Three.js, Cannon.js, and the A* pathfinding algorithm, the problem of unsmooth model movement in existing collision avoidance methods is solved, enabling real-time model avoidance in complex scenes and improving the realism and interactivity of the virtual environment.

CN117492617BActive Publication Date: 2026-11-10COWAVE SATELLITE COMM TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311436511.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-11-01
Publication Date
2026-11-10
Estimated Expiration
2043-11-01

AI Technical Summary

Technical Problem

Existing collision avoidance methods fail to adequately consider speed and effects during model movement, resulting in unsmooth and unrealistic movement. They cannot fully utilize the potential and characteristics of model movement, leading to suboptimal and inflexible movement.

Method used

Using a method based on Three.js and Cannon.js, the model achieves smooth obstacle avoidance by listening to collision events, employing the A* pathfinding algorithm and the Tween.js library, optimizing path planning with heuristic functions, and simulating obstacles in complex environments through satellite communication.

Benefits of technology

It enables the model to avoid obstacles in real time and smoothly in complex scenes, improving the realism and interactivity of the virtual environment, adapting to multiple obstacles and complex terrain, and enhancing the flexibility and realism of model movement.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117492617B_ABST
    Figure CN117492617B_ABST
Patent Text Reader

Abstract

This invention relates to a model collision avoidance method based on Three.js, specifically including: creating models and objects of moving targets and obstacles using Three.js and Cannon.js; listening for collision events in the physical world, and when a collision occurs, triggering a collision event handling function to obtain the positions of the colliding moving target and obstacle, converting them into grid coordinates; creating an obstacle mesh based on the grid coordinates, triggering an A / B pathfinding algorithm for path planning, and obtaining an avoidance path; using the Tween.js library to implement smooth movement of the model along the avoidance path, enabling the model to avoid obstacles in real time; setting a timer or frame rate control, and re-triggering the A / B pathfinding algorithm for path planning when the position change exceeds a predetermined value. This invention enables models in a Three.js scene to automatically avoid collisions in real time, making user and object interactions more realistic and fluid; by combining with the Cannon.js physics engine, the model can more accurately avoid collisions, making model movement more flexible.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of collision avoidance methods, and in particular to a model collision avoidance method based on Three.js. Background Technology

[0002] Collision avoidance is a crucial and useful technology that unlocks new possibilities and advantages for applications such as gaming, virtual reality, and augmented reality. These applications all require simulating realistic physical phenomena and effects within a virtual environment. Collision avoidance allows objects or characters in a virtual environment to automatically avoid or push away other objects or characters based on their position, speed, and target, thereby enhancing the realism and interactivity of the virtual environment.

[0003] Existing collision avoidance methods generally use fixed or simple algorithms to realize the movement of the model during collision. Although these methods are convenient and fast, they also have some shortcomings. Fixed or simple algorithms cannot fully consider the speed and effect of model movement, resulting in unsmooth and unrealistic model movement; they cannot fully utilize the potential and characteristics of model movement, resulting in unoptimized and inflexible model movement. Summary of the Invention

[0004] Purpose of the invention: To provide a model collision avoidance method based on Three.js to solve the above-mentioned problems existing in the prior art.

[0005] Technical solution: A model collision avoidance method based on Three.js, including the following steps:

[0006] S1. Use Three.js and Cannon.js to create models and objects of moving targets and obstacles, add the models to the scene, and add the objects to the physical world;

[0007] S2. Listen for collision events in the physical world. When a moving target collides with an obstacle, trigger the collision event handling function, obtain the positions of the colliding moving target and obstacle, and convert them into grid coordinates.

[0008] S3. Create an obstacle grid based on the grid coordinates, mark the positions of obstacles on the grid, trigger the A-pathfinding algorithm to perform path planning, and obtain an avoidance path;

[0009] S4. Use the Tween.js library to make the model move smoothly along the avoidance path, so that the model avoids obstacles in real time;

[0010] S5. Set a timer or frame rate control to continuously monitor the position of moving targets and obstacles within a predetermined time interval. When the position change exceeds a predetermined value, re-trigger the A-pathfinding algorithm for path planning.

[0011] According to one aspect of this application, step S1 specifically comprises:

[0012] S11. Use the Three.js library to create a scene, set up the camera and renderer, and add the renderer's output to the HTML page;

[0013] S12. Use Three.js to create models of moving targets and obstacles, set their positions, sizes, and materials, and add the models to the scene;

[0014] S13. Use the Cannon.js library to create a physical world and set the parameters for gravity and the physics calculator.

[0015] S14. Create Cannon.js objects for the moving target and obstacles respectively, set their shape, mass and initial position, and add the objects to the physical world.

[0016] According to one aspect of this application, step S2 specifically comprises:

[0017] S21. Use Cannon.js event listeners to listen for collision events in the physical world. When a moving target collides with an obstacle, the collision event handling function is triggered.

[0018] S22. In the collision event handling function, obtain the collision event object and the positions of the moving target and obstacle in the collision.

[0019] S23. Based on the grid size and origin position in the scene, convert the position information of the moving target and obstacles into grid coordinates;

[0020] S24. Use the rounding function to round the position coordinates to obtain the rounded grid coordinates.

[0021] According to one aspect of this application, step S3 specifically comprises:

[0022] S31. Create a two-dimensional array as an obstacle grid, initialize it to all 0s, and represent the walkable area;

[0023] S32. Based on the grid coordinates of the obstacle, set the corresponding element on the obstacle grid to 1 to represent the obstacle;

[0024] S33. Create an A pathfinding algorithm object, passing in the obstacle grid, the grid coordinates of the moving target, and the grid coordinates of the target position as parameters;

[0025] S34. Call the pathfinding method of the A pathfinding algorithm object, which returns an array containing all grid coordinates on the avoidance path.

[0026] According to one aspect of this application, step S4 specifically comprises:

[0027] S41. Import the Tween.js library, create a Tween object, and pass the position attribute of the moving target model as a parameter;

[0028] S42. Traverse all grid coordinates on the avoidance path, convert them into model position information based on the grid size and origin position in the scene, and add them to the animation sequence of the Tween object;

[0029] S43. Set the basic properties of the Tween object, including duration, easing function, and number of repetitions, and start the animation;

[0030] S44. Update the state of the Tween object before each frame is rendered to make the moving target model move smoothly along the avoidance path.

[0031] According to one aspect of this application, step S5 specifically comprises:

[0032] S51. Use setInterval or requestAnimationFrame to set a timer or frame rate control, specifying the time interval or number of frames;

[0033] S52. Within the time interval or number of frames, acquire the position information of the moving target and obstacles, compare it with the previous position information, and calculate the distance or angle of the position change.

[0034] S53. If the position change exceeds a threshold, the A-pathfinding algorithm will be retried for path planning; otherwise, monitoring will continue.

[0035] According to one aspect of this application, step S1 further includes setting the rotation and scaling properties of the model and the object, specifically:

[0036] S1a. Use Three.js and Ammo.js to set the rotation and scaling properties of models and objects; rotation properties are represented by Euler angles or quaternions, and scaling properties are represented by a 3D vector;

[0037] S1b: When detecting collisions between models and objects, use Three.js to obtain the model's position, rotation, and scaling information in the scene; use Ammo.js to obtain the object's position, rotation, and scaling information in the physical world.

[0038] S1c uses Three.js to update the model's properties based on the object's position, rotation, and scaling information, and uses Ammo.js to update the object's properties based on the model's position, rotation, and scaling information, thus achieving synchronization of position, rotation, and scaling.

[0039] According to one aspect of this application, it also includes using heuristic functions to optimize the performance and results of the A-pathfinding algorithm, specifically:

[0040] Sa, Define a heuristic function to calculate an estimate based on the current position of the moving target and the target position;

[0041] Sb. In the A-pathfinding algorithm, a heuristic function is used to sort and select candidate nodes based on the estimated values;

[0042] Sc. Find the optimal or suboptimal path based on the sorting and selection results;

[0043] It also includes using easing functions to adjust the speed and effect of model movement;

[0044] It also includes the use of obstacle generation methods to increase scene diversity. Obstacle generation methods include random generation, terrain-based generation, and rule-based generation.

[0045] According to one aspect of this application, step S1 further includes:

[0046] Step S1i: On the web browser, use the Three.js library to create a 3D scene, set up the camera and renderer, and add the renderer's output to the HTML page; On the web browser, use Three.js to create a model of a robot or drone, and set their position, size, and materials;

[0047] Step S1ii: On the satellite side, create a physical world using the Cannon.js library and set the parameters for gravity and the physics calculator; on the satellite side, create Cannon.js objects for the robot or drone respectively, set their shape, mass and initial position, and add the objects to the physical world.

[0048] Step S1iii: Create satellite communication modules on both the web browser and satellite sides to enable data transmission and reception. The satellite communication modules use communication methods including radio, laser, or infrared, and perform signal processing based on the satellite's orbit and position, including modulation, encoding, encryption, transmission, decryption, decoding, and demodulation.

[0049] According to one aspect of this application, step S3 further includes:

[0050] Step S3i: Receive grid coordinates sent by the satellite;

[0051] Step S3ii: The 3D scene is abstracted into an undirected graph, where nodes represent models or obstacles, and edges represent the distances or connections between them; this undirected graph is constructed based on the detected information, and each node and edge is assigned some feature values, including position coordinates, movement speed, and target direction;

[0052] Step S3iii: Use GCN or GRN graph neural networks to learn the embedding vector of each node, representing their position and state in the scene, and set the loss function and optimization objective according to the task requirements;

[0053] Step S3iv: Use the embedding vectors output by the graph neural network to calculate the distance or cost from each node to the target node, and use a greedy strategy or other optimization methods to select the optimal movement direction.

[0054] Data is transmitted via a satellite communication module: On the satellite side, the avoidance path is converted into a data packet and sent to the web browser via the satellite communication module; on the web browser side, after receiving the data packet, it is converted into an avoidance path, and the position and state of the model are updated through the Three.js library, and the renderer is called to perform rendering.

[0055] Beneficial effects: This invention enables models in Three.js scenes to automatically avoid collisions in real time, making user and object interactions more realistic and smooth; the method of this invention can handle complex scenes, including multiple obstacles and complex terrains, and by combining with the Cannon.js physics engine, the model can avoid collisions more accurately, making the model's movement more flexible. Attached Figure Description

[0056] Figure 1 This is a simulation scenario diagram of the collision avoidance of the present invention. Detailed Implementation

[0057] Collision avoidance is a crucial and useful technology that unlocks new possibilities and advantages for applications such as gaming, virtual reality, and augmented reality. These applications all require simulating realistic physical phenomena and effects within a virtual environment. Collision avoidance allows objects or characters in a virtual environment to automatically avoid or push away other objects or characters based on their position, speed, and target, thereby enhancing the realism and interactivity of the virtual environment.

[0058] Existing collision avoidance methods generally use fixed or simple algorithms to realize the movement of the model during collision. Although these methods are convenient and fast, they also have some shortcomings. Fixed or simple algorithms cannot fully consider the speed and effect of model movement, resulting in unsmooth and unrealistic model movement; they cannot fully utilize the potential and characteristics of model movement, resulting in unoptimized and inflexible model movement.

[0059] This invention proposes a model collision avoidance method based on Three.js, including:

[0060] S1. Use Three.js and Cannon.js to create models and objects for moving targets and obstacles, add the models to the scene, and add the objects to the physical world.

[0061] Three.js is a JavaScript-based 3D graphics library used to create, render, and display 3D scenes and models in web browsers. It provides a simple and easy-to-use API, enabling developers to easily build interactive 3D applications and games. Three.js supports various materials and lighting effects, achieving highly realistic 3D graphics displays. Cannon.js is a JavaScript library for physics simulation. It can simulate real-world physical behaviors such as gravity, collisions, and inertia. Cannon.js can be used to create physical worlds and objects, and to implement object motion and collision detection in 3D scenes. In this invention, Cannon.js is used to simulate the collision behavior of objects and trigger relevant processing functions when a collision event occurs.

[0062] In a further embodiment, a spherical moving target model and a cubic obstacle model are created. Geometry is created using the SphereGeometry and BoxGeometry classes of Three.js, materials are created using the MeshBasicMaterial class of Three.js, models are created using the Mesh class of Three.js, and objects are created using the Sphere and Box classes of Cannon.js. Their mass, position, and other properties are set, and then the models are added to the scene and the objects are added to the physical world.

[0063] This allows for the correspondence between models and objects, enabling models to display the motion state of objects in the physical world within a scene.

[0064] S11. Use the Three.js library to create a scene, set up the camera and renderer, and add the renderer's output to the HTML page;

[0065] S12. Use Three.js to create models of moving targets and obstacles, set their positions, sizes, and materials, and add the models to the scene;

[0066] S13. Use the Cannon.js library to create a physical world and set the parameters for gravity and the physics calculator.

[0067] S14. Create Cannon.js objects for the moving target and obstacles respectively, set their shape, mass and initial position, and add the objects to the physical world.

[0068] Furthermore, a scene object is created using the Scene class in Three.js; a perspective camera object is created using the PerspectiveCamera class in Three.js, and its viewpoint, aspect ratio, near plane, and far plane properties are set; a WebGL renderer object is created using the WebGLRenderer class in Three.js, and its size, background color, anti-aliasing, and other properties are set; the renderer's output is added to the HTML page using the document.body.appendChild method in Three.js; spherical and cube geometry objects are created using the SphereGeometry and BoxGeometry classes in Three.js, and their radius, width, height, and depth properties are set; a basic material object is created using the MeshBasicMaterial class in Three.js, and its color, transparency, texture, and other properties are set; a mesh object is created using the Mesh class in Three.js, and the geometry object and material object are passed as parameters; and the posi is used in Three.js... The `tion` property sets the position of the mesh object in the scene, and the `scene.add` method of Three.js adds the mesh object to the scene. A physics world object is created using the `World` class of Cannon.js, and its gravity direction and size properties are set. A collision detector object is created using the `NaiveBroadphase` class of Cannon.js and assigned to the `broadphase` property of the physics world object. A physics solver object is created using the `Solver` class of Cannon.js and assigned to the `solver` property of the physics world object. Sphere and Box objects are created using the `Sphere` and `Box` classes of Cannon.js, and their radius, width, height, and depth properties are set. A rigid body object is created using the `Body` class of Cannon.js, passing its shape and mass as parameters. The `position` property of Cannon.js sets the initial position of the rigid body object in the physics world, and the `world.add` method of Cannon.js adds the rigid body object to the physics world.

[0069] This allows for the creation and setup of scenes, cameras, renderers, models, and objects, enabling scenes to display models and objects in HTML pages, and allowing objects to be affected by gravity and collisions in the physical world.

[0070] S2. Listen for collision events in the physical world. When a moving target collides with an obstacle, trigger the collision event handling function, obtain the positions of the colliding moving target and obstacle, and convert them into grid coordinates.

[0071] S21. Use Cannon.js event listeners to listen for collision events in the physical world. When a moving target collides with an obstacle, the collision event handler function is triggered.

[0072] S22. In the collision event handling function, obtain the collision event object and the positions of the moving target and obstacle in the collision.

[0073] S23. Based on the grid size and origin position in the scene, convert the position information of the moving target and obstacles into grid coordinates;

[0074] S24. Use the rounding function to round the position coordinates to obtain the rounded grid coordinates.

[0075] Furthermore, a 'collide' event listener is added to the physical world using the `world.addEventListener` method of Cannon.js, passing in a custom collision event handler function as a parameter. Within the collision event handler function, the `event.contact` property of Cannon.js can be used to obtain the collision event object, from which the position information of the moving target and obstacles in the collision can be retrieved, such as `event.contact.bi.position` and `event.contact.bj.position`. Then, based on the grid size and origin position in the scene, the position information of the moving target and obstacles is converted into grid coordinates, such as (x-x0) / dx and (y-y0) / dy, where x0 and y0 are the origin position, and dx and dy are the grid size. Finally, the position coordinates are rounded using rounding functions such as `Math.floor` or `Math.round` to obtain the rounded grid coordinates, such as `Math.floor((x-x0) / dx)` and `Math.floor((y-y0) / dy)`.

[0076] This enables the detection and response to collision events, allowing moving targets to know that they have collided with obstacles and obtain the corresponding location information.

[0077] S3. Create an obstacle grid based on the grid coordinates, mark the positions of obstacles on the grid, trigger the A-pathfinding algorithm to perform path planning, and obtain an avoidance path.

[0078] A* Pathfinding is a commonly used graph search algorithm used to find the optimal path from a starting point to a target point. It is widely used in grid-like scenarios, such as game maps and automatic navigation. The A* algorithm selects the optimal path through heuristic search by evaluating the cost and estimate of each node. In this invention, A* Pathfinding is used to find an avoidance path for the model in an obstacle grid, enabling the model to intelligently avoid obstacles.

[0079] S31. Create a two-dimensional array as an obstacle grid, initialize it to all 0s, and represent the walkable area;

[0080] S32. Based on the grid coordinates of the obstacle, set the corresponding element on the obstacle grid to 1 to represent the obstacle;

[0081] S33. Create an A pathfinding algorithm object, passing in the obstacle grid, the grid coordinates of the moving target, and the grid coordinates of the target position as parameters;

[0082] S34. Call the pathfinding method of the A pathfinding algorithm object, which returns an array containing all grid coordinates on the avoidance path.

[0083] In a further embodiment, a two-dimensional array is created as an obstacle grid, such as var grid=[[0,0,0,0,0],[0,0,1,0,0],[0,1,1,1,0],[0,0,1,0,0],[0,0,0,0,0]], where 0 represents walkable areas and 1 represents obstacles; according to the grid coordinates of the obstacles, the corresponding elements on the obstacle grid are set to 1, such as grid[2][2]=1; then an A-pathfinding algorithm object is created, such as var astar=new AStar(grid), and the obstacle grid is passed in as a parameter; the pathfinding method of the A-pathfinding algorithm object is called, such as var path = astar.findPath([4,4],[0,0]), and pass in the grid coordinates of the moving target and the grid coordinates of the target position as parameters; return an array containing all grid coordinates on the avoidance path, such as path = [[4,4],[3,3],[2,2],[1,1],[0,0]].

[0084] This setting allows for the description and representation of the distribution of obstacles in the scene, enabling moving targets to move based on the shortest or optimal path between themselves and the target location.

[0085] S4. Use the Tween.js library to enable the model to move smoothly along the avoidance path, so that the model can avoid obstacles in real time.

[0086] S41. Import the Tween.js library, create a Tween object, and pass the position attribute of the moving target model as a parameter;

[0087] S42. Traverse all grid coordinates on the avoidance path, convert them into model position information based on the grid size and origin position in the scene, and add them to the animation sequence of the Tween object;

[0088] S43. Set the basic properties of the Tween object, including duration, easing function, and number of repetitions, and start the animation;

[0089] S44. Update the state of the Tween object before each frame is rendered to make the moving target model move smoothly along the avoidance path.

[0090] In a further embodiment, the Tween.js library is imported, a Tween object is created, and the position attribute of the moving target model is passed as a parameter, such as var tween = new TWEEN.Tween(player.position); all grid coordinates on the avoidance path are traversed, and based on the grid size and origin position in the scene, they are converted into model position information and added to the animation sequence of the Tween object, such as tween.to({x:x0+x*dx,y:y0+y*dy},1000), where x0 and y0 are the origin position, and dx and dy are the grid size; the duration, easing function, number of repetitions, and other properties of the Tween object are set, and the animation is started.

[0091] For example, `tween.easing(TWEEN.Easing.Quadratic.InOut).repeat(Infinity).start();` updates the state of the Tween object before each frame is rendered, so that the moving target model moves smoothly along the avoidance path, such as `TWEEN.update(time)`.

[0092] This allows for the control and optimization of model movement, enabling the moving target model to move along the avoidance path at a certain speed and with a certain effect, avoiding stuttering or jittering.

[0093] S5. Set a timer or frame rate control to continuously monitor the position of moving targets and obstacles within a predetermined time interval. When the position change exceeds a predetermined value, re-trigger the A-pathfinding algorithm for path planning.

[0094] S51. Use setInterval or requestAnimationFrame to set a timer or frame rate control, specifying the time interval or number of frames;

[0095] S52. Within the time interval or number of frames, acquire the position information of the moving target and obstacles, compare it with the previous position information, and calculate the distance or angle of the position change.

[0096] S53. If the position change exceeds a threshold, the A-pathfinding algorithm will be retried for path planning; otherwise, monitoring will continue.

[0097] In a further embodiment, a timer is set using the `setInterval` method, specifying a time interval of 100 milliseconds. Within each time interval, the position information of the moving target and obstacles, such as `player.position` and `obstacle.position`, is obtained using the `position` property of Cannon.js. This position information is then compared with the previous position information to calculate the distance of the position change, which is `Math.sqrt((player.position.x - player.prevPosition.x)`. 2 +(player.position.y-player.prevPosition.y) 2 If the position changes by more than 0.5 meters, the A-pathfinding algorithm is retried to perform path planning; otherwise, monitoring continues.

[0098] This enables real-time response to changes in the position of obstacles in the scene, allowing the moving target model to plan its path and move according to the latest obstacle distribution, thereby improving avoidance efficiency and effectiveness.

[0099] Furthermore, step S1 also includes setting the rotation and scaling properties of the model and object, specifically:

[0100] S1a. Use Three.js and Ammo.js to set the rotation and scaling properties of models and objects; rotation properties are represented by Euler angles or quaternions, and scaling properties are represented by a 3D vector;

[0101] S1b: When detecting collisions between models and objects, use Three.js to obtain the model's position, rotation, and scaling information in the scene; use Ammo.js to obtain the object's position, rotation, and scaling information in the physical world.

[0102] S1c uses Three.js to update the model's properties based on the object's position, rotation, and scaling information, and uses Ammo.js to update the object's properties based on the model's position, rotation, and scaling information, thus achieving synchronization of position, rotation, and scaling.

[0103] To set a random rotation angle and a random scaling ratio for the moving target model, use the Euler or Quaternion class from Three.js to create rotation properties and the Vector3 class from Three.js to create scaling properties, assigning them to the model's rotation and scale properties. Then, set the same rotation angle and scaling ratio for the moving target object, using the btQuaternion or btMatrix3x3 class from Ammo.js to create rotation properties and the btVector3 class from Ammo.js to create scaling properties, assigning them to the object's quaternion or basis and localScaling properties. When detecting collisions, use the position, rotation, or quaternion and scale properties from Three.js to obtain the model's position, rotation, and scaling information in the scene. Use the getWorldTransform method from Ammo.js to obtain the object's transformation matrix in the physical world and extract its position, rotation, and scaling information. Then, use the copy method from Three.js to copy the object's position, rotation, and scaling information to the model, and use the setWorldTransform method from Ammo.js to set the model's position, rotation, and scaling information to the object, achieving synchronization of position, rotation, and scaling.

[0104] This allows for the setting and updating of rotation and scaling properties of models and objects, enabling models to display the physical transformations of objects within the scene.

[0105] Furthermore, it also includes using heuristic functions to optimize the performance and results of the A-pathfinding algorithm, specifically:

[0106] Sa, Define a heuristic function to calculate an estimate based on the current position of the moving target and the target position;

[0107] Sb. In the A-pathfinding algorithm, a heuristic function is used to sort and select candidate nodes based on the estimated values;

[0108] Sc. Find the optimal or suboptimal path based on the sorting and selection results.

[0109] Define a heuristic function as the Euclidean distance function, which is the estimated value calculated based on the current moving target position (x1, y1) and the target position (x2, y2) as sqrt((x2-x1)). 2 +(y2-y1) 2Then, in the A* pathfinding algorithm, a heuristic function is used to calculate the estimated value of each candidate node and add it to the distance already traversed from the starting point to that node as the total cost. The candidate nodes are then sorted according to the total cost, and the node with the minimum cost is selected as the next node to be visited. This process is repeated until the target node is found or there are no more candidate nodes. This optimizes the performance and results of the A* pathfinding algorithm, enabling the moving target to find the shortest or optimal path faster and avoiding detours or dead ends.

[0110] Furthermore, this includes using easing functions to adjust the speed and effect of model movement. First, select a suitable easing function based on the model's movement effect and speed, import the corresponding easing function library, create an easing object, and pass the model's position attributes as parameters; then, add all position information on the path to the easing object, and set the easing object's duration, repetition count, and other attributes; finally, update the easing object's state before rendering each frame, and make the model move smoothly along the path.

[0111] When setting the properties of a Tween object, you can use different easing functions provided by the Tween.js library, such as Linear, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Elastic, Back, and Bounce, to adjust the speed and effect of the model's movement. Different easing functions will cause different changes when the model moves, such as constant speed, acceleration, deceleration, elasticity, and bounce.

[0112] By using easing functions, the speed and effect of model movement can be adjusted, allowing moving targets to choose appropriate easing functions according to their preferences or the needs of the scene, thus increasing the fun and aesthetics of movement.

[0113] In a further embodiment, obstacle generation methods such as random generation, terrain-based generation, and rule-based generation are also included to increase scene diversity. First, an appropriate obstacle generation method is selected based on the scene's diversity and difficulty. An obstacle generation function is defined to generate the position, size, and shape of obstacles based on random numbers, terrain data, and rule parameters. Then, obstacle models are created in the scene, and their positions, sizes, and materials are set, and they are added to the scene. Finally, corresponding physical objects are created for the obstacle models, and their shapes, masses, and initial positions are set, and they are added to the physical world.

[0114] Furthermore, a random generation method can be used, where parameters such as the number, location range, and size range of obstacles are set, and random numbers are generated using the Math.random method. Obstacle models and objects are then created based on these random numbers. Alternatively, a terrain-based generation method can be used, where parameters such as terrain height maps or noise maps are set, and terrain geometry is created using the Three.js PlaneGeometry or TerrainGeometry classes. Obstacle models and objects are then created based on the height or noise values ​​on the terrain geometry. Finally, a rule-based generation method can be used, where parameters such as obstacle arrangement rules or distribution rules are set, and obstacle models and objects are created using algorithms such as loops or recursion.

[0115] This allows for a diversification of obstacle generation methods, enabling scenes to exhibit different styles and characteristics, thus increasing the richness and complexity of the scenes.

[0116] In a further embodiment, a detailed implementation process of the model collision avoidance method based on Three.js is given, specifically as follows:

[0117] A. Initialize the Three.js scene and the Cannon.js physical world:

[0118] Creating a Three.js scene: Use the Three.js library to create a scene, set up the camera and renderer, and add the renderer's output to the HTML page.

[0119] Create moving target and obstacle models: Use Three.js to create models of moving targets and obstacles, and set their positions, sizes, and materials.

[0120] Creating a physics world in Cannon.js: Use the Cannon.js library to create a physics world and set the parameters for gravity and the physics calculator.

[0121] Create Cannon.js objects: Create Cannon.js objects for the moving target and obstacles, and set their shape, mass, and initial position. Add the objects to the physical world.

[0122] Three.js is used to build 3D scenes and display models; Cannon.js is used to simulate physical behavior and perform collision detection.

[0123] B. Listen for collision events and trigger A's pathfinding algorithm:

[0124] Arithmetic pathfinding algorithms are used for path planning to find avoidance paths for a model. Since Arithmetic pathfinding is typically used for path planning on 2D or 3D meshes, an obstacle grid needs to be created in the scene to represent walkable areas and obstacles. Then, the Arithmetic algorithm is used to find paths on this grid.

[0125]

[0126]

[0127]

[0128] Use Cannon.js to listen for collision events: In the physics world, listen for collision events and trigger the collision event handler function when a moving target collides with an obstacle.

[0129] Collision event handling function: In the collision event handling function, the position of the colliding object (moving target and obstacle) is obtained and converted into grid coordinates. An obstacle mesh is created based on the grid coordinates, and the positions of the obstacles are marked on the mesh.

[0130] Trigger A-Pathfinding Algorithm: Use the grid coordinates of the moving target as the starting point and set the grid coordinates of the target position as the ending point to trigger the A-Pathfinding Algorithm for path planning.

[0131] C and A pathfinding algorithms:

[0132] Create an A node class: Define an AStarNode class to represent a node in the A algorithm, containing the node's grid coordinates and some auxiliary attributes, such as parent node, G value, H value, etc.

[0133] The G value represents the accumulated cost from the starting node to the current node, reflecting the length or cost of the actual path traversed. At the start of the algorithm, the G value of the starting node is 0. As the algorithm progresses, the G value of each subsequent node is updated based on the G value of its parent node and its distance from the parent node. The H value is a heuristic cost estimate representing the projected cost from the current node to the target node, reflecting the length or cost of the path that may be traversed in the future.

[0134] Initialize open and closed lists: Create an open list and a closed list to store nodes to be examined and nodes that have already been examined.

[0135] The A* algorithm's main loop: In the main loop, select the node with the smallest F value from the open list as the current node, move it to the closed list, and examine its adjacent nodes.

[0136] Calculate the G, H, and F values ​​of nodes: For adjacent nodes, calculate their G, H, and F values ​​and process them accordingly based on whether they are in the open or closed list.

[0137] The F-value represents the total estimated cost of the path from the starting node to the target node, passing through the current node. It is equal to the sum of the G-value and the H-value. The F-value combines the costs of already traversed paths and those that may be traversed in the future, and is used to evaluate the priority of each node in the search.

[0138] Backtracking path: Once the target node is found, backtracking begins from the target node to obtain the avoidance path.

[0139] D. Update the avoidance path:

[0140] Using timers or frame rate control: To avoid overcomputation, timers or frame rate control can be used to limit the frequency of path planning.

[0141] Continuous monitoring of object positions: Set a timer or frame rate control to continuously monitor the positions of moving targets and obstacles within a specified time interval. When there is a significant change in position, the A-pathfinding algorithm is re-triggered for path planning.

[0142] Update model avoidance movement: Once a new avoidance path is obtained, the Tween.js library is used to enable the model to move smoothly along the path, allowing the model to avoid obstacles in real time.

[0143] Furthermore, such as Figure 1 As shown, a simulated scene is constructed in a 3D environment, creating a target block and obstacles. When the target block begins to move, it detects a collision with an obstacle in the physical world, triggering an obstacle detection event. This event prompts the execution of the A* pathfinding algorithm, which is applied to a discretized grid in the 3D scene to retrieve the optimal path. Specifically, the grid is marked with the locations of obstacles, and the A* algorithm performs path searching on this grid to plan an obstacle avoidance path for the moving target.

[0144] This process is similar to an intelligent agent perceiving and planning a path in a complex environment, utilizing a combination of sensor information (collision detection) and pathfinding (Algorithm A). The moving target analyzes collision events, becomes aware of potential obstacles, and transforms this information into a search problem in a discretized space. Algorithm A evaluates feasible paths and finds the optimal path that allows the target to reach its destination while avoiding obstacles.

[0145] Finally, the path planning results are animated using the Tween.js library. Tween.js connects the discrete points along the path, creating a smooth animation that allows the moving target to traverse complex obstacle courses in a fluid manner.

[0146] In a further embodiment, certain special or scientific missions require the deployment of small robots or drones in space to perform tasks in complex environments and communicate via satellite. These robots or drones can use technologies similar to this approach to achieve collision detection and automatic obstacle avoidance and pathfinding in space.

[0147] S1. Initialize the Web Browser and Satellite Sides: On the web browser side, create a 3D scene using the Three.js library, set up the camera and renderer, and add the renderer's output to the HTML page. On the satellite side, create a physical world using the Cannon.js library and set the parameters for gravity and the physics calculator.

[0148] S2. Create robot or drone models: On the web browser side, use Three.js to create robot or drone models and set their position, size, and materials. On the satellite side, create Cannon.js objects for the robot or drone respectively and set their shape, mass, and initial position. Add the objects to the physical world.

[0149] S3. Create a satellite communication module: Create satellite communication modules on both the web browser and satellite sides to enable data transmission and reception. The satellite communication module can use radio, laser, or infrared methods to perform signal modulation, encoding, encryption, transmission, decryption, decoding, and demodulation operations based on the satellite's orbit and position.

[0150] S4. Listen for collision events and trigger the A / B pathfinding algorithm: In the collision event handling function, obtain the position of the colliding object (robot, drone, or other objects) and convert it to grid coordinates. Create an obstacle mesh based on the grid coordinates and mark the positions of the obstacles on the mesh. Trigger the A / B pathfinding algorithm, using the robot or drone's grid coordinates as the starting point and the target location's grid coordinates as the ending point, to perform path planning.

[0151] S5.A Pathfinding Algorithm: Define an `AStarNode` class to represent a node in the A algorithm, containing the node's grid coordinates and some auxiliary attributes such as parent node, G value, and H value. Create an open list and a closed list to store nodes to be examined and nodes already examined. In the main loop, select the node with the smallest F value from the open list as the current node, move it to the closed list, and examine its neighboring nodes. For neighboring nodes, calculate their G, H, and F values, and process them accordingly based on whether they are in the open or closed list. Once the target node is found, backtrack from the target node to obtain the avoidance path.

[0152] S6. Update the avoidance path: Using a timer or frame rate control, continuously monitor the position of the robot, drone, and other objects at specified time intervals. When there is a significant positional change, re-trigger the A* pathfinding algorithm for path planning. Once a new avoidance path is obtained, use the Tween.js library to enable the model to move smoothly along the path, allowing the model to avoid obstacles in real time.

[0153] S7. Data transmission via satellite communication module: On the satellite side, the avoidance path is converted into data packets and sent to the web browser via the satellite communication module. On the web browser side, after receiving the data packets, it is converted into an avoidance path, and the position and state of the model are updated using the Three.js library, and the renderer is invoked for rendering.

[0154] In a further embodiment, the 3D scene is abstracted as an undirected graph, where nodes represent models or obstacles, and edges represent distances or connections between them. This graph can be constructed based on detected information, and each node and edge can be assigned some feature values, such as position coordinates, movement speed, target direction, etc.

[0155] Next, a graph neural network is used to learn the embedding vector of each node, representing its position and state in the scene. Appropriate graph neural network models and algorithms can be selected, such as GCN, GAT, or GRN, and different loss functions and optimization objectives can be set according to different tasks. For example, if the goal is to achieve node classification, i.e., determining which nodes are enemies or allies, the cross-entropy loss function can be used; if the goal is to achieve edge prediction, i.e., determining which edges are safe or dangerous, the binary cross-entropy loss function can be used; if the goal is to achieve graph generation, i.e., generating new nodes or edges, the generative adversarial loss function can be used, and so on.

[0156] Then, the embedding vectors output by the graph neural network are used to calculate the distance or cost from each node to the target node, and a greedy strategy or other optimization methods are used to select the optimal movement direction. Different evaluation criteria and constraints can be set according to different scenarios, such as considering enemies, friendly forces, obstacles, resources, etc. Finally, data is transmitted through a satellite communication module: on the satellite side, the avoidance path is converted into data packets and sent to the web browser. On the web browser side, after receiving the data packets, it is converted into an avoidance path, the position and state of the model are updated through the Three.js library, and the renderer is invoked for rendering.

[0157] In this embodiment, a graph neural network (Graph Neural Network) is used to replace or optimize the A* pathfinding algorithm, thereby improving the innovation of this approach. A Graph Neural Network is a deep learning-based method for processing graph-structured data. It can learn the structural features and node attributes of a graph and can be used for various graph-related tasks. This approach abstracts the 3D scene and model as an undirected graph and uses a Graph Neural Network to learn the embedding vector of each node, representing its position and state in the scene. These embedding vectors are then used to calculate the distance or cost from each node to the target node, and a greedy strategy or other optimization methods are used to select the optimal movement direction. This enables a Graph Neural Network-based pathfinding algorithm that can adapt to complex and dynamically changing scenes and has better generalization ability and efficiency.

[0158] This invention cleverly combines Three.js for scene rendering, Cannon.js for physics simulation and collision detection, and the A / B pathfinding algorithm for path planning. This integrated approach provides a comprehensive solution capable of handling real-time model avoidance. Other algorithms not only rely on A / B pathfinding but also incorporate collision detection and physics simulation to achieve automatic avoidance. When the model encounters an obstacle, it automatically adjusts its path to avoid collisions, which is extremely useful for applications such as games, virtual reality, and augmented reality, enabling more realistic and fluid user-object interactions. The collision avoidance method in this invention can handle complex scenes, including multiple obstacles and complex terrain. By combining with the Cannon.js physics engine, the model can more accurately avoid collisions. This method also provides a basic framework that can be further customized and extended according to project needs, optimizing collision detection and pathfinding algorithms to adapt to larger-scale and more complex applications. This invention also improves development efficiency by combining with existing libraries and algorithms, saving development time and effort. These libraries provide efficient implementations, allowing developers to focus on algorithm integration and optimization.

[0159] The preferred embodiments of the present invention have been described in detail above. However, the present invention is not limited to the specific details in the above embodiments. Within the scope of the technical concept of the present invention, various equivalent transformations can be made to the technical solutions of the present invention, and these equivalent transformations all fall within the protection scope of the present invention.

Claims

1. A model collision avoidance method based on Three.js, characterized in that, Includes the following steps: S1. Use Three.js and Cannon.js to create models and objects of moving targets and obstacles, add the models to the scene, and add the objects to the physical world; S2. Listen for collision events in the physical world. When a moving target collides with an obstacle, trigger the collision event handling function, obtain the positions of the colliding moving target and obstacle, and convert them into grid coordinates. S3. Create an obstacle grid based on the grid coordinates, mark the positions of obstacles on the grid, trigger the A-pathfinding algorithm to perform path planning, and obtain an avoidance path; S4. Use the Tween.js library to make the model move smoothly along the avoidance path, so that the model avoids obstacles in real time; S5. Set a timer or frame rate control to continuously monitor the position of moving targets and obstacles within a predetermined time interval. When the position change exceeds a predetermined value, re-trigger the A-pathfinding algorithm for path planning. Step S1 also includes setting the rotation and scaling properties of the model and object, specifically: S1a. Use Three.js and Ammo.js to set the rotation and scaling properties of models and objects; the rotation properties are represented by Euler angles or quaternions, and the scaling properties are represented by a 3D vector; S1b: When detecting whether a model and an object collide, use Three.js to obtain the model's position, rotation, and scaling information in the scene; Use Ammo.js to obtain the position, rotation, and scaling information of an object in the physical world; S1c uses Three.js to update the model's properties based on the object's position, rotation, and scaling information, and uses Ammo.js to update the object's properties based on the model's position, rotation, and scaling information, thus achieving synchronization of position, rotation, and scaling. Step S3 further includes: Step S3i: Receive grid coordinates sent by the satellite; Step S3ii: The 3D scene is abstracted into an undirected graph, where nodes represent models or obstacles, and edges represent the distances or connections between them; this undirected graph is constructed based on the detected information, and each node and edge is assigned some feature values, including position coordinates, movement speed, and target direction; Step S3iii: Use GCN or GRN graph neural networks to learn the embedding vector of each node, representing their position and state in the scene, and set the loss function and optimization objective according to the task requirements; Step S3iv: Use the embedding vectors output by the graph neural network to calculate the distance or cost from each node to the target node, and use a greedy strategy or other optimization methods to select the optimal movement direction. Data is transmitted via a satellite communication module: On the satellite side, the avoidance path is converted into a data packet and sent to the web browser via the satellite communication module; On the web browser side, after receiving the data packet, it is converted into an avoidance path, and the position and state of the model are updated through the Three.js library, and the renderer is called to perform rendering. Specifically, a graph neural network is used to learn the embedding vector of each node, representing its position and state in the scene; a graph neural network model and algorithm, including GCN, GAT or GRN, are selected, and different loss functions and optimization objectives are set according to different tasks; To achieve node classification, that is, to determine which nodes are enemies or allies, the cross-entropy loss function is used; To achieve edge prediction, that is, to determine which edges are safe or dangerous, the binary cross-entropy loss function is used; To achieve graph generation, i.e., to generate new nodes or edges, a generative adversarial loss function is used.

2. The model collision avoidance method based on Three.js according to claim 1, characterized in that, Step S1 is as follows: S11. Use the Three.js library to create a scene, set up the camera and renderer, and add the renderer's output to the HTML page; S12. Use Three.js to create models of moving targets and obstacles, set their positions, sizes, and materials, and add the models to the scene; S13. Use the Cannon.js library to create a physical world and set the parameters for gravity and the physics calculator. S14. Create Cannon.js objects for the moving target and obstacles respectively, set their shape, mass and initial position, and add the objects to the physical world.

3. The model collision avoidance method based on Three.js according to claim 1, characterized in that, Step S2 is as follows: S21. Use Cannon.js event listeners to listen for collision events in the physical world. When a moving target collides with an obstacle, the collision event handler function is triggered. S22. In the collision event handling function, obtain the collision event object and the positions of the moving target and obstacle in the collision. S23. Based on the grid size and origin position in the scene, convert the position information of the moving target and obstacles into grid coordinates; S24. Use the rounding function to round the position coordinates to obtain the rounded grid coordinates.

4. The model collision avoidance method based on Three.js according to claim 1, characterized in that, Step S3 is as follows: S31. Create a two-dimensional array as an obstacle grid, initialize it to all 0s, and represent the walkable area; S32. Based on the grid coordinates of the obstacle, set the corresponding element on the obstacle grid to 1 to represent the obstacle; S33. Create an A pathfinding algorithm object, passing in the obstacle grid, the grid coordinates of the moving target, and the grid coordinates of the target position as parameters; S34. Call the pathfinding method of the A pathfinding algorithm object, which returns an array containing all grid coordinates on the avoidance path.

5. The model collision avoidance method based on Three.js according to claim 1, characterized in that, Step S4 is as follows: S41. Import the Tween.js library, create a Tween object, and pass the position attribute of the moving target model as a parameter; S42. Traverse all grid coordinates on the avoidance path, convert them into model position information based on the grid size and origin position in the scene, and add them to the animation sequence of the Tween object; S43. Set the basic properties of the Tween object, including duration, easing function, and number of repetitions, and start the animation; S44. Update the state of the Tween object before each frame is rendered to make the moving target model move smoothly along the avoidance path.

6. The model collision avoidance method based on Three.js according to claim 1, characterized in that, Step S5 is as follows: S51. Use setInterval or requestAnimationFrame to set a timer or frame rate control, specifying the time interval or number of frames; S52. Within the time interval or number of frames, acquire the position information of the moving target and obstacles, compare it with the previous position information, and calculate the distance or angle of the position change. S53. If the position change exceeds a threshold, the A-pathfinding algorithm will be retried for path planning; otherwise, monitoring will continue.

7. The model collision avoidance method based on Three.js according to claim 1, characterized in that, This also includes using heuristic functions to optimize the performance and results of the A-pathfinding algorithm, specifically: Sa, Define a heuristic function to calculate an estimate based on the current position of the moving target and the target position; Sb. In the A-pathfinding algorithm, a heuristic function is used to sort and select candidate nodes based on the estimated values; Sc. Find the optimal or suboptimal path based on the sorting and selection results; It also includes using easing functions to adjust the speed and effect of model movement; It also includes the use of obstacle generation methods to increase scene diversity. Obstacle generation methods include random generation, terrain-based generation, and rule-based generation.

8. The model collision avoidance method based on Three.js according to claim 1, characterized in that, Step S1 further includes: Step S1i: On the web browser, use the Three.js library to create a 3D scene, set up the camera and renderer, and add the renderer's output to the HTML page; On the web browser, use Three.js to create a model of a robot or drone, and set their position, size, and materials; Step S1ii: On the satellite side, create a physical world using the Cannon.js library and set the parameters for gravity and the physics calculator; on the satellite side, create Cannon.js objects for the robot or drone respectively, set their shape, mass and initial position, and add the objects to the physical world. Step S1iii: Create satellite communication modules on both the web browser and satellite sides to enable data transmission and reception. The satellite communication modules use communication methods including radio, laser, or infrared, and perform signal processing based on the satellite's orbit and position, including modulation, encoding, encryption, transmission, decryption, decoding, and demodulation.