Jump to content

Why isn't my player character going towards the ground even though I have the gravity set?


En929
 Share

Recommended Posts

Hello, I'm new to this forum and new to Babylon.js. I started out coding 2D games in Blitz Basic and am now trying to delve into 3D. I still consider myself a beginner. 

Here, I was trying to figure out why isn't my player character going towards the ground even though I have the gravity set on the player in the code below? Thanks!

The sprites that I used for the scene is attached to this message and the part that I needed help with is between the dotted lines:

 

 

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Babylon.js sample code</title>
        <!-- Babylon.js -->
        <script src="http://www.babylonjs.com/hand.minified-1.2.js"></script>
        <script src="http://www.babylonjs.com/cannon.js"></script>
        <script src="http://www.babylonjs.com/oimo.js"></script>
        <script src="http://www.babylonjs.com/babylon.js"></script>
        <style>
            html, body {
                overflow: hidden;
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
            }

            #renderCanvas {
                width: 100%;
                height: 100%;
                touch-action: none;
            }
        </style>
    </head>
<body>
    <canvas id="renderCanvas"></canvas>
    <script>
        var canvas = document.getElementById("renderCanvas");
        var engine = new BABYLON.Engine(canvas, true);

        var createScene = function () {
                       var scene = new BABYLON.Scene(engine);


//-----------------------Here's the part where I need help -------------------------------------------------------------------		

   
				scene.collisionsEnabled = true;
				
				scene.gravity = new BABYLON.Vector3(0, -9.81, 0);

var light = new BABYLON.PointLight("Point", new BABYLON.Vector3(5, 10, 5), scene);

     
 
var camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 0.8, 0), scene);
                 camera.attachControl(canvas, true);
		 camera.checkCollisions = true;
		 camera.applyGravity = true;
			

          //This is the player character that I was speaking about:

var spriteManagerPlayer = new BABYLON.SpriteManager("playerManager", "textures/player.png", 2, 64, scene);
             var player = new BABYLON.Sprite("player", spriteManagerPlayer);
                 player.playAnimation(0, 40, true, 100);
                 player.position = new BABYLON.Vector3(1, 4, 0);
                 player.size = 0.6;
			     player.applyGravity = true;
			     player.checkCollisions = true;
           

		   var plane = BABYLON.Mesh.CreatePlane("plane", 120, scene);
			     plane.position.y = 0;
			     plane.rotation.x = Math.PI / 2;
			     plane.checkCollisions = true;


		   
//-----------This is all I needed help with for now. Ignore what's below this line. Thanks!--------------------------------------------------------------------
		   
		   
		   
		   
           
	
				 //Creation of a repeated textured material
			var materialPlane = new BABYLON.StandardMaterial("texturePlane", scene);
			materialPlane.diffuseTexture = new BABYLON.Texture("textures/Face.png", scene);
			materialPlane.diffuseTexture.uScale = 2.0;//Repeat 5 times on the Vertical Axes
			materialPlane.diffuseTexture.vScale = 2.0;//Repeat 5 times on the Horizontal Axes
			materialPlane.backFaceCulling = false;//Always show the front and the back of an element
           
		   plane.material = materialPlane;
		   
            
        
            return scene;
        }
        
        
        var scene = createScene();

        engine.runRenderLoop(function () {
	
            scene.render();
        });

        // Resize
        window.addEventListener("resize", function () {
            engine.resize();
        });
    </script>
</body>
</html>

 

 

 

player.png

Face.png

Link to comment
Share on other sites

Without running your code, I can tell you this -

The internal collision system doesn't constantly check If an object needs to move of the object is static, like your player, it will stay in place until moved (using the moveWithCollisions method). To constantly check that you will need a physics engine. See first if the internal collision system is enough.

I'm on mobile do I can't show an example, but search the forum for move with collisions, I think you'll find the information you need

Link to comment
Share on other sites

Did somebody say moveWithCollision? -> http://www.babylonjs-playground.com/#1M7CV7#3

This is not my PG this is one of the RaananW's treasures btw :D 

@En929 Some of the collision magic has been discussed here: 

Plus you can also check http://doc.babylonjs.com/tutorials/Intersect_Collisions_-_mesh

 

Link to comment
Share on other sites

Thanks RaananW and Omar. I figured it out. I only needed one line of code to make my characters move down and that line was:

 

		   scene.registerBeforeRender(function () {
		   
			var move = 0.01;
				
		        
				player.position.y -= move;
				player2.position.y -= move;
				
			
				
		}

 

I'm seeing that it is a very very important line to have for making games. I hope that my thread here helps someone else out who may have had the same problem that I had. Thanks again:

 

 

Link to comment
Share on other sites

I agree, En929, thanks for pointing this out. (And welcome to the forum!)

Check out the NAME of this doc:

http://doc.babylonjs.com/tutorials/Cameras,_Mesh_Collisions_and_Gravity

Just that name (which I have struggled-with for 2 years now)... implies that mesh have .applyGravity property.

Inside that doc, the subject matter starts with setting camera gravity and collision stuff... and next... the subject goes to mesh-to-mesh collision (but reader's minds are still thinking about gravity, but it doesn't apply to mesh).  So, in my opinion, that doc is still named wrong, and should be split into two tutorials, or made more clear.

But this brings us to another interesting thing.  How much extra "load" would happen... if we DID have/allow a mesh.applyGravity = true/false?  That's a question for the big core programmers, and I bet this thought has happened before.  Likely, there is a good reason why mesh.applyGravity is not available.

Yep, thanks for the tip, En... duly noted and docs soon to be adjusted.

Link to comment
Share on other sites

9 hours ago, En929 said:

Thanks RaananW and Omar. I figured it out. I only needed one line of code to make my characters move down and that line was:

 


		   scene.registerBeforeRender(function () {
		   
			var move = 0.01;
				
		        
				player.position.y -= move;
				player2.position.y -= move;
				
			
				
		}

I'm seeing that it is a very very important line to have for making games. I hope that my thread here helps someone else out who may have had the same problem that I had. Thanks again:

Wouldn't that simply move your player endlessly down? 

I am not sure this is the best solution for your problem. I am also not too sure it is a very very important line. But if it solved your issues, use it :)

If you ask me, I think the solution is moveWithCollisions , or using a proper physics engine.

Link to comment
Share on other sites

14 hours ago, Wingnut said:

I agree, En929, thanks for pointing this out. (And welcome to the forum!)

Check out the NAME of this doc:

http://doc.babylonjs.com/tutorials/Cameras,_Mesh_Collisions_and_Gravity

Just that name (which I have struggled-with for 2 years now)... implies that mesh have .applyGravity property.

Inside that doc, the subject matter starts with setting camera gravity and collision stuff... and next... the subject goes to mesh-to-mesh collision (but reader's minds are still thinking about gravity, but it doesn't apply to mesh).  So, in my opinion, that doc is still named wrong, and should be split into two tutorials, or made more clear.

But this brings us to another interesting thing.  How much extra "load" would happen... if we DID have/allow a mesh.applyGravity = true/false?  That's a question for the big core programmers, and I bet this thought has happened before.  Likely, there is a good reason why mesh.applyGravity is not available.

Yep, thanks for the tip, En... duly noted and docs soon to be adjusted.

 

Thanks Wingnut. Yes, I got confused with  thinking that the mesh had .applyGravity properties. I applied Gravity to it and didn't know why it didn't work.. Thus, thanks for point that out. I can now be aware of that when I try to further develop my coding. 

Link to comment
Share on other sites

13 hours ago, RaananW said:

Wouldn't that simply move your player endlessly down? 

I am not sure this is the best solution for your problem. I am also not too sure it is a very very important line. But if it solved your issues, use it :)

If you ask me, I think the solution is moveWithCollisions , or using a proper physics engine.

 

 

Hey RaananW, I'm always opened to better ways of doing things in coding. What I wrote above was a small code bit to bring my player characters downward, but if there's a better way of doing what I did, I'm opened to suggestions. Afterall, I'm still learning. Thanks!

Link to comment
Share on other sites

2 hours ago, En929 said:

 

 

Hey RaananW, I'm always opened to better ways of doing things in coding. What I wrote above was a small code bit to bring my player characters downward, but if there's a better way of doing what I did, I'm opened to suggestions. Afterall, I'm still learning. Thanks!

There are better ways. I think I suggested two if them :-) 

Your code will simply drag your player down. Unless you manually (code) block it, it will forever go down. 

Link to comment
Share on other sites

31 minutes ago, RaananW said:

There are better ways. I think I suggested two if them :-) 

Your code will simply drag your player down. Unless you manually (code) block it, it will forever go down. 

RaananW, my original code was going to have it so that the player would stop going down at a certain point.  The code that I wrote above was a bare bones simplified version of what I was originally going to write. It was me trying to figure out how to get my character to move down altogether. Then, I was going to add the other stuff back in. I just tried to simplify the code above so that when someone reads my code on here, they wouldn't have as much stuff to sift through. I hope this makes sense. But you're right, what I wrote above makes the player go down forever and ever. 

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