Jump to content

Jumping on objects


Recommended Posts

Hello guys!

Here I am with yet another question :D

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){
				camera = $this.camera,		
				jumpAnim = new BABYLON.Animation("jumpAnim", "position.y", 20, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE),
				keys = [],

			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});
			var easingFunction = new BABYLON.BezierCurveEase(.13,.01,.63,1.41);

			$this.isJumping = true;
			$this.scene.beginAnimation(camera, 0, 46, false, 4, function(){
				$this.isJumping = false;
				$this.isCrouching = false;


Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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 :o) 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 :D 

I am not even sure this was your problem, if it's the case, leave a feedback !


Link to comment
Share on other sites

Hey @Pouet I'm actually not moving meshes with the jump function but the camera (Its a FPS game)

_createCamera : function(){
			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 ?

Link to comment
Share on other sites

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.

- 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!

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.

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.


  • Recently Browsing   0 members

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