Is there a way to determine if the X and Z coordinates are colliding with a specific mesh in Three.js without relying on mouse input?

Background Context

I'm currently developing a strategy game that combines elements from both Europa Universalis 4 and Age of Empires 3. The game is being created using JavaScript and the Three.js (r109) library. At this stage, I have successfully implemented randomly generated low-poly terrain with trees and realistic water reflections. In the initial stages of the game, I aim to spawn a Navy unit in the form of a galleon (as shown in the screenshot below). My goal is to ensure that when this unit spawns, it does so at a random location within the boundaries of the water. The water surface is depicted by a semi-transparent plane covering the entire map area, supported by a THREE.Reflector object below it. While the terrain also consists of a plane, its appearance has been altered using a SimplexNoise heightmap.

https://i.sstatic.net/lhzJO.png

The Query

How can I effectively identify whether an x,z position intersects with the water mesh rather than the terrain mesh? While I believe THREE.Raycaster could be beneficial for this task, I am curious if there might be a more optimized solution available. If utilizing THREE.Raycaster is indeed the ideal approach, how should I proceed with implementing it specifically for this scenario? Is it necessary to create separate instances of THREE.Raycaster for each object involved? Note that my objective is not to place this object using mouse input; instead, I intend to determine its placement through a method that assesses the position mentioned earlier.

Answer №1

Providing specific guidance without any insight into your code is challenging, but it appears that creating a collision list for valid water surfaces and verifying it when spawning something may be the solution.

You can view a basic jsfiddle here. This example establishes a "land" mesh (green) and a "water" mesh (blue), includes the "water" mesh in a variable named collisionList, and then triggers a spawn function for coordinates spanning across both surfaces. The function employs a raycaster to determine if the coordinates overlap with the "water" mesh and creates a red cube accordingly.

Below is the provided code snippet:

window.onload = function() {
    // code implementation
}
// additional instructions...

The effectiveness of this method largely hinges on the game's overall design. For procedurally generated worlds, it might be more efficient and error-proof to establish spawn points (and other key components) beforehand to develop the geography consistently rather than retroactively.

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

Locate the index and value of the item that has the most recent date

Exploring my API design const dataArr = [{ end_date: '2019-12-16' }, { end_date: '2018-12-16' }]; const latestEntry = dataArr.filter( /* Seeking the index of the item with the most recent date */ ); Interested in vanilla JavaScript ...

Inability to input text into a textbox with AngularJS

I am currently working on developing an AngularJS app. I have encountered a problem where I am unable to input text into a textbox. The issue lies with my zoomService which handles zoom increments and decrements. While the user can zoom using a slider and ...

Click twice on the editable AngularJS table to wrap each item

As a new learner of Angularjs, I found an existing code example and decided to customize it. My goal was to enable double-click editing for each li item. However, after adding one more li, the functionality did not work as expected. <li ng-dblclick="st ...

Uploading files and data in Laravel using Vue.js and Vuetify: A step-by-step guide

My Vuetify form is working fine with text input, but when I try to include images, I encounter the error GET http://danza.test/thumbnails[object%20File] 404 (Not Found). How can I successfully pass both text and images in a form? This is part of the code ...

Is there a way to make a NodeJS Transform stream produce multiple records for each input chunk?

Currently, I am working on a Transform stream that has the capability to receive an incoming stream of XML data and then emit a subset of that in the form of JavaScript objects. <item> <name>some name</name> <files> <fil ...

Can a websocket be used to communicate with a server that is not the same as the one hosting the html5 website?

My goal is to establish communication between a hardware device and an HTML5 website. It seems like the best way to do this would be through WebSockets or possibly WebRTC over a WiFi network. Is that correct? I haven't come across any solutions invol ...

When using ngClick with a parameter, the parameter is not being successfully passed

My table resembles a tree structure with two ng-repeats. <table> <tr ng-repeat-start="am in anArray"> <td><button ng-click="TheFunction(am)"></button></td> </tr> <tr ng-repeat-start="em in anotherArray"> < ...

Breaking apart a mesh using THREE.js

Can a single THREE.Mesh be divided into multiple meshes? For instance, can a mesh with 2,000,000 polygons be split into 2,000 meshes each with 1,000 polygons? Edit: Realistically, it may not be possible to retain the exact same number of polygons/vertice ...

Unexpected error: the process is not recognized

I am currently working with node.js to develop a web application. Upon running the application (either by opening index.html in the browser or executing "npm start" in the terminal), I encounter two errors: Uncaught ReferenceError: process is not defined ...

How can AJAX be utilized to show search results dynamically?

At the moment, I have a PHP page that pulls a list of results from my SQL database and displays the ID and names. There is a search bar at the top where you can search for keywords in the "name" field, and when you click on search, it takes you to a new p ...

When navigating back to the Homepage from another page, React displays the Homepage

Recently, I started learning React and following the Traversy crash course along with an additional video on React router 6+. My route setup looks like this: import { BrowserRouter as Router, Route, Routes } from 'react-router-dom' return ( &l ...

Implementing a list using display: inline-block without any specified order

Currently, I am immersed in a project that involves simulating an input using HTML and CSS. This input should be capable of executing a function like: my_cool_function(param0, param1, param2, param3). To accomplish this, I have constructed an unordered lis ...

Prevent the bottom row from being sorted

I have implemented sortable table rows in my angular project, however the sorting functionality also affects some query-ui code elements. Now I am looking to exclude the last row from being sortable. HTML <div ng:controller="controller"> <ta ...

Attempting to minimize the number of jQuery calls within a Domino XPage

Trying to incorporate the 'DataTables' table plug-in for jQuery into a basic Domino XPage. Successfully loaded required libraries from CDN sources... JQuery: ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js DataTables: cdn.datatables. ...

I am struggling with sending post requests in Node.js

I am currently facing a challenge with handling form data from a webpage using Node.js and writing that data to a file. It seems like there might be an issue with how Node.js is processing my POST request, or perhaps the way I am sending the request from t ...

`I'm having difficulty transferring the project to Typescript paths`

I posted a question earlier today about using paths in TypeScript. Now I'm attempting to apply this specific project utilizing that method. My first step is cloning it: git clone <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cf ...

Utilizing the HTML5 <video> element to enable pausing the video when navigating to the next slide in BXSLIDER

Is there a way to easily stop an HTML 5 video from playing when you click on a next button? I've tried searching on Google but couldn't find a solution that works. I have implemented bxslider for a client on the about page . If you scroll down, ...

Using Next.js for dynamic routing with JSON arrays in getStaticPaths

I am currently facing an issue while trying to incorporate a json file with multiple arrays into my Next.js application. The structure of the json file is quite complex, and I'm unsure about how to convert it into a format that can be effectively util ...

Adjust the color of static hyperlinks based on the background color

Is there a way to dynamically change the color of my fixed side links based on the background color when scrolling? I attempted to use CSS mixed-blend-mode: difference, but it does not provide the level of control I need. Therefore, I am looking to achieve ...

Button-click scrolling is our featured feature!

One interesting feature on my website is a button (within a div-element) located in the bottom-right corner. I am now looking to enhance this by adding a jQuery function that enables the user to scroll down the page incrementally simply by clicking and hol ...