I wonder how could we access the volume an object occupies in space, I mean, the bounding box.
Haven’t found any example project and can’t really make pc.BoundingBox to work, don’t know how.
Thank you!
I wonder how could we access the volume an object occupies in space, I mean, the bounding box.
Haven’t found any example project and can’t really make pc.BoundingBox to work, don’t know how.
Thank you!
There is aabb
property on each meshInstance. You can create one AABB for entity, and then iterate through each meshInstance and do add
it to main model.
Here is some snippet (written in post, not tested):
// initialize
this.aabb = new pc.BoundingBox();
// once model is available (if preloaded, then straight away)
var meshes = this.entity.model.model.meshInstances;
if (meshes.length) {
this.aabb.copy(meshes[0].aabb);
for(var i = 1; i < meshes.length; i++)
this.aabb.add(meshes[i].aabb);
}
I achieve creating the bounding box, but fail to add it to the main model. when I call
this.aabb.copy(mesh.aabb);
I recieve an error "cannot read property ‘center’ of undefined
Looks like mesh.aabb
is undefined.
Please give a link for a script file you having trouble in.
I was doing some test in this project: https://playcanvas.com/project/360718/overview/bancopruebas1 (the animateCC scene)
This would be the script:
pc.script.create('pruebaBounding', function (app) {
// Creates a new PruebaBounding instance
var PruebaBounding = function (entity) {
this.entity = entity;
};
PruebaBounding.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
this.aabb = new pc.BoundingBox();
var mesh = this.entity.model.model.meshInstances;
this.aabb.copy(mesh.aabb);
this.aabb.add(mesh.aabb);
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
}
};
return PruebaBounding;
});
As you said, mesh.aabb is undefined, y have an error when accesing it’s “center” property in the console.
this.entity.model.model.meshInstances
- is an array, that has meshes inside of it. You are trying to get aabb
from array.
For ease of debugging, try using breakpoints in Sources tab in Dev Tools in Chrome/Firefox. This allows you to step through your code while it is running, and inspect variables and scopes. That way you can find out what is what.
Additionally, just doing this:
console.log(mesh);
, would print to console value of mesh
, so you could see what you are dealing with.
Dev Tools in browsers, today are really useful and powerful for debugging purposes, please use them!
LOL, yeah, you’re right!
Anyway, I copied a wrong version. This is the one:
crearBoudingBox: function(entity){
// initialize
var aabb = new pc.BoundingBox();
// once model is available (if preloaded, then straight away)
var meshes = this.entity.model.model.meshInstances; <- Esta es la linea
if (meshes.length) {
this.aabb.copy(meshes[0].aabb);
console.log(meshes);
for(var i = 1; i < meshes.length; i++)
this.aabb.add(meshes[i].aabb);
}
And the error:
Error: AlgoritmoAestrellaMejorado.js:1147 Uncaught TypeError: Cannot read property 'model' of undefined
We 'console.log’ed the entity inside the function, so entities are getting inside, but when we access meshes inside model we’re getting ‘undefined’
Sorry for being late to aswer, hard week!
Ok, so let’s be a bit more specific
crearBoudingBox: function(entity){
//1st part
console.log(entity.getName());
console.log("Posicion del objeto "+entity.getLocalPosition());
// initialize
var aabb = new pc.BoundingBox();
// once model is available (if preloaded, then straight away)
var meshes = entity.model.model.meshInstances;
if (meshes.length) {
1º part: entity enter correctly and we can access it’s data
//2nd part
console.log(meshes[0]);
aabb.copy(meshes[0].aabb);
for(var i = 1; i < meshes.length; i++)
this.aabb.add(meshes[i].aabb);
}
2º part: In the’meshes’ variable, it seems that initialises with defautl data instead of initialising with entity.model.model.meshInstances info. It has always ‘zero’ values at max, min, center etc…
//3rd part
console.log(aabb);
return aabb;
3º part: When you have a mesh with default values, aabb variable generates always with the same Max Min values (0.5,0.5,0.5) and the opposite. As I said, (0,0,0) for center. Always
This is the project, https://playcanvas.com/editor/scene/424939, the ‘Algoritmo A* mejorado’
EDIT: Mistaken Link!!!
Your code is a bit overly large, and hard to go through.
I’ve created small example project, with one script file, so you can go and learn from it.
Let me know if that clarifies it.
Yes… and in fact in spanish, sorry about that, haha
Thank you max!