Sign in to follow this  
Gamerazor

Problem with physics

Recommended Posts

Hello

Im doing the game like arkanoid. The problem is when I try to move the bar with physics. When the bar has collided with the wall it lose the path and don't move straight to left to right. When I also try with moveWithCollisions() it works bad when the bar collide with the ball. How can I resolve this?

post-7795-758019_thumb.png

Share this post


Link to post
Share on other sites

ok the problem is resolved with:

 

barra = Balltingo.getMeshByID("Bar")
body = barra.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor,{mass: 1000, friction: 0.00001, restitution:0.00001})

 

move = (body,barra) ->
 s= 2
 if body.moveLeft
  barra.applyImpulse(new BABYLON.Vector3(0,0,-s),barra.position)
 else if body.moveRight
  barra.applyImpulse(new BABYLON.Vector3(0,0,s),barra.position)
 body.body.linearVelocity.scaleEqual(0.92)
 body.body.angularVelocity.scaleEqual(0)
 return

 

#this is coffescript

Share this post


Link to post
Share on other sites

Hi

 

Ahh, you made the bar very heavy... good idea!

 

You may still see the bar move a small amount every time the ball hits it.

 

Do you know that physics engines sometimes zero-out rotations on a mesh... when you do mesh.setPhysicsState?  (It is because they switch to quaternion-based rotation, I believe). 

 

Now, think about doing this (pseudo code) inside a render loop/animation loop:

 

bar.clearPhysicsState()

bar.fixMyBarZpos()  // reset bar z-position in case the ball has been "pounding it downward"

bar.setPhysicsState(just like initially)

 

You probably won't need a .fixMyBarRotation() because... as stated earlier... sometimes .setPhysicsState resets the rotation automatically.

 

Essentially, once per frame, you shut off physics for the bar, fix any positional problems with the bar, and then setPhysicsState on the bar again.... which should null-out any ball-caused rotation.

 

And maybe... IF you DO this... and then lower the mass on the bar again, MAYBE you will see the ball actually "shoot" away from a bar.  This happens because you are moving the bar forward just after a ball has hit the bar backwards a small amount.  When you move the bar forward (to normal placement), and re-set its physics state... the ball may still be touching the physics imposter for the bar.  So, the ball might "spring" off the bar with more restitution than you planned-for.  :)  But it's all fun.  Possibly, the lower the mass for bar, the more ball spring.  But you also have restitution settings on ball and bar... to adjust everything as you wish.

 

You might also try this same sneaky method for moving your bar left and right.  Instead of using applyImpulse to move the bar, use the animation loop:

 

bar.clearPhysicsState()

bar.adjustPos()  // reset bar z-position and check for +/-X movement needed (player input).  If needed, do it.

bar.setPhysicsState(just like initially)   // re-apply the physics state after the position adjustments.

 

Wow, huh?  Just a thought.  I don't know if this idea will help you, but maybe.  I HAVE used this method to position objects that were physics-active (most of the time).  :)

Share this post


Link to post
Share on other sites

thank you wingnut but i only had problem when the bar collided with the left wall but I have resolved this problem with this way:

move = (body,barra) -> s= 2.5 if body.moveLeft && barra.position.z > -5  barra.applyImpulse(new BABYLON.Vector3(0,0,-s),barra.position) else if body.moveRight  barra.applyImpulse(new BABYLON.Vector3(0,0,s),barra.position) body.body.linearVelocity.scaleEqual(0.92) body.body.angularVelocity.scaleEqual(0) return

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.