how i can restart the velocity of the player

hi my issue is that i apply a Linear Impulse to the player… and when the player hit the left wall
i stop the physics because i want to simulated that is adhered to it… but when i apply impulse again, the direction is different than the real direction… i think is taking the new and old impulse so is there is a why to reset this? help :smiley:
[lua] player:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y)
function glue (self,event)
if(event.phase==“began”)then
if self.name == “wall” and event.other.name ==“player” then
physics.pause();
–player:applyLinearImpulse(event.x-event.xStart , event.y -event.yStart )
end
end
end

leftwall.collision = glue
leftwall:addEventListener(“collision”,leftwall)[/lua] [import]uid: 138440 topic_id: 29051 reply_id: 329051[/import]

By pausing the physics, you give the appearance that the object is sticking to the wall, but that’s only because the entire “world” is frozen. In fact, the object is still moving toward the wall, and it’s just about to bounce off it, which is what you will see once the physics simulation resumes.

Instead of pausing the physics, you should consider making the object “stick” to the wall some other way. You could try setting its x,y velocity to 0 using [lua]setLinearVelocity[/lua] (http://developer.coronalabs.com/reference/index/bodysetlinearvelocity) (which should work if you’re not using gravity), or you could try creating a weld joint between the object and the wall (which you would later remove when the don’t want the object to be stuck anymore).

  • Andrew [import]uid: 109711 topic_id: 29051 reply_id: 116951[/import]

What Andrew says is correct. Note also that linear impulses are additive, as in, if you apply an impulse from the opposite side of an object’s linear path, it will offset the current velocities. Setting the actual linear velocities using setLinearVelocity(), however, is a direct value setting.

Brent [import]uid: 9747 topic_id: 29051 reply_id: 116960[/import]

Thanks for your answers i am going to take a look on setLinear an joints and then i can decide which is better. IF you know where i can find tutorials or basic info of join please tell me… [import]uid: 138440 topic_id: 29051 reply_id: 117073[/import]

Hi there,
Here’s the documentation on joints. They can be a little tricky in the beginning, but once you see how the parameters are set up, it’s fairly easy.

Best of luck!

http://developer.coronalabs.com/content/game-edition-physics-joints
[import]uid: 9747 topic_id: 29051 reply_id: 117077[/import]

hi again… i am learning joints… i can created now and do some stuff… but when i try to apply joints on collision i get and error that say “physics.newJoint() cannot be called on collision” so i try to use precollision and is the same so help please…

[lua]function glue(event)

if event.phase ==“began” and event.other.name ==“wall” then
print(“hi”)
J = physics.newJoint( “weld”, player, leftwall,player.x,player.y)

elseif(event.phase ==“ended”) then
– J = physics.newJoint( “weld”, player, leftwall,player.x,player.y)

end
end

player:addEventListener( “collision”, glue )[/lua] [import]uid: 138440 topic_id: 29051 reply_id: 117196[/import]

also i fix my issue with the setLinearImpulse but i still using physics.stop() and i really want to use joints… so if you can help me i will be grateful… [import]uid: 138440 topic_id: 29051 reply_id: 117207[/import]

Hi again,
There are several actions in the physics engine which can’t be performed immediately upon collision, because Box2D needs to work out the math and calculations before it proceeds. To simple solution is to create the joint a few milliseconds afterward, which the player will never notice.

So, to create the joint in this case, you just need to do it using a very, very short timer. This basically allows the collision to “resolve” before the joint is created.

local function createJoint()  
 J = physics.newJoint( "weld", player, leftwall,player.x,player.y)  
end  
  
--in the collision handler, call the function on a 10 millisecond timer  
timer.performWithDelay( 10, createJoint )  

It’s basically that simple! As a further note, you should localize everything including the joint (right now, it’s global). In fact, you’ll need to pre-define the joint somewhere initially (as in, create a reference variable to it), so later you can refer to it and remove the joint, thus releasing the player from the wall.

Brent [import]uid: 9747 topic_id: 29051 reply_id: 117225[/import]

Thank you so much it works great!!! thanks for helping me and all people in this forum [import]uid: 138440 topic_id: 29051 reply_id: 117253[/import]