Jump to content

Physics Movement Is Gounded


MackeyK24
 Share

Recommended Posts

Does anyone know how to tell/check if the object the physics state attach to is 'grounded' or 'airborne'...

I need to check if I'm on the ground for movement and jumping... Unity has a CharacterController.isGrounded flag they use to check for such things.

Do we have ANYTHING like that so I can check if my player character is grounded or airborne ???

Yo @Deltakosh yet another one for ya :)

 

Link to comment
Share on other sites

I saw the collide.... I actually use that for all my physics collision... But seems expensive to collide with everything to see if I'm grounded or not.

What I am actually trying to do is create some "Downloadable Content" for the Toolkit... So far I got Particle systems and material libraries done.

But now I am try to create a 'MovementController' class ... which will function like the Unity CharacterController... It is responsible for all the movement and meant to be used by other components... So you could attach a MovementController to an object and then create a PlayerController that has the main player logic and use this.getComponent("BABYLON.MovementController") from the player controller to get reference to movement controller and do whatever... Just like you would in a unity game... I also added AUTO User input support right on the Movement controller to actually handle getting the user input and moving accordingly.

I have TWO problems I gotta work out... First... I Gott a tell when I'm on the ground (or grounded) I need this to NOT allow DOUBLE JUMPING in the air...

Also I want the player to lookAt the horizontal and vertical direction the user input is... So I use mesh.lookAt ... BUT IT SNAPS TO THAT ROTATION to look at

the direction... its way too fast look funny to SNAP from left to right instead of rotating a little slower....  @Deltakosh got any ideas for me here :)

Here is my current MovementController.ts script design to be attached to ANY 'Actor' to give movement... Please look at how I could check to see if grounded or ANY OTHER WAY to handle jumping... And what can I do about the mesh.lookAt so it does not SNAP to that look at direction but rotates to that direction...

Think Third-Person Controller :)

MovementController:

/* Babylon Character Movement Controller Component */
/* <reference path="{*path*}/Assets/Babylon/Library/babylon.d.ts" /> */

module BABYLON {
    export class MovementController extends BABYLON.MeshComponent {
        public enableInput:boolean = true;
        public autoTurning:boolean = true;
        public moveSpeed:number = 5.0;
        public jumpForce:number = 5.0;
        public dropForce:number = 20.0;
        public rotateSpeed:number = 0.085;
        public isGrounded():boolean { return this._grounded; }
        public getVelocity():BABYLON.Vector3 { return this._movementVelocity; }
        public onUpdateMovement:(velocity:BABYLON.Vector3, horizontal:number, vertical:number, mousex:number, mousey:number, jumped:boolean)=>void = null;

        private _grounded:boolean = true;
        private _inputVelocity:BABYLON.Vector3 = BABYLON.Vector3.Zero();
        private _movementVelocity:BABYLON.Vector3 = BABYLON.Vector3.Zero();
        public constructor(owner: BABYLON.AbstractMesh, scene: BABYLON.Scene, tick: boolean = true, propertyBag: any = {}) {
            super(owner, scene, tick, propertyBag);
            this.autoTurning = this.getProperty("autoTurning", true);
            this.moveSpeed = this.getProperty("moveSpeed", 5.0);
            this.jumpForce = this.getProperty("jumpForce", 5.0);
            this.dropForce = this.getProperty("dropForce", 20.0);
            this.rotateSpeed = this.getProperty("rotateSpeed", 0.085);
            this.enableInput = this.getProperty("enableInput", true);
        }

        public move(velocity:BABYLON.Vector3):void {
            this._movementVelocity.x = velocity.x;
            this._movementVelocity.y = velocity.y;
            this._movementVelocity.z = velocity.z;
            this.manager.moveWithPhysics(this.mesh, velocity);
        }

        protected after() :void {
            var ground:number = 2.0; // TODO: Get Physics Grounded
            this._grounded = !(this.mesh.position.y > ground); 
            if (this.enableInput === true) {
                var horizontal:number = this.manager.getUserInput(BABYLON.UserInputAxis.Horizontal);
                var vertical:number = this.manager.getUserInput(BABYLON.UserInputAxis.Vertical);
                var mousex:number = this.manager.getUserInput(BABYLON.UserInputAxis.MouseX);
                var mousey:number = this.manager.getUserInput(BABYLON.UserInputAxis.MouseY);
                var gravity:number = this.dropForce * this.manager.deltaTime;
                var jumped:boolean = false;
                // Avatar movement and jumping velocity
                if (this._grounded === true) {
                    var jump1:boolean = this.manager.getKeyInput(BABYLON.UserInputKey.SpaceBar);
                    var jump2:boolean = this.manager.getButtonInput(BABYLON.Xbox360Button.A);
                    this._inputVelocity.x = horizontal * this.moveSpeed;
                    this._inputVelocity.z = vertical * this.moveSpeed;
                    if ((jump1 || jump2) && this.jumpForce > 0.0) {
                        jumped = true;
                        this._inputVelocity.y = this.jumpForce;
                    }
                } else {
                    this._inputVelocity.y -= gravity;
                }
                // Update avatar movement handler
                if (this.onUpdateMovement != null) {
                    this.onUpdateMovement(this._inputVelocity, horizontal, vertical, mousex, mousey, jumped);
                }
                // Update avatar position and rotation
                this.move(this._inputVelocity);
                if (this.autoTurning === true && (horizontal !== 0.0 || vertical !== 0.0)) {
                    // TODO: Rotate actor to face horizontal and vertical movement direction
                    this.mesh.lookAt(this.mesh.position.add(new BABYLON.Vector3(-horizontal, 0.0, -vertical)));
                }
            }
        }
    }
}

 

What do you think ???

 

Link to comment
Share on other sites

6 hours ago, adam said:

I think you're going to have to determine if the body is grounded by observing collisions.

You can use PhysicsImposter.registerOnPhysicsCollide:

https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/babylon.physicsImpostor.ts#L320

I use this in other places to detect collision so I know where to set it up... Also I found the THREE.JS/CANNON source for "CanJump" flag:

 

 var canJump = false;

    var contactNormal = new CANNON.Vec3(); // Normal in the contact, pointing *out* of whatever the player touched
    var upAxis = new CANNON.Vec3(0,1,0);
    cannonBody.addEventListener("collide",function(e){
        var contact = e.contact;

        // contact.bi and contact.bj are the colliding bodies, and contact.ni is the collision normal.
        // We do not yet know which one is which! Let's check.
        if(contact.bi.id == cannonBody.id)  // bi is the player body, flip the contact normal
            contact.ni.negate(contactNormal);
        else
            contactNormal.copy(contact.ni); // bi is something else. Keep the normal as it is

        // If contactNormal.dot(upAxis) is between 0 and 1, we know that the contact normal is somewhat in the up direction.
        if(contactNormal.dot(upAxis) > 0.5) // Use a "good" threshold value between 0 and 1 here!
            canJump = true;
    });

 

Ill try and mess with that... Thanks for the info... Ill let ya know how it works :)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...