Enabling gravity system

ok so I wanna add gravity into the game so tht when the ball goes to the side it falls down and you die instead of this: https://streamable.com/jyvo0

You should read the Physics section in the User Guide:

https://developer.playcanvas.com/en/user-manual/physics/physics-basics/

1 Like

alright so did exactly what you did and it worked on a testing project but still in galaxy ball the ball doesnt go down? Heres the link: https://playcanvas.com/editor/scene/773725

ok so i made some obstacles but the ball keeps going through the obstacles how do I fix that?

Your problems are explained in this section:

https://developer.playcanvas.com/en/user-manual/physics/physics-basics/#teleporting-dynamic-bodies

Instead of using translate for the ball, you need to be either applying forces to the ball to move it, or setting linearVelocity directly. But if you set linearVelocity directly, gravity will be ignored. So you could do a ray cast from the center of the ball downwards to see if it’s actually on the track. If not, stop setting linearVelocity and the ball will fall due to gravity.

I don’t have linearvelocity Heres the code:` var Movement = pc.createScript(‘movement’);

Movement.attributes.add('speed', { type: 'number', default: 1 });

// initialize code called once per entity
Movement.prototype.initialize = function() {
    
};

//Attributes
Movement.attributes.add(“Main”, { type: ‘entity’ });
Movement.attributes.add(“Stop”, { type: ‘entity’ });

//Will work on wwsad movement soon//

// update code called every frame
Movement.prototype.update = function(dt) {
    var left     = this.app.keyboard.isPressed(pc.KEY_LEFT);
    var right   = this.app.keyboard.isPressed(pc.KEY_RIGHT);
    var up  = this.app.keyboard.isPressed(pc.KEY_UP);
    var down  = this.app.keyboard.isPressed(pc.KEY_DOWN);

    if (left) {
        this.entity.translate(-this.speed * dt, 0, 0);
    }
    if (right) {
        this.entity.translate(this.speed * dt, 0, 0);
    }
    
    if (this.Main.enabled === true) {
        this.entity.translate(0, 0, -this.speed * dt);
    }
    
    if (down) {
        this.entity.translate(0, 0, (this.speed * 2) * dt);
    }
    if (this.entity.getPosition().z <= this.Stop.getPosition().z) {
        this.speed = 0;
    }
};

// swap method called for script hot-reloading
// inherit your script state here
Movement.prototype.swap = function(old) {

};`

Btw the ball still wont go down? think any can give me a hand

As said by Will, if you want to move rigid bodies, you have to do so via the physics system (ie setting the linear velocity and/or using forces) for the physics to behave properly.

If you move the entity via translate (like you are doing now), physics won’t behave properly which is why the ball is going through obstacles and not falling via gravity.

If you look at an example of Rolling ball game here, you can see how the controls are used to apply forces to the ball. https://playcanvas.com/project/409549/overview

I did this

    // update code called every frame
    Movement.prototype.update = function(dt) {
        var left     = this.app.keyboard.isPressed(pc.KEY_LEFT);
        var right   = this.app.keyboard.isPressed(pc.KEY_RIGHT);
        var up  = this.app.keyboard.isPressed(pc.KEY_UP);
        var down  = this.app.keyboard.isPressed(pc.KEY_DOWN);

        if (left) {
            this.entity.linearvolicity(-this.speed * dt, 0, 0);
        }
        if (right) {
            this.entity.linearvolicity(this.speed * dt, 0, 0);
        }
        
        if (this.Main.enabled === true) {
            this.entity.linearvolicity(0, 0, -this.speed * dt);
        }
        
        if (down) {
            this.entity.linearvolicity(0, 0, (this.speed * 2) * dt);
        }
        if (this.entity.getPosition().z <= this.Stop.getPosition().z) {
            this.speed = 0;
        }
    };

this.entity.lineairvolicity is not a function

Actually Ill just do it the simple way Ill need to make the ball jump any ideas on that?

I definitely recommend getting familiar with the JavaScript debugger in the browser’s Dev Tools. It will enable you to self-diagnose problems with your code. Take this line:

this.entity.linearvolicity(-this.speed * dt, 0, 0);
  1. linearvolicity should be linearVelocity (see API ref docs).
  2. linearVelocity is a property of pc.RigidBodyComponent, not pc.Entity, so it should be this.entity.rigidbody.linearVelocity.
  3. linearVelocity is a property, not a function, so you would set it like this:
this.entity.rigidybody.linearVelocity = new pc.Vec3(-this.speed * dt, 0, 0);
2 Likes

I know I’m kind of late, but if you want to use the gravity system, you should make the Rigidbody Dynamic and apply forces instead of translating. You can also use translation to create your own gravity system, although it may not be as smooth as the built in physics gravity.