How make a complex mesh collision, keep all items at same position y Y axis:

Hi, i have this situation :

I tried Havok collision but the asteroid did it :

the problem is that my game is from top view, and all objects are at same position in Y axis :

so, the question is :

1- Can I use havok only to detect collisions and manage the consequences of the collision myself…?

note : if not, my plan B is create some RayCasting around of figther (and asteroid against cruiser) :

i think tha a solution with havok is more elegant/optimal …

If you control the position/orientation of all your objects, you might use Havok with Animated bodies (aka Kinematic) objects and then use shapecasting for collision detection.

https://doc.babylonjs.com/features/featuresDeepDive/physics/shapeCast

if everything is animated by the physics engine, maybe you can use some constraints to make sure everything stays at the same Y but this seems a bit complicated.

1 Like

Great, let me try to implement in my game :

yes, something like that. And when a proximity point is found, compare its distance to the center (top down projection) of the ship and its radius. if it’s < then you can add an impulse to compensate.

mm, why in this case the green sphere is not joined to a face …?

It’s not a collision. it’s a proximity shape cast. It computes the distance between 2 shapes, when < maxDistance.

yes sr, but the green sphere is not over a face correctly, like this :

image

in that case, this area will be marked as in collision…

Is the shape a convex hull?

this is a “convex hull” (the mesh with yellow wireframe)?

excuse me, I’m not familiar with that concept in English. (my native language is spanish)

It’s a convexhull in the PG:

const shape3 = new BABYLON.PhysicsShapeConvexHull(_mesh,scene);

Use a mesh shape instead.

Some infos on concave vs convex hulls : https://help.scribblemaps.com/hc/en-us/articles/11574387716365-Concave-vs-Convex-How-to-merge-polygons#:~:text=What%20is%20the%20difference%3F,beach%20ball%20or%20a%20basketball.

convex hulls are good for fast collision detection but in your case, you’ll need more detailed collisions because the ship mesh is very big compared to the player.

For the player, a convex hull is good enough.

1 Like

Shape cast has been introduced recently, after 7.0 release

oh, i was working with 6.31, with 6.49 it is ok…

Ho! I was sure it was done later :slight_smile:
glad to hear you’ve found a version that works for you !

However it is not working :

Ship Mamba :

class ShipMamba {
//....

setMesh(_mesh){
    // console.log( "player MAMBA en set mesh" );
    
    this.id_radar_screen = gb_radar_screen.register(this.alias);
      
    this.total_boosters=2;
    if( _mesh != null ){
      this.mesh = _mesh.clone("player_raptor_mamba");
      this.mesh_collider = scenes[this.parent_scene].getMeshByName("player_raptor_mamba.rx6_collider");
    }
    
    Bullet.setTypeBullet();

    //escudo 
    this.shield = BABYLON.Mesh.CreateIcoSphere(this.name+"_shield_mesh", {radius:this.shield_data.radio, subdivisions: 8},scenes[SC_PLAYING]);
    // this.shield = BABYLON.MeshBuilder.CreateSphere(this.name+"_shield_mesh", {diameter: 1, segments: SEGMENTS_SHIELD_ENEMIES}, scenes[SC_PLAYING]);
    this.shield.material = blue_shield_material;
    this.shield.isPickable = false;
    this.shield.setEnabled(false);
    
    // this.shield.position=this.mesh.position;
    this.shield.parent = this.mesh;
    this.pos_damage=new BABYLON.Vector3(0,0,0);
    this.falling=false;

    //proximity point
    this.mesh_collider.rotationQuaternion = new BABYLON.Quaternion(0,0,0,1);
    this.data_collider={actual:3,max:3};
    this.shape = new BABYLON.PhysicsShapeMesh(this.mesh_collider,scenes[this.parent_scene]);
    this.body = new BABYLON.PhysicsBody(this.mesh_collider, BABYLON.PhysicsMotionType.ANIMATED, false, scenes[this.parent_scene]);
    this.body.shape = this.shape;
    this.body.disablePreStep = false;
    this.hit_mesh = BABYLON.MeshBuilder.CreateSphere(this.name+"_hit", {diameter: 0.15}, scenes[this.parent_scene]);
    this.hit_mesh.parent = this.mesh_collider;
    this.hit_mesh.material = test_mat_green.clone("hit_col_mamba");
    this.cast_result = new BABYLON.ProximityCastResult();
  }//end of set mesh

collision(){
    // console.log( "en colision", this.name );
    //colision CON big player ? ... 
    if( big_enemy != null ){
      player.cast_result.reset();
      big_enemy.cast_result.reset();

      // Perform a shape proximity query with the first shape
      havok.shapeProximity(
          {
            shape: player.shape,
            position: player.mesh_collider.absolutePosition,
            rotation: player.mesh_collider.absoluteRotationQuaternion,
            maxDistance: 2
          },
          player.cast_result,
          big_enemy.cast_result
      );

      console.log( this.cast_result.hasHit , big_enemy.cast_result.hasHit );
      if (this.cast_result.hasHit && big_enemy.cast_result.hasHit) {
          console.log( "hubo colis" );
          const shapeLocalPos = this.cast_result.hitPoint;
          const worldHitPos = big_enemy.cast_result.hitPoint;
          
         
          
          // options.points[0] = shapeLocalPos.clone();
          // options.points[1] = shapeLocalPos.clone().add(hitWorldResult.hitNormal);
          // //options.points[1].normalize();
          // options.instance = lines;
          // lines = BABYLON.MeshBuilder.CreateLines("lines", options);

          this.hit_mesh.position = shapeLocalPos;
          big_enemy.hit_mesh.position = worldHitPos;
      }

    }//

  }//fin de colision
}


Cruiser ::

class Cruiser {
//.... 

setMesh(){
//....

//proximity point
    this.mesh_collider.rotationQuaternion = new BABYLON.Quaternion(0,0,0,1);
    this.shape = new BABYLON.PhysicsShapeMesh(this.mesh_collider,scenes[this.parent_scene]);
    this.body = new BABYLON.PhysicsBody(this.mesh_collider, BABYLON.PhysicsMotionType.STATIC, false, scenes[this.parent_scene]);
    this.body.shape = this.shape;
    this.body.disablePreStep = false;
    this.hit_mesh = BABYLON.MeshBuilder.CreateSphere(this.name+"_hit", {diameter: 0.15}, scenes[this.parent_scene]);
    this.cast_result = new BABYLON.ProximityCastResult();

    this.id_radar_screen = gb_radar_screen.register(this.alias);
    // console.log( this.name,", mi radar id es ",this.id_radar_screen );
    gb_radar_screen.show(this.id_radar_screen);


}
}

can you please repro in a PG?

I tried it with render mesh, because mesh_collider is a child of render mesh (I know it should be the other way around, that render mesh is a child of collider mesh)

Let me upload it on a pg.

Hi, here is placed :

It should look like your use case:

Many thanks, appears that it no have solution, i will try with rayCast.
( I need make collission bwtween 2+ custom meshes/colliders, basic shapes is not enought )