Giter Site home page Giter Site logo

applyForce() about physijs HOT 4 CLOSED

chandlerprall avatar chandlerprall commented on July 18, 2024
applyForce()

from physijs.

Comments (4)

chandlerprall avatar chandlerprall commented on July 18, 2024 1

I have added applyForce and applyCentralForce. The difference between an impulse and a force, like you stated, is the timing. An impulse is applied in full all at once while a force is applied over time. Gravity is a force, it's applied steadily over the life of an object, but shoving something is an impulse.

Best way to use the new force methods is to add an event listener to the scene for the update event:

var scene = new Physijs.Scene;
scene.addEventListener( 'update', myUpdateHandler );

Apply the force again each time myUpdateHandler is called. You can ensure only one time step is simulated at a time by calling scene.simulate( undefined, 1 ); - the first argument is the amount of time to simulate, undefined tells Physijs to attempt to simulate all time since the last call. The second argument limits the number of steps to simulate to just 1, which will cap the simulated time at the time step (default is 1/60 or 16.7ms). This will make sure myUpdateHandler is called after every frame so you can apply the force again.

I will have to set up a test to try and duplicate what you are seeing with the rocket speeding up endlessly, I don't know what is causing that. In the mean time, let me know how using force works for you.

from physijs.

rscottfree avatar rscottfree commented on July 18, 2024

YES! applyForce() and applyCentralForce() work as expected. Thank you.

As for the issue with applyImpulse() and it speeding up: my latest theory is that when I'm applying the impulse over and over again, I'm actually applying the impulse faster than the simulation can take it in, and when I turn the object around and accelerate in the opposite direction, it applies all the impulses that it didn't have a chance to apply before, causing the object to speed up in the same direction it was heading instead of slowing.

Not knowing the math or code very well, I'm just grasping at straws. Either way, applyForce() works predictably and I'm quite happy with it. Thanks again.

from physijs.

rscottfree avatar rscottfree commented on July 18, 2024

I figured out the issue of ever-accelerating objects. It had to do with the way I was converting the force vector into the the right rotation to apply to my object (to be applied in the XYZ of the object instead of the XYZ of the world). This is the link to my initial question about this

Instead of doing this:
ship.applyCentralForce(ship.matrix.multiplyVector3(new THREE.Vector3(0, 10, 0)));

You have to do something like this:

var rotation_matrix = new THREE.Matrix4();
rotation_matrix.extractRotation(ship.matrix);
var force_vector = new THREE.Vector3(0, 100, 0);
var final_force_vector = rotation_matrix.multiplyVector3(force_vector);
ship.applyCentralForce(final_force_force);

What I was noticing before was the farther my ship object got away from origin (0, 0, 0), the more erratic it seemed to behave when I applied forces or impulses to it. I realized that when I was multiplying the matrix by my force vector, it was probably multiplying it by the position part of the matrix as well, which I didn't want. I just wanted to convert the force's rotation to be relative to the rotation of the object.

There might be an easier way to do this still, but for now it is working quite well. Also, applyCentralForce() seems to have better results than applyCentralImpulse() when dealing with continual application of forces (like a rocket ship firing).

from physijs.

lazd avatar lazd commented on July 18, 2024

Here's a version of @rscottfree's method that works with three.js r55+:

var rotation_matrix = new THREE.Matrix4().extractRotation(ship.matrix);
var force_vector = new THREE.Vector3(0, 100, 0).applyMatrix4(rotation_matrix);
ship.applyCentralImpulse(force_vector);

from physijs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.