Jump to content

intersectMesh with moveWithCollisions


Kilombo
 Share

Recommended Posts

Good afternoon,

 

I'm trying to check intersection between 2 meshes. For that I created 2 bounding boxes. When the intersection happens, the NPC should pursuit the ship.

Although, it's not working, for some reason, the NPC is behaving like it had intersedted all of the meshes in the screen at the same time (like if the bounding box had infinite size.

there's part of the code.

 

activate is called on the main scope, The example his checking the intersection between the this.boundingbox and the playermesh, but i already tried with the boundingboxship (of the player), and tried with "false" also, it doesn't work, with false doesn't detect anything at all.

 

I also commented out all the collision stuff, including the ellipsoids, still nothing.

 

Thanks for the help in advance.

 patroller.prototype.activate = function(){        var a = this.units.length - 1;                console.log(this.arrived);        if (this.arrived === true){            var tempx = Math.floor(Math.random()*(this.mapSize-1+1)+1);            var tempz = Math.floor(Math.random()*(this.mapSize-1+1)+1);            this.targetX = tempx;            this.targetZ = tempz;            this.arrived = false;        }            //estado patrol                if ((this.arrived === false) && (typeof(units[a].getMesh())!=="undefined")&&(typeof(this.mesh)!=="undefined")){                //this.npcPosX = temp.getPositionX();                //this.npcPosZ = temp.getPositionZ();                this.move(this.targetX,this.targetZ);                //var meshtemp = units[0];                //console.log(units[0].getMesh());                //console.log(meshtemp.mesh);                for (var i=0; i<this.units.length; i++){                  if (typeof(this.units[i].getMesh())!=="undefined" ){                      //console.log(units[i].getMesh());                    //console.log(this.mesh);                    //console.log(meshtemp);                    //console.log(this.boundingBox);                    //console.log(units[i].boundingBoxShip);                    if (this.boundingBox.intersectsMesh(this.units[i].getMesh()),true){                        console.log("funciona");                        this.attackedUnit = this.units[i];            //estado de perseguição                        this.move(this.units[i].getShipPosx(),this.units[i].getShipPosz());                        var targetx = units[i].getShipPosx();                        var targetz = units[i].getShipPosz();                        var posix = this.getPositionX();                        var posiz = this.getPositionZ();                        var dx = targetx - posix;                        var dz = targetz - posiz;                        var vector = Math.sqrt(dx * dx + dz * dz);                        if (vector<=25){            //estado de ataque                            //attack ();                        }                    }                    if (this.armor<5){            //estado de fuga                        this.move(Math.floor(Math.random()*(this.mapSize-1+1)+1),Math.floor(Math.random()*(this.mapSize-1+1)+1));                    }                   }               }
Link to comment
Share on other sites

That's what I'm using (at least that was the example, that I saw to arrive at this state). Well.... problem is, I've got already collisions working. So I would have to change everything again to use it, and would loose the collisions.

The point of this is to create one reactive agent, that pursuits players, attacks, etc, like one NPC. It's not generalized (since it doesn't have the Y axis), but it's a reusable class, so when i finish it i'll share it (if some one want's it, cause my programming is not that good, lol ).

 

Thanks,

Link to comment
Share on other sites

Nope it's not possible because of the meshes. Although, I didn't find any example on intersections with moveWithCollisions. If you could tell me the basics, or where to find one example that would be very helpfull :D

Link to comment
Share on other sites

Does this help somehow? http://www.babylonjs-playground.com/#BMHQA#1

This is a kind of a simulation of moving with collisions - when the objects collide, the 2nd box stop moving. then the first box "makes room". and the 2nd box can keep on going. Very simple, very effective :)

 

But it is not too effective if you have many meshes that are being inspected against many meshes. You will need to program a kind of "proximity" inspection and only then inspect mesh interaction. 

 

Having said that - it is actually not hard adding an "onCollide" callback to the moveWithCollisions function. Let me check. But this will only come in 2.2, so maybe the first example would work better :)

 

So, didn't take too long - http://www.babylonjs-playground.com/#BMHQA#4

This is a hack (at the moment, i'll soon PR it after cleaning it up a bit), but this adds an on-collide callback to the moveWithCollisions of box2. Check your console, you will see that the box collides once and then stops with the mvoeWithCollision (no need, it collided :-) )

Edited by RaananW
Link to comment
Share on other sites

Thanks mate, it helped me. but....

 

When you have meshes imported from blender. I understand that we should parent a box or a sphere to it. And the problem looks to be in there, I'm probably doing i it wrong or checking the intersection in the wrong place.

Because i wasn't beeing able to work it out, i tried with the OnIntersectionEnterTrigger, he was doing the same. Instead triggering when the collision occurs, he started imidiatly pursuing the other mesh (he should be patrolling, and if it finds, he pursues), so... I thought, wrong place, it's on the render loop, let me put it on the import mesh method, now he just patrolls, he doesn't pursue at all (he doesn't trigger the collision).

 

Thanks anyway, and sorry for beeing a pain....

Link to comment
Share on other sites

I believe you could use the moveWithCollisions with those meshes. It is using the mesh's ellipsoid and the other mesh's BoundingBox (first) to check for intersections. So no need for parent spheres or boxes. But it would work with parent meshes as well, of course. 

 

Oh, and - we are here only because we want to (all except for one :-) which REALLY wants to). You can't be "a pain" by asking good questions that help everyone else as much as they help you. If we wouldn't enjoy doing that, we wouldn't be here! I guess... So, no apologizing, and there are no stupid questions (just stupid answers).  

Link to comment
Share on other sites

Here we go...

You see, I can't work it out.

 

If you check on the image, the first lines says "funciona" means it works..... but it shouldn't because has you can see on the other lines, that shouldn't be happening.

In the next 3 lines, we have the destination cords (the cords of the ship that we can see in the image), next you have the originX of the mesh that should already have intersected the ship.... impossible, because on the last line there's the mesh patroller scale (200,200,200), now 200+2580 is 2780, no way it intersects something on 23000.

 the intersectMesh is set to True, because on false he doesn't detect anything.... I tried everything. If some one has any ideas, I'll be glad to hear them. I've got stuck...... :(

 

post-8187-0-89128800-1435362152_thumb.pn

post-8187-0-07507300-1435361744_thumb.pn

Link to comment
Share on other sites

And... no answers :)

 

I think there is no way out of seeing some code in that case. Doesn't have to be in the playground, just with a (preferably) non-minified version of babylon and some comments. This way we can all understand what exactly you mean and  we could debug and help.

Think it will be possible?

Link to comment
Share on other sites

I will do better then that, you can log in in the web page and check it out for your self. the user and password for testing is xpto

You have the mainengine.js, the patroller.js and the unit.js.

mainengine - calls everything, and all the stuff.

patroller - it's the class for the NPC's (the one that is not working), to put one working you just have to instantiate it like npc = new patroller(args);

unit - exactly the same thing.

 

Dont get scared with the portuguese comments on patroller, it has to be that way it's for a scholl project. but all the vars and the methods are pretty understanble.

 

Thank you for your time RaanaW, you will probably save my grade :)

 

Almost forgot, the website is, www.andromedaquest.com

Link to comment
Share on other sites

I can't help but laugh a bit, right here, at this point in the thread.  :)  (Wingy hands Raanan a moist towelette and a bottle of aspirin)

 

oh Kilombo... you are endangering Raanan's sanity, you know?  sigh.  I shouldn't butt-in, should I?  sigh.

 

K, why can't you use FEW mesh, and simple mesh, preferably non-Blender mesh, and then show us your "pursue" and "patrol" code... in the most simple form you can, in the playground?  To ask a BJS helper... to dive into your FAT project... especially if you are poor at code-organizing and commenting... man.  Gruesome.

 

This forum is PACKED with information and issues... regarding moveWithCollisions.  There are probably 50 20 moveWithCollisions tests/experiments/demos, PG and not.  If you can't find them via a forum search for moveWIthCollisions, then you are just plain blind.  Proof of concept is #1.  Small test, make sure it's bulletproof, then scale it up with fat mesh and many of them, as wanted.  But to troubleshoot an algorithm (a concept), keep it tiny.  Show us patrol and pursue... in the playground.  You claim you cannot, but you actually can, once you scale back down to concept level.  (remove the fat from the problem area so we can find it easier)

 

Ok, sorry for butting-in, but somebody had to say it.  :)

Link to comment
Share on other sites

Yap, you are right mate, but... although the playground is a really great tool, when it comes to imported meshes and multiple files it becames impossible to show something, .... and no... unfortunately there's not so many topics and demos about it.

Of course, my code is a crap, and since i'm in a short time... it's becomming to look "patched" and confuse (even for me), lol

I already understood that I have to sort this one out on my one, it's a long shoot for someone to get my code.

Any way,... Every help that appears is a good help and make me stay more near of my objective :) (like in the other topic, that stuff really helped me out, and i think i'm probably more near of the solution that i was before.

Link to comment
Share on other sites

Well, that's an excellent attitude... cool.  I understand, and I've been there myself.  Even for your own hacking... you know... scrape-off as much fat as you can, and the situation sometimes improves.  You know this already.

 

Pursue and patrol... that is not easy coding  (well at least not for me).  The "ai" could get pretty complicated.  Share your experiments with us when you get a chance... if the code isn't secret.  Doing pathFollowing?  I would imagine so.  phew.

Link to comment
Share on other sites

Thanks mate, I'll surely share it, that's the point :)

But i do feel that's something missing, a callback or something. You see when we put the intersection method in the renderbeforerender, it seems to work it out, but we also have to put the movement in the renderbeforerender, and that is the question..... how do you call the moveWithCollision ? Because it doesn't seem to work, but maybe I'm wrong (again, lol). I wonder if someone already used this with moveWithCollision and with the importMesh at the same time.

Link to comment
Share on other sites

Ahoi!

 

Wingnut, that was one honest post  :D

So, I too was trying to go through the code a bit - this is a bit over my head. I shall keep my sanity!

But I did play around with moveWithCollisions and the importMesh:

http://www.babylonjs-playground.com/#PNDJG#1

Look at those two skulls rubbing at each other... Now, the thing with mvoeWithCollisions is that it can be awesomely tricky to get it right. The collisions are inspected with the ellipsoid od the colliding object and the REAL mesh of the collided object. Meaning - setting the  ellipsoid of s1 (as in Superduperskull nr. 1) won't do a thing if we call moveWithCollisions on s2, s2's ellipsoid is all that matters regarding s2. So s2 could be a box for all that it matters.

Setting the ellipsoid is another interesting thing - clarifying why I found the value would take a while (maybe I will write about it when I have time), but it has to do with the object's bounding box. Sadly the object is not centered so I couldn't simply take the bounding box's size. Play with the values and you will understand how crucial it is to "get it right".

 

And, finally - maybe try isolating the case using two objects in your scene and slowly work it out. If there are two objects and the code is a bit more structured, I am prettz sure a lot here would be happy to help. Because we love code and love seeing how other people use the framework, but most of all, we like helping out! 

Link to comment
Share on other sites

No solution yet but I cleaned the playground a bit... did proper code formatting, removed some small syntax "errors" (nothing serious as far as I can tell), disabled a few things that I thought are not that important, set the camera in a direction that you can see what's going on right from the start and added some color ... just because it's fancy ;)

 

I don't have too much time at the moment, but I think you are doing something really wrong here:

if (boundingBox.intersectsMesh(box), true) {    console.log("works");    this.targetX = box.position.x;    this.targetZ = box.position.z;}

I might be wrong and it's some fancy way to use an if condition, but I actually don't think that the condition is correct like this. You probably mean if (boundingBox.intersectsMesh(box, true)).I tried and replaced if (boundingBox.intersectsMesh(box), true) with if(true) just to see whats happening and it still works the same. That means for me that your condition is always true and the intersectsMesh gets ignored. If I have time later I think I can fix that somehow, I just have to figure out what exactly you are doing there. If somebody else is faster, good, too ;)

Link to comment
Share on other sites

Wow, that's a great cleanup :)

http://playground.babylonjs.com/#UFRH2#6 - here are my two cents.

iiceman is right - this if is totally wrong. I assume the true was supposed to be inside the box. Removing it and putting it outside the if/else scope make it actually do what (I assume) you wanted it to do.

Another thing - the playground doesn't require you to initialize the engine and canvas - it is all done for you. 

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...