[SOLVED] How do I trigger the collision field continuously

Hello my dear teachers,

The title might be a little weird, I didn’t know exactly what to write, I apologize in advance for that.

Here is my problem, I’m getting interaction of 2 different objects that don’t have solid body. However, although it works correctly the first time, it stops working correctly the second time and does not give any response. This might be a problem as I copied the object it interacted with at runtime the 2nd time, but I’m not sure. I leave a sample project link below for you to examine the problem closely.

What I want to do is to continuously create a copy of the same object slightly in front of it every time the camera enters the collision area with the object opposite.

Sample project: https://playcanvas.com/project/930937/overview/trigger

Sorry, I don’t understand the end result you are looking to achieve here? Can you describe the user actions and what you want to happen. eg

  1. User clicks on a cube
  2. Cube is destroyed
  3. A new cube is created that has a random position
  1. The camera object enters the contact area of ​​the cube in front of it
  2. The cube disappears and the same cube reappears 100 units ahead.
  3. While the above two actions can happen, the camera is constantly in the right motion.

What is desired is, because the camera is constantly in motion, each time the cube formed in front of it enters the contact area, the cube it contacts will disappear and 1 of the same cube will be formed in front of 100 units. This process should continue in an endless loop, but in the project I gave as an example, this process only happens once. Despite entering the cube area formed 100 units ahead in front of the camera, no action is taken.

This process takes place in the RandomRoom script

I am using BoundingBox property to control contact area of ​​camera and cube

Basically, every time the camera hits the cube, the cube is moved 100 units along the z axis of the camera and the camera and/or the cube is always moving?

The camera moves continuously along the z-axis.

The cube, on the other hand, is destroyed (destroy()) when the camera enters the contact area and is copied as a child of the “Room” object in the root directory, recreating the copied object (cube) 100 units away.

Copying and adding to the root directory is done here.

RandomRoom.prototype.spawngo = function(){
    //Spacing of chambers
    var mid = new pc.Vec3(0,0,this.entity.getPosition().z -100);

RandomRoom.prototype.spawn = function (position) {
     /* remember the root to local variable - for better performance */
     this.root = this.root || this.app.root.findByName('Root');

     /* select the child from the Tile1 by this random index and clone it*/
     const tile = this.Tile1.children[0].clone();
     /* disable the tile before changing position/rotation to prevent physics issues */
     tile.enabled = false;
     /* set name */
     tile.name = "newTilee";
     /* set its position and angles */
     tile.setLocalEulerAngles(0, 0, 0);
     /* re-enable it after all the physics properties are set */
     tile.enabled = true;

The issue is with the RandomRoom script:

RandomRoom.prototype.initialize = function() {
    // Keep an array of all the entities we can pick at
    this.pickableEntities = [];
    // Register events for when pickable entities are created
    this.app.on("shapepicker:add", this.addItem, this);
    this.cam = this.app.root.findByName("Camera");

What happens here on startup of the scene:

  1. RandomRoom adds an event listener for “shapepicker:add”
  2. The trigger entity boundingBoxShape script fires the event
  3. RandomRoom adds the trigger entity to pickableEntities array
  4. The camera entity boundingBoxShape script fires the event
  5. RandomRoom adds the camera entity to pickableEntities array
  6. Game is played
  7. The trigger entity collides with the camera
  8. The trigger entity is destroyed
  9. A new trigger entity is created with the RandomRoom
  10. The new trigger entity boundingBoxShape script fires the event “shapepicker:add”
  11. The new trigger entity RandomRoom script adds it to the array pickableEntities
  12. The camera entity is not in the array of pickableEntities of the new trigger entity so the collision is never checked in the update loop

What I would do is to make this simpler and have RandomRoom only check it’s own boundbox to the camera’s directly rather than use events and arrays of pickable entities

1 Like

Hello, first of all thank you very much for your reply.

How can I directly check the box bounds of the camera itself, please? Can you show this on the syntax? What do I need to add where in the project? Thank you very much again.

You are already doing it in the update look where you checking intersection between all bounding shapes. I’m suggesting instead of RandomRoom doing it for all and having this subscription pattern, just doing it between itself and the camera’s

1 Like

Hi, I really appreciate your feedback, I hope I’m not giving you a headache.

I also added the following code block into the “spawn” function of the “RandomRoom” script, but it does work somehow, although it doesn’t work completely. The problem this time around is that the camera reacts as if it’s inside the object, even if it’s not within range of the object in front of it. Results.length returns a value of 1 and occurs continuously at fixed intervals. How can i solve this problem :slight_smile:

     const obje = tile.getPosition();
     const aabb = new pc.BoundingBox(obje, new pc.Vec3());
     this.app.fire("shapepickerr:add", tile,aabb);

As the RandomRoom doesn’t need to know about any other shapes except itself and and the camera, as I mentioned before, I would completely get rid of the subscriber model that you currently have.

The RandomRoom Shape should only test intersections between itself and the camera’s.

This simplifies the logic a lot as you can see here in the fixed project: https://playcanvas.com/editor/scene/1425630

1 Like

Thank you for your response and sample project. As I am still a beginner I sometimes have difficulty understanding even simple functions. Thank you again and I wish you a good day. :slight_smile: