Jump to content

IntersectMesh Trouble


DylanD
 Share

Recommended Posts

Hi again everyone, 

I have an abstractMesh that is loaded in through the scene loader.importMesh function.  So from what I understand I get an array of abstract meshes from this function, which I then use to position the actual mesh and set its properties.  I want to save one piece of that mesh (3d model right? yea..) and use it for mesh intersection (my cheap man's collision, which is great btw not trying to belittle it, but it is the easiest collision to use) so I have a public variable in typescript,  that is attached to its class lets call it the player class and the variable it the playerBody variable, I was to use that body variable later in a totally different class by calling It through an instance of that class, the same instance I used to import the mesh.  However whenever I try to use it with the import mesh it says the variable is underfined.

So here a bit of what the code looks like:

 



var importFunc=(meshes:BABYLON.AbstractMesh[])=>{
           this.player = meshes[3];//this.player is an abstractMesh public on my class player
        }


BABYLON.SceneLoader.ImportMesh("","./models/","player.babylon",this.scene,function(meshes){
            importFunc(meshes);   
        });

so my questions are:

1: Why won't the this.player point to meshes[3]?

2:Even if I do get the abstractMeshes pointed correctly will I be able to use mesh.intersectsMesh() with and abstract mesh and a regular mesh (e.g. a box mesh)?

Any ideas?

Thanks for your time!

Link to comment
Share on other sites

Hi There,

1. I'm not sure if "this.player" references the proper 'this' object you had in mind.

That could be a reason why you don't see your player mesh to be set?

I often use a temp var that references the 'this' of the outer variable scope when I want to use that 'this' from the outer variable scope inside some local scope such as a function body.

Example:

var that = this; // remember this so we can use inside another local scope like below

var importFunc=(meshes:BABYLON.AbstractMesh[])=>{
           that.player = meshes[3];//that.player is an abstractMesh public on my class player
        }

The general programming concept that you want to understand here is called ' variable scope '.

2. My experience is that BABYLON.Mesh (and probably also an abstract mesh) don't have a size. So I neither get a positive collision/intersect result when this type of mesh intersects another Box for example.

My solutions for that are:

- to don't use BABYLON.Mesh but use a Box or a Sphere with some size in which the full model fits (effectively giving the mesh a "size"). it's simple and fast, and most of the time very effective.

- or to recompute and set the bounding box myself on the Mesh by traversing the child / sub meshes of the model. it's a bit more complex and you probably want to recompute if your player physique changes much (e.g. stand, sit, lie down)

- or to have intersection not on the main (abstract) mesh but on some child mesh which has a bounding box / volume already and thereby benefits from the Babylon engine math already, e.g. the "chest" of the player.

You can show the bounding box with an option:

mesh.showBoundingBox = true;

It works great for debugging your intersections, at least it does so for me when for some reason my players walk through walls and hills while they shouldn't ?

Hope my suggestions help you find the solution to your specific problem.

Happy coding!

Link to comment
Share on other sites

This is probably because you are using function(meshes){ importFunc(meshes); }) instead of  (meshes)=>{ importFunc(meshes); })

In the first case this represents the calling context whereas in the second the current one is passed through.

BR,

Link to comment
Share on other sites

On 6/16/2018 at 7:40 PM, Sebavan said:

This is probably because you are using function(meshes){ importFunc(meshes); }) instead of  (meshes)=>{ importFunc(meshes); })

In the first case this represents the calling context whereas in the second the current one is passed through.

BR,

Hey Sebavan, this did not seem to change anything.

 

On 6/16/2018 at 7:14 AM, dbawel said:

Have you defined "meshes" as a constant or any type of variable?

DB

 Hi again drawl, no I hadn't defined meshes as a constant or any other type of variable(its only ever used in that function parameter).

 

On 6/16/2018 at 7:10 AM, QuintusHegie said:

Hi There,

1. I'm not sure if "this.player" references the proper 'this' object you had in mind.

That could be a reason why you don't see your player mesh to be set?

I often use a temp var that references the 'this' of the outer variable scope when I want to use that 'this' from the outer variable scope inside some local scope such as a function body.

Example:


var that = this; // remember this so we can use inside another local scope like below

var importFunc=(meshes:BABYLON.AbstractMesh[])=>{
           that.player = meshes[3];//that.player is an abstractMesh public on my class player
        }

The general programming concept that you want to understand here is called ' variable scope '.

2. My experience is that BABYLON.Mesh (and probably also an abstract mesh) don't have a size. So I neither get a positive collision/intersect result when this type of mesh intersects another Box for example.

My solutions for that are:

- to don't use BABYLON.Mesh but use a Box or a Sphere with some size in which the full model fits (effectively giving the mesh a "size"). it's simple and fast, and most of the time very effective.

- or to recompute and set the bounding box myself on the Mesh by traversing the child / sub meshes of the model. it's a bit more complex and you probably want to recompute if your player physique changes much (e.g. stand, sit, lie down)

- or to have intersection not on the main (abstract) mesh but on some child mesh which has a bounding box / volume already and thereby benefits from the Babylon engine math already, e.g. the "chest" of the player.

You can show the bounding box with an option:


mesh.showBoundingBox = true;

It works great for debugging your intersections, at least it does so for me when for some reason my players walk through walls and hills while they shouldn't ?

Hope my suggestions help you find the solution to your specific problem.

Happy coding!

Hey there QuintisHegie, I gave these a try and I have decided making a invisible sphere is my best option.

 

Thanks for the help everyone!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...