Champa Posted June 17, 2016 Share Posted June 17, 2016 Hello guys! Here I am with yet another question So I want to make it possible for players to jump on objects (meshes) that are somewhat lower than the players camera. I have set the checkCollisions to true but i can't seem to jump on any objects Here is my jump function //Object i wanna jump on var cube = BABYLON.Mesh.CreateBox("box", 10, scene); cube.checkCollisions = true; cube.position.y = - 4.5 ; //Jump function _playerJump : function(){ $this = this; if($this.isJumping == false && $this.energy - 30 >= 0){ var camera = $this.camera, jumpAnim = new BABYLON.Animation("jumpAnim", "position.y", 20, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE), keys = [], jumpStep; camera.animations = []; for(jumpStep = 0; jumpStep < 23; jumpStep ++) keys.push({frame: jumpStep, value: camera.position.y + jumpStep / 5}); for(jumpStep = 24; jumpStep < 46; jumpStep ++) keys.push({frame: jumpStep, value: camera.position.y + 4.8 - (jumpStep - 24) / 5}); jumpAnim.setKeys(keys); var easingFunction = new BABYLON.BezierCurveEase(.13,.01,.63,1.41); easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEIN); jumpAnim.setEasingFunction(easingFunction); camera.animations.push(jumpAnim); $this.isJumping = true; $this._setEnergy(-30); $this.scene.beginAnimation(camera, 0, 46, false, 4, function(){ $this.isJumping = false; $this.isCrouching = false; }); } } Quote Link to comment Share on other sites More sharing options...
Champa Posted June 17, 2016 Author Share Posted June 17, 2016 Anyone ? Pretty please ? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted June 17, 2016 Share Posted June 17, 2016 Hello, if you could provide a Playground, it would be utterly simpler to help you Quote Link to comment Share on other sites More sharing options...
Champa Posted June 17, 2016 Author Share Posted June 17, 2016 I can't seem to get the playground script to work Quote Link to comment Share on other sites More sharing options...
Boz Posted June 17, 2016 Share Posted June 17, 2016 Do the Animation take the checkCollisions into account ? For my part when I was using a jump animation I got collisions problems, I think both should not be used in the same time Quote Link to comment Share on other sites More sharing options...
Champa Posted June 17, 2016 Author Share Posted June 17, 2016 1 minute ago, Pouet said: Do the Animation take the checkCollisions into account ? For my part when I was using a jump animation I got collisions problems, I think both should not be used in the same time What do you mean? can you give me an example ? Quote Link to comment Share on other sites More sharing options...
Boz Posted June 17, 2016 Share Posted June 17, 2016 I think that checkCollisions is used only in moveWithCollisions function. When you move your player with the keyboard, you call moveWithCollisions(dx, dy, dz) which compute the displacement in x, y and z axis, regarding if there is collisions or not. It then apply this displacement at your "mesh.position" and everything works fine. Great. When you start an animation, I'm afraid that this animation do not use moveWithCollisions to move, but directly uses mesh.position. This is normal behavior, an animation has a predefined array of positions (the keys[] array), and goes from start position to end position. So I assume (maybe i'm totally wrong ) that your animation does not take care of your checkCollisions boolean, position is updated until the last frame. I'm currently doing exactly the same for my game (make the player moving and jumping). I don't get a satisfying solution for now. However, I have 2 ideas : the ugly one : create your own animation model in your Player class, and call moveWithCollisions each frame, with the jumpStep values. Congratulations, you just created the CollisionAnimation object, nice ! the I-hope-its-not-too-resource-consuming one : you have to check yourself if there is a collision during the jump. You can registerBeforeRender a new function which will check if your playerMesh intersects the object you want to jump in. You call this function each step of the animation (so basically each frame until the end of the anim). If the player intersects the mesh, stop the animation : it will not go through the object Sadly, you have to check all the objects of the scene because you cant know on which object you're jumping. Maybe you can if you store the id of the nearest object... or with other trick. I will test the second option for my game, if I have good results I will tell you. If you do it before me and you got good results.. Tell me too I am not even sure this was your problem, if it's the case, leave a feedback ! Bye, Pouet Quote Link to comment Share on other sites More sharing options...
Champa Posted June 17, 2016 Author Share Posted June 17, 2016 Hey @Pouet I'm actually not moving meshes with the jump function but the camera (Its a FPS game) _createCamera : function(){ var camera = new BABYLON.FreeCamera("playerCamera", this.spawnpoint, this.scene); camera.attachControl(this.scene.getEngine().getRenderingCanvas(), false); camera.ellipsoid = new BABYLON.Vector3(2, 10, 2); //camera.collisionRadius = new BABYLON.Vector3(0.5, 0.5, 0.5) camera.checkCollisions = true; camera.applyGravity = true; camera.keysUp = [87]; // W camera.keysDown = [83]; // S camera.keysLeft = [65]; // A camera.keysRight = [68]; // D camera.speed = this.walkSpeed; camera.inertia = this.inertia; camera.angularInertia = this.angularInertia; camera.angularSensibility = this.angularSensibility; camera.layerMask = 2; return camera; } I'm not working with moveWithCollision because that's for meshes only, right ? Quote Link to comment Share on other sites More sharing options...
Boz Posted June 17, 2016 Share Posted June 17, 2016 Yes, but if you apply an animation on your camera, it will be the same, checkCollisions is not detected in my opinion. What you can do is : - define only UP frames in your animation : player will jump, it will stop at max height, and gravity will make him go down. OK because I suppose gravity take checkCollisions into account. OR - apply the second solution I gave to you.. check intersectsMesh every frame and stop the animation when it collides. These are just some ideas, I don't have this implemented.. will do it this week-end, so I can give you my solution on next week (I work with either freeCamera and meshes) See ya! Champa 1 Quote Link to comment Share on other sites More sharing options...
Champa Posted June 18, 2016 Author Share Posted June 18, 2016 Uhhh, my problem is not the collision but not able to jump on meshes I cant jump on some object that are somewhat lower than the camera Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.