cannon.js icon indicating copy to clipboard operation
cannon.js copied to clipboard

Stop event after bounce

Open rmrbytes opened this issue 6 years ago • 7 comments

Is there any event available that triggers when an object stops after a bounce? Thanks

rmrbytes avatar Oct 31 '18 10:10 rmrbytes

How about something like this?

var bounced = false;
body.addEventListener("collide",function(e){
    bounced = true;
});
world.addEventListener("postStep",function(e){
    if(bounced && body.velocity.length() < 0.01){
        console.log("bounced and stopped!");
    }
});

schteppe avatar Nov 04 '18 10:11 schteppe

Thanks for the above @schteppe however the postStep event does not seem to be triggering. The collide event tho does trigger. I tried checking for body.velocity in the collide event and it gives an undefined value.

rmrbytes avatar Nov 05 '18 03:11 rmrbytes

postStep not triggering? I wonder why... Maybe you can try body.postStep=function(){...}; instead?

Body.velocity cannot be undefined.. if it was, cannon would not work. Maybe it’s the body variable? Replace it with the body variable you are interested in.

schteppe avatar Nov 05 '18 07:11 schteppe

Hmm. I forgot to mention that I am using cannon.js under the aframe framework using Don Mccurdy's physics system (https://github.com/donmccurdy/aframe-physics-system). Not sure if your code works inside it. If it helps, here is the code

 ...
  let bounced = false;
  let world = new CANNON.World();
  let ball = document.createElement('a-entity');
  ball.setAttribute('id', 'ball');
  ball.setAttribute('velocity', '0 0 0')
  ball.setAttribute('geometry', 'primitive:sphere; radius:0.457');
  ball.setAttribute('material', 'src:#ball');
  ball.setAttribute('shadow', 'receive:true; cast:true');
  ball.setAttribute('position', '0 2 -4');
  ball.setAttribute('dynamic-body', 'linearDamping:0.1');
  ball.addEventListener("collide",function(e){
    console.log('collide called', ball.velocity);
      bounced = true;
  });
  world.addEventListener("postStep",function(e){
    console.log('postStep called', ball.velocity);
    if(ball.velocity.length() < 0.01){
        console.log("bounced and stopped!");
    }
  });
  console.log(world);
  _scene.appendChild(ball);
...

And collide gets called with the ball.velocity saying undefined

rmrbytes avatar Nov 05 '18 07:11 rmrbytes

oops. I know why the ball.velocity is undefined. Because ball is a DOM object, it should be ball.getAttribute('velocity') but then that will give the velocity attribute of the DOM object, not the velocity property of the Cannon Object.

But I am wondering, now, how within the aframe system I should access the postStep event?

rmrbytes avatar Nov 05 '18 07:11 rmrbytes

I see. If you need help with the cannon.js API in A-frame, ask in that repo. I don’t know how cannon is integrated into it.

schteppe avatar Nov 06 '18 07:11 schteppe

yes. will check that repo. You have set me in the right direction and thanks for that. Let me figure it out and will add a comment to this thread. Thanks.

rmrbytes avatar Nov 06 '18 08:11 rmrbytes