Domanda

I've been looking around for a while now for a conclusion for this, but I seem to be unable to find any solution to this.

I'm trying to create a simple scene to get a little platform action to work using THREE.JS and PHYSI.JS. I have one flat box with a mass of 0 to act as my workd, and a box mesh to act as my character. The site of Physijs appears to suggest that I set the __dirtyPosition variable to true. The problem here is this: __dirtyPosition messes with the physics. As I move my character off the edge of the level, he's not falling. In fact, it seems to stop halfway the world cube when it does collide.

When I try to update the position without applying __dirtyPosition, the cube gets put back in it's place. It trembles a bit as if it's having a tiny seizure and that's it. It'd be great if I could use both physics and movement, using Physi for just collision feels kind of like overdoing it. Here's how I do it now, clearly the wrong way:

level1.generateScene = function()
{
   level1.camera = new THREE.PerspectiveCamera(75, this.cameraWidth / this.cameraHeight, 0.1, 1000);

   level1.material = new AdapterEngine.createBasicMaterial({color: 0x00ff00}); //Threejs basic material
   level1.ground = new AdapterEngine.createBoxMesh(5, 0.1, 5, level1.material, 0); //Physijs box mesh with a mass of 0

   level1.playerMaterial = new AdapterEngine.createBasicMaterial({color:0x0000ff}, 0.6, 0.3); //Physijs basic material with a fruction of 0.6, and a restitution of 0.3
   level1.player = new AdapterEngine.createBoxMesh(0.3, 0.3, 0.3, level1.playerMaterial, 0.1); //Physijs box mesh with a mass of 0.1
   level1.player.y = 50; //yes, it should fall down for a bit.

   level1.scene.add(level1.ground);
   level1.scene.add(level1.player);

   level1.camera.position.z = 5;
   level1.camera.position.y = 1.4;
   level1.activeCamera = level1.camera;
   level1.controls.init();
}

This is the function where the 'level', with both character and world gets created. What the level object also contains, is an update function that plays when after the requestAnimationframe function is called, and right before the renderer renders.

level1.update = function()
{
   this.scene.simulate();

   level1.player.__dirtyPosition = true;
   if(level1.controls.isKeyDown('RIGHT')) level1.xvelocity = 0.1;
   else if(level1.controls.isKeyDown('LEFT')) level1.xvelocity = -0.1;
   else if(level1.controls.isKeyUp('RIGHT') || level1.controls.isKeyUp('LEFT')) level1.xvelocity = 0;

   level1.player.rotation.x = 0;
   level1.player.rotation.y = 0;
   level1.player.rotation.z = 0;

   level1.player.position.x = level1.player.position.x + 0.5*level1.xvelocity;
}

I know similar questions have popped up, but none have really yielded satisfactory answers. I am genuinely at a loss of what to do, or which functions I should use.

edit I forgot to add this part: I've been using a little framework that determines whether I should get a THREE.js object or a PHYSI.js object that works as a bridge between my game and my libraries. These follow the same parameters as THREE.js and PHYSI.js do, I have marked with comments which function returns which type of object.

È stato utile?

Soluzione

Ok. First of all, you should NOT use __dirtyPosition to work with speed. I recommend using setLinearVelocity and getLinearVelocity:

level1.update = function(){
    this.scene.simulate();

    // Conditions...

    var oldVector = this.player.getLinearVelocity(); // Vector of velocity the player already has
    var playerVec3 = new THREE.Vector3(oldVector.x + .5 * this.xvelocity, oldVector.y, oldVector.z);
    this.player.setLinearVelocity(playerVec3); // We use an updated vector to redefine its velocity
}

Second of all, the reason you have this problem is probably because you didn't set the __dirtyRotation flag to true. You had a small area where you wanted to reset the rotation. Other than that, I don't see anything wrong from what you've posted.

Altri suggerimenti

For just a single cube character movement, try object.setLinearVelocity(vector) instead of using object.__dirtyPosition,

I think __dirtyPosition generally disables most of physics effects

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top