Jump to content

Move object to another position already after rendering scene begun


yerzhik
 Share

Recommended Posts

I have set some scene, inside of which there is a boxes/

Now, by some event, for example a button press I want to set a position of a certain box to some initial position values.

 

When setting the scene I was using this code:

 

var mainBox = BABYLON.Mesh.CreateBox("Box", 3, scene);mainBox.position = new BABYLON.Vector3(0, -20, -25);var materialWood = new BABYLON.StandardMaterial("wood", scene);materialWood.diffuseTexture = new BABYLON.Texture("assets/wood.jpg", scene);materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.0, 0.5);mainBox.material = materialWood;

Now in the render function I want, if button was pressed to set position to mainBox:

....mainBox.position = new BABYLON.Vector3(0, -20, -25);

But its ignoring it and its not moving to a new position.

 

How should I do it instead?

 

 

 

Link to comment
Share on other sites

Hello yerzhik,

 

You can check this link: http://www.html5gamedevs.com/topic/2264-move-forward-and-rotation/?hl=move

Here you have a complete example of how to use keys to move a mesh in the render loop.

 

Another complete example (fully functional) is the one in the http://www.babylonjs.com/ . Go there and check the Carlander Cargame.

 

Anyway, you are setting the mesh to the same position where you draw it in the first place.

 

Hope I've been helpfull.

Link to comment
Share on other sites

Hello yerzhik,

 

You can check this link: http://www.html5gamedevs.com/topic/2264-move-forward-and-rotation/?hl=move

Here you have a complete example of how to use keys to move a mesh in the render loop.

 

Another complete example (fully functional) is the one in the http://www.babylonjs.com/ . Go there and check the Carlander Cargame.

 

Anyway, you are setting the mesh to the same position where you draw it in the first place.

 

Hope I've been helpfull.

 

Hi Kilombo, 

 

I was applying impulse there.

Each time I want apply it again on the same object I want to move it to initial position. But its not moving.

I have seen your link and I have done same thing. take a look please:

 

scene.registerBeforeRender(function() {        if (vectorX != 0 || vectorY != 0 || vectorZ!=0) {            mainObjectBox.position.x = -0;            mainObjectBox.position.y = -0;            mainObjectBox.position.z = -25;            mainObjectBox.applyImpulse(new BABYLON.Vector3(-vectorY, vectorZ, vectorX), new BABYLON.Vector3(0, 0, 0));            vectorX = 0;            vectorY = 0;            vectorZ = 0;        }    });

Its applying impulse but not moving to the initial position

Link to comment
Share on other sites

You want to move the mesh with physics enabled ?

 

In that case, you have to set one impostor to the mesh and set the physics, something like this:

 

scene.collisionsEnabled = true;
scene.enablePhysics();
 
then on the meshes:
mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0, friction: 0.5, restitution: 0.7 });
 
Then, you can apply impulse.
 
But I don't use Cannon.js, and don't use physics at all. So I'm not the best person to help you on this. Sorry :(
Link to comment
Share on other sites

 

You want to move the mesh with physics enabled ?

 

In that case, you have to set one impostor to the mesh and set the physics, something like this:

 

scene.collisionsEnabled = true;
scene.enablePhysics();
 
then on the meshes:
mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0, friction: 0.5, restitution: 0.7 });
 
Then, you can apply impulse.
 
But I don't use Cannon.js, and don't use physics at all. So I'm not the best person to help you on this. Sorry :(

 

Sure, I have already set mesh, impostor etc. So as Ive already said its applying the impusle and box starts moving. But initial position setting is not working.

Link to comment
Share on other sites

Oh. Ok. that's different sorry :) I wasn't understanding your question.

 

Can you post a playgroung link os a jsfiddle ? 

 

Sorry I couldn't load babylon and cannon libs to jsfiddle. But if you have time you can take a look on js code on pastebin.

 

 

physics.js: 

http://pastebin.com/6JPGkG3R 

 

index.js:

http://pastebin.com/tw968ctF 

 

index.html:

http://pastebin.com/HmYyDKZ8

Link to comment
Share on other sites

I see a few problems here.  First, be aware that a function created or called with a scene.registerBeforeRender... runs EVERY FRAME, as far as I know.  I don't think you want to reset the box to initial position every frame.  If you do, your box will appear to never move at all.  Instead, make a function called 'init_box_position' and do not use scene.registerBeforeRender for that.  Only call the init_box_position() just before you apply a new impulse.

 

Also, notice that you named the box as 'Box'.

 

Your init_box_position function needs to 'look-up' the mesh named 'Box' before it can reposition it.  So, your function might look like this...

 

function init_box_position() {

 

   var scene = engine.scenes[0];  // look up the scene object

 

   var theBox = scene.getMeshByName("Box"); // look up the box mesh

 

   // init the positions...

   theBox.position.x = 0;
   theBox.position.y = 0;
   theBox.position.z = -25;

   // console.log("box position initialized");

}

 

IF you can send scene as an arg to the function... like this...

 

init_box_position(scene);

 

...then you won't have to use the line that looks-up the scene object.

 

Again, avoid using scene.registerBeforeRender.  That is for functions that run once per frame.

 

As always, I could be mistaken, but maybe not.  :)

Link to comment
Share on other sites

Hi again!  :)

 

I don't know if this will help, but here is a little demo of some physics stuff... and a zip file for it.  It uses a jQuery keypress function that listens for keys f, s, and r.  (fire, slide, reset).  It also has html buttons for each of those, as you can see.

 

For slide, you must repeatedly click the slide button (quickly), or you can hold-down the 's' key.  If the box is going real fast, it will go through the walls instead of bouncing-off of them (frame rates and physics sampling speeds cause that, I've heard).

 

And why the box is tumbling instead of sliding flat... I have no idea.  I have no Y value on the force direction for slide, and there is no friction value on the floor or box.  I would have thought it would slide flat, without tumbling.  Oh well, we can look at the reasons for that... some other day. 

 

Also, be advised that the reset key/button does not re-initialize the box position.  Instead, it .dispose() the old box and creates a new box.

 

If this demo doesn't do any help, it is at least ALMOST fun.  :)  Party on!

Link to comment
Share on other sites

Also, be advised that the reset key/button does not re-initialize the box position.  Instead, it .dispose() the old box and creates a new box.

Thank you very much, I also tried disposing object and recreating it and it worked for me!  :)

Link to comment
Share on other sites

For slide, you must repeatedly click the slide button (quickly), or you can hold-down the 's' key.  If the box is going real fast, it will go through the walls instead of bouncing-off of them (frame rates and physics sampling speeds cause that, I've heard).

I am experiencing exactly the same issue, I wonder is it possible to make it so that the box would never go through the walls? Is there any solution for that or should it be a new issue report..

Link to comment
Share on other sites

Hi again, yerzhik.  Good to hear that a few things are starting to work for you.

 

The "through the wall" and "through the floor" subject has come up a few times in the forum.  Rumor has it... that it is not a problem with the framework, nor a problem with cannon.js physics engine.  It is a problem with the speed of javascript.

 

There are a few things you could try.  Did you download the zip version of that polygun04 demo?  If so, you will see a file in the JS directory... called workbox01.js

 

Inside that file, you will see a setting... this.wallthickness = .2;  MAYBE, you might want to try increasing that value... maybe to 1 or even 2.  Although the box will go into the wall a little ways, this might give the physics engine more time to react to the collision.  No promises.  Maybe double layer walls... two thin walls with little or no gap.  Feel free to do some experimenting on this subject and please tell of us your discoveries.

 

Here is another physics demo:  http://urbanproductions.com/wingy/babylon/particlefun/splode/thump02.htm

 

View source on it, if you please.  That demo... does not have a separate file to make the workbox.  Instead, it uses the exact same workbox (though maybe bigger) as Deltakosh's demo from the BJS website.  Maybe using that method to make a rubber workbox... is a better method than using a separate file like I did.  *shrug*

 

In the old days, the boxes would fall through the floor A LOT!  Take a look at this demo....

 

http://urbanproductions.com/wingy/babylon/physics/rubber_room.htm

 

That was one of my first physics demos... and in earlier versions of the framework, the boxes would fall through the floor 70% of the time.  With newer versions of the framework, they rarely fall through the floor.  The problem of physics objects going through walls, floors, and ground... has improved quite a bit already, thanks to work from the authors of the framework.  And search the forum... for terms like "through the wall" and "through the floor" and similar.  *shrug*

 

Good luck.  No reason to report this as a new forum topic.  The framework authors (and many super-coders) read these posts, and they might write some comments about this.  I am essentially a newbie to 3D graphics... and to physics engines.  There are some very smart and experienced people that will read this, and hopefully they will add comments.  Be well!

Link to comment
Share on other sites

Hi again, yerzhik.  Good to hear that a few things are starting to work for you.

 

 

Hi Wingnut, thank you very much for so much information, I tried using the thick walls, now its working though sometimes its entering partially inside of the wall. I had to make the wall with the thickness equal to 50. Don't know wether its normal or not. But at least its not going through the wall. 

:)

Link to comment
Share on other sites

My pleasure, yerzhik.  Keep us posted if you discover more things. 

 

Some other things to try... is to put another box inside the first box, or maybe a box in a box in a box.  Set Physics State and collision detection on all of them... and maybe set box1.parent = box2, and box2.parent = box3... things like that.  I have never tried those things, but maybe. 

 

Or maybe a sphere inside a box, using the sphereImposter on the sphere, and the boxImposter on the outer box.  Again, don't forget to set parenting, so when you applyImpulse to one of the meshes... the other meshes will travel along.

 

I wonder if mesh size makes a difference.  Probably not. :)  Did you try the '2 thin walls' and '3 thin walls' method?  Curious.  Maybe you will become our physics expert, soon, yerzhik.  :)

 

A 50 thick wall is not normal.  There must be some 'tricks' we can learn to make physics objects quit going through walls and floors. I will study it more when I get a chance... and tell you about any discoveries.  Experts of the forum, help us out.  Tell us what you have learned about this subject, even though the topic is marked solved.  Thanks!

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