schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.71k stars 711 forks source link

Stop event after bounce #402

Open rmrbytes opened 6 years ago

rmrbytes commented 6 years ago

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

schteppe commented 6 years ago

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!");
    }
});
rmrbytes commented 6 years ago

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.

schteppe commented 6 years ago

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.

rmrbytes commented 6 years ago

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 commented 6 years ago

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?

schteppe commented 6 years ago

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.

rmrbytes commented 6 years ago

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.