[SOLVED] rigidbody.applyForce problem

rigidbody.applyForce sometime Does not work?about once every 20 times.

How are you currently using it? Do you have a project to share?

// More information about touch events can be found here
// http://developer.playcanvas.com/en/api/pc.Touch.html

var Touch = pc.createScript("touch");

// initialize code called once per entity
Touch.prototype.initialize = function() {
    this.originPos = new pc.Vec2();
    this.pos = new pc.Vec2();
    this.cameraEntity = this.app.root.findByName("Camera");

    // Only register touch events if the device supports touch
    var touch = this.app.touch;
    if (touch) {
        touch.on(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
        touch.on(pc.EVENT_TOUCHMOVE, this.onTouchMove, this);
        touch.on(pc.EVENT_TOUCHEND, this.onTouchEnd, this);
        touch.on(pc.EVENT_TOUCHCANCEL, this.onTouchCancel, this);

Touch.prototype.updateFromScreen = function (screenPos) {

Touch.prototype.onTouchStart = function (event) {
    // For the demo, we only work with the first registered touch
    if (event.touches.length === 1) {
        this.pos = new pc.Vec2();
        this.originPos = event.touches[0];

Touch.prototype.onTouchMove = function (event) {
    // Use only the first touch screen x y position to move the entity
    this.pos= new pc.Vec2(this.pos.x+event.touches[0].x-this.originPos.x,this.pos.y+event.touches[0].y-this.originPos.y);

Touch.prototype.onTouchEnd = function (event) {
    // Change the material only if the last touch has ended
    var total = Math.abs(this.pos.x) +Math.abs(this.pos.y)+0.1;

only in Cell phone,computer is ok。

It looks like you are only applying force on one frame which is the same as just tapping an object. Force has to applied constantly. If you want something to have instant acceleration, use applyImpulse or set the velocity of the rigid body.

Actually, I’ve also posted on this same topic. The thing you need to do is linearVelocity(x, y, z);
addForce doesn’t work anymore, tried it on everything. I don’t even think there is one single use for it.

Errr… applyForce does work. Here is a quick example of force being applied against gravity: https://playcanvas.com/editor/scene/600141

Oh, ok. But what is the difference between applyForce and linearVelocity?

LinearVelocity is where you set the velocity of the object. There is no acceleration, it is just instant.

applyForce is the equivalent of pushing or pulling something. If you only ‘push’ for one frame, it isn’t going to move very far as you are only applying acceleration to the object for a single frame. (force = mass * acceleration). If you want an object to speed up and move, you have to be constantly be applying force (and therefore acceleration) until it reaches the desired speed.

Ah, thanks. That makes sense both in code and application.

thank you . thank you . thank you,i wiil try it

thank you, I already understand。thank you very much。

1 Like