kripken / ammo.js

Direct port of the Bullet physics engine to JavaScript using Emscripten

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ho to move a kinematic rigid body with mouse with Vector3

oliverellmers opened this issue · comments

Hello,

I am working in THREE.JS with the ammo.js port, and can not figure out ow to go above apply simply XYZ postiion transformaitons to a rigidbody that i woul dliek to use to hit other soft bodies.

Is there are example or documentaiton on how to do with with X Y Z mouse input?

Are you asking how to set up kinematic bodies? (If you don't know about kinematic bodies in Ammo, the PlayCanvas docs have some info). The PlayCanvas engine codebase shows how kinematic bodies are set up. Ideally, you should ensure the body is configured as follows:

    body.setCollisionFlags(body.getCollisionFlags() | BODYFLAG_KINEMATIC_OBJECT);
    body.setActivationState(BODYSTATE_DISABLE_DEACTIVATION);

Thaks for the links - sadly I can't quite find any answers to my problem in those above..

I am trying to achieve something that in my mind seems fairly simple -

I have a softbody object anchored in space i would to be able to swipe or hit with my mouse, and it to respond to this.

My thinking here is to have a rigidbox ball be moved around with the mouse, which you can then use to 'hit' the softbody with.

Currently I have a rigib body ball moving around with the mouse, but it doesnt affect any softbody objects in the scene....

function createMouseGeometry(){

              const ballMass = 0;
              const ballRadius = 7;

              ball = new THREE.Mesh( new THREE.SphereGeometry( ballRadius, 18, 16 ), ballMaterial );
		
              ball.castShadow = true;
              ball.receiveShadow = true;
              const ballShape = new Ammo.btSphereShape( ballRadius );
              ballShape.setMargin( margin );
              pos.copy( raycaster.ray.direction );
		pos.add( raycaster.ray.origin );
		quat.set( 0, 0, 0, 1 );
              ballBody = createRigidBody( ball, ballShape, ballMass, pos, quat );
              ballBody.setFriction( 0.5 );
		

              ballBody.setCollisionFlags( 2 );
		scene.add(ball);
	}

			
	// Follows the mouse event
	function onMouseMove(event) {

		mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
		mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
		raycaster.setFromCamera(mouse, camera);
		raycaster.ray.intersectPlane(plane, intersects);

		ball.position.set(intersects.x, intersects.y, intersects.z);

		
		const velocity = new Ammo.btVector3(intersects.x, intersects.y, intersects.z);                
              ballBody.setLinearVelocity(velocity);
              ballBody.needUpdate = true;
              Ammo.destroy(velocity);
		
		
	};