The imported mesh is failing to interact with other objects - Babylon.js

I'm currently facing an issue while trying to configure a 3D environment in Babylon.js. During testing, I used meshes generated through code.

However, we aim to utilize blender models for the actual environment. When I import a mesh using the scene loader, it simply falls through the floor.

The structure involves running a scene.vue script, which then renders the Floor.vue and Brick.vue components.

Can anyone explain why my cube is still falling through the floor?

Scene.vue

<template>
  <div>
    <camera v-if="scene" v-bind:canvas="canvas" v-bind:scene="scene"/>
    <floor v-if="scene" v-bind:scene="scene" v-bind:canvas="canvas"/>
    <brownie v-if="scene" v-bind:scene="scene" v-bind:canvas="canvas"/>
  </div>
</template>
<script>

import * as BABYLON from 'babylonjs';
import Camera from './Camera.vue';
import Brownie from './Brownie';
import Floor from './Floor';

export default {
  name: "Scene",
  components:{
    Camera,
    Floor,
    Brownie,
  },

  props: ["canvas"],
  data(){
    return {
      engine: null,
      scene: null
    }
  },

  mounted() {
    this.engine = new BABYLON.Engine(
        this.canvas,
        true,
        {
          preserveDrawingBuffer: true,
          stencil: true,
        },
    );

    this.scene = new BABYLON.Scene(this.engine);
    this.scene.enablePhysics();
    this.scene.collisionsEnabled = true;
    new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), this.scene);


    this.engine.runRenderLoop(() => {
      this.scene.render();
    });
  },
}

</script>

Floor.vue

<template>
  <div>
  </div>
</template>

<script>
import * as BABYLON from 'babylonjs';

export default {
  name: "Floor",
  props: ["scene", "canvas"],

  methods: {
    generateFloor(){
      let floor = BABYLON.MeshBuilder.CreateGround('floor', {width: 50, height: 50});
      floor.position = new BABYLON.Vector3(0, 0, 0);
      floor.physicsImpostor = new BABYLON.PhysicsImpostor(floor, BABYLON.PhysicsImpostor.BoxImpostor, {mass: 0, friction:0.1, restitution:0.8}, this.scene);
    }
  },

  mounted() {
    this.generateFloor();
  }
}
</script>

<style scoped>
</style>

Brownie.vue

<template>
  <div>
  </div>
</template>

<script>
import * as BABYLON from 'babylonjs';
import 'babylonjs-loaders';

export default {
  name: "Brownie",
  props: ["scene", "canvas"],

  methods: {
    generateBrownie(){
      // let testObj = BABYLON.MeshBuilder.CreateBox('floor', {width: 1, height: 1});
      // testObj.position = new BABYLON.Vector3(5, 10, 0);
      // testObj.physicsImpostor = new BABYLON.PhysicsImpostor(testObj, BABYLON.PhysicsImpostor.BoxImpostor, {mass: 1, friction:0.1, restitution:0.8}, this.scene);

      BABYLON.SceneLoader.ImportMesh("", "./", "cube.glb", this.scene, (newMeshes) => {
        let mesh = newMeshes[0];
        this.scene.meshes.forEach(mesh => {
          mesh.checkCollisions = true;
        });
        mesh.position.x = 0;
        mesh.position.y = 2;
        mesh.position.z = 0;
        mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.BoxImpostor, {mass: 1, friction:0.1, restitution:0.8}, this.scene);
        this.scene.mesh.checkCollisions = true;
      });
    }
  },

  mounted() {
    this.generateBrownie();
  }
}
</script>

<style scoped>
</style>

Answer №1

To enable collisions for the floor in your Floor.vue file, you can follow a similar approach like the one shown below:

methods: {
    generateFloor(){
      let floor = BABYLON.MeshBuilder.CreateGround('floor', {width: 50, height: 50});
      floor.position = new BABYLON.Vector3(0, 0, 0);
      floor.physicsImpostor = new BABYLON.PhysicsImpostor(floor, BABYLON.PhysicsImpostor.BoxImpostor, {mass: 0, friction:0.1, restitution:0.8}, this.scene);
      floor.checkCollisions = true;
    }
  },

Implementing these changes should enable collisions for your floor. Let me know if you have any questions!

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 the syntax for creating a zip function in TypeScript?

If I am looking to create a zip function: function zip(arrays){ // assume more than 1 array is given and all arrays // share the same length const len = arrays[0].length; const toReturn = new Array(len); for (let i = 0; i < len; i+ ...

Executing a TypeORM query with a distinct clause that ignores case sensitivity

I am attempting to construct a TypeORM query builder that pulls data from a postgresql database to retrieve all unique names. Here is how my query currently looks: names = await this._context.manager .getRepository(Names) .createQueryBuilde ...

Using JavaScript to Show Variables When Clicked

I have been working on populating multiple divs with data stored in variables that change when an image is clicked. Here's a snippet of the code within my tag:</p> <pre><code>function displayName(name){ document.getElementById( ...

What is the best way for OnSubmit to access an external file?

Recently, I adjusted this code snippet to include the onsubmit attribute with a validation function call. Instead of having the validate function within the same file, I moved it to an external file for better organization. Now, I am wondering how do I e ...

Tips for interacting with the DOM in an Angular 4 application

I am trying to call the addItems method, but I keep getting an error: Uncaught TypeError: this.addItems is not a function Currently, I am using Angular 4 along with jQuery and the fullpage.js library. page-content.component.ts import { Component, OnI ...

Sending a request to a JSON file using Ajax

I have 2 questions: 1. I am trying to run this file, but it is not giving any errors or showing results. Please help me identify the problem. 2. I added a dropdown menu in my HTML file, but I'm unsure how to use it to display a list of names. Any sugg ...

The processing indicator fails to function properly when trying to refresh a jQuery datatable

I am currently customizing the loading indicator for the datatable using a spinner called startLoadGlobal(SPINNER_DATA_TABLE_FINANCEIRO_CARREGAR_REGISTROS) in jQuery. It works correctly when I initially load the datatable, however, when I reload it, the sp ...

Versioning resources in Spring MVC with Thymeleaf

https://i.sstatic.net/ArllV.png Struggling with resource versioning in Spring Mvc 4 while using thymeleaf template engine. Despite the code provided, I am unable to see the new version URL when viewing the page source. What could be causing this issue? Wh ...

Distinguishing each unique JavaScript property within an array of objects

I've been struggling with this problem for quite some time. I have an array of objects, focusing on the "00" object at the moment, and I am trying to group together the bestScore properties in a specific way: .. User Group apple .. User Group ba ...

What do you call a function that serves no purpose?

Consider a scenario where you have a function defined as: function FunctionT(){ //do something } When describing this function, would you classify it as empty, undefined, or can either term be used interchangeably? Is there a specific designation for thi ...

Load custom JS with Google

I have integrated the Google Ajax API and now I need to load custom javascript that relies on the libraries loaded by the ajaxapi. What is the best way to accomplish this? ...

Comparing getElementById with $('#element') for retrieving the length of an input field

Consider the following scenario: <input type="text" id="apple"> Why does the first code snippet work? $(document).ready(function () { alert($('#apple').val().length); }); However, why does the second code snippet not work as expecte ...

Enzyme locates and chooses the initial occurrence of an element

I am attempting to simulate a click on the initial box out of three. My React script appears as follows: const Boxes = (props) => { return ( <div className="container"> <div onClick={props.showMessage} className="box">One& ...

`Zooming and scrolling feature within a masked image`

I'm struggling to achieve a scrolling zoom effect similar to the website mentioned below, but I can't seem to get it to fully zoom. Additionally, when I try to zoom in on a clipped shape or text while scrolling, the entire div ends up scrolling ...

What is the best way to transform an array of strings into a JSON object?

When navigating back to a list page, I want to make sure that the filter criteria are still retained. My solution involves using cookies, which are set when the form filter is submitted. In the list page's mounted hook, I retrieve the specific cookie ...

Combining two sets of data into one powerful tool: ngx-charts for Angular 2

After successfully creating a component chart using ngx-charts in angular 2 and pulling data from data.ts, I am now looking to reuse the same component to display a second chart with a different data set (data2.ts). Is this even possible? Can someone guide ...

What is the best way to incorporate the parallax effect into a v-carousel?

Currently, I have a "v-carousel" containing multiple images and now I am looking to incorporate a parallax effect into it, similar to "v-parallax". <v-carousel cycle height="600" hide-delimiter-background show-arrows-on-hover> <v-carousel-i ...

Retrieving package information from NPM using a web browser

I am currently developing a Chrome Web App that retrieves information from NPM. However, I have encountered issues due to Chrome adhering to the Access-Control-Allow-Origin flags set by websites. While I am able to access the following URL: http://regist ...

What is the best way to turn off autocorrect in a textarea on IE11 without turning off spellcheck?

In my experience, disabling the spellcheck attribute seems to solve the auto-correct issue, but it also eliminates the underlining of misspelled words. <textarea id="TextArea1" spellcheck="false"></textarea> I prefer to keep spellcheck enabl ...

Several dropdown menus within the same container, working independently of each other

I recently encountered an issue with a drop-down navigation bar. When I have multiple drop-downs and click on one, it opens as expected. However, if I then proceed to click on another drop-down while the first one is already open, both of them remain open ...