Problems with getLinearVelocity.

Hi everyone.

I’m trying to use “myBody:getLinearVelocity()” to find the x velocity of my player character. I need to pass this variable to a “camera” I’ve set up, so that the background moves at the same speed as the player.

The problem is that “myBody:getLinearVelocity()” prints “0” to the terminal, even though the character is in motion.

My code looks like this:

vx, vy = player1:getLinearVelocity()  
print (vx)  

Does anyone have any idea how to fix this?

Thanks
[import]uid: 67933 topic_id: 16175 reply_id: 316175[/import]

you get two return values, vx and vy, which are the linear velocities on the x axis and the y axis respectively.

You are printing vx which is 0, this means that your physics object is falling vertically, the linear velocity is in vy, that you have missed to look for.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16175 reply_id: 60180[/import]

By any chance are you adding the object to physics as “static”?

Maybe you need to add it as “dynamic” instead

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16175 reply_id: 60190[/import]

Ok guys,

To explain a little further, my app is running in landscape, hence my interest in the vx, rather than vy.

I have managed to get my camera system working by adding a runtime (onEnterFrame) listener to my camera function as follows:

function dynamicCam()  
  
 canCamMove = false  
  
 function cam()  
 if canCamMove == true then -- canCamMove is set to true when board collides with halfpipe.  
 if player1.x \> 250 then   
 masterGroup.x = masterGroup.x - vx/20 -- instead of "1" use velocity of player1  
 --print("works")  
 end  
 end  
  
 if canCamMove == true then -- moves it back into initial position  
 if player1.x \< 250 then  
 if masterGroup.x \< 0 then  
 masterGroup.x = masterGroup.x + vx/20  
 print (masterGroup.x)  
 end  
 end  
 end  
  
 end  
  
 Runtime:addEventListener("enterFrame", cam)   
end  
  
dynamicCam()  

The problem I now have is that whilst the “masterGroup,” (which contains all necessary graphics assets, background, physics objects, player, sensors etc) moves by the value of vx, the motion is WAY too fast.

The second if statement inside my function should set the mastergroup back to it’s original position but it throws up a weird kind of stuttering error.

I would love to post the full plug and play code, but the whole thing is nearly 1000 lines long now. I will post the whole thing for everyone once the final app is published though :wink:

Anyone have any idea why the speed of the background movement is so fast, and why the background doesn’t reset back to its’ original position?

Thanks
[import]uid: 67933 topic_id: 16175 reply_id: 60202[/import]

Do you have any physics bodies in your masterGroup? I’m not sure about this but I think the reason why your group doesn’t reset is because when you move an entire group comprising of physics bodies,
it shows some weird behaviour. You will have to move each object individually. [import]uid: 64174 topic_id: 16175 reply_id: 60221[/import]

Hey Satheeth.

I do have physics bodies in the group, (the player and halfpipe are all physics objects) but I don’t think that’s whats causing the problem.

Initially I had it set up to move the “masterGroup” in increments of “1” which worked perfectly. I wanted to move the “masterGroup” by the same velocity as the player, but in the opposite direction, that way I could control the players exact position on screen.

Do you know why the “masterGroup” moves so much faster, (about 10X)
than the player, when the values are the same?

I will try and work up some plug and play code to demonstrate the problem, but it’s gonna take a while.

Thanks [import]uid: 67933 topic_id: 16175 reply_id: 60323[/import]

Ok, so I’ve written some drag and drop code which demonstrates the problem. I have 2 problems, firstly the movement of the “masterGroup” is MUCH faster than the movement of the player, despite them both moving by “vy.” To correct this in the example I’ve set the “masterGroup” to move by “vy/20” to reduce the speed.

The second problem is that over time, the whole “masterGroup” seems to sink off the screen. It’s as if it doesn’t quite return it’s position to 0.

Can someone please take a look and let me know where I’m going wrong, it’s driving me round the bend!

physics = require("physics")  
physics.start()  
physics.setGravity(0,9.8)  
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.  
  
\_H = display.contentHeight;  
\_W = display.contentWidth;  
  
local masterGroup = display.newGroup()  
local plangroup = display.newGroup()  
local playergroup = display.newGroup()  
  
masterGroup:insert(plangroup)  
masterGroup:insert(playergroup)  
  
function physObjects()  
player1 = display.newRect (0, 0, 30, 30)  
player1.x = \_W / 2 + 150  
player1.y = 41 + player1.contentHeight / 2 -- was 35  
player1:setFillColor(0, 0, 255)  
physics.addBody(player1, {density = 1.0, friction = 0, bounce = 1})  
player1.myName = "player1"  
playergroup:insert(player1)  
  
function velocity()  
 vx, vy = player1:getLinearVelocity()  
 --print (vy)  
end  
Runtime:addEventListener("enterFrame", velocity)   
  
player1.collision = onLocalCollision  
player1:addEventListener( "collision", player1 )  
  
ground = display.newRect (0, 0, \_W, 5)  
ground.x = \_W / 2  
ground.y = \_H - ground.contentHeight / 2  
ground:setFillColor(0, 255, 0)  
physics.addBody(ground, "static", {density = 1.0, friction = 0, bounce = 0.2})  
ground.myName = "ground"  
playergroup:insert(ground)  
  
end  
  
function background()  
back1 = display.newRect (0, 0, \_W, 96)  
back1.x = \_W / 2  
back1.y = \_H - back1.contentHeight / 2  
back1:setFillColor(244,244,244)   
plangroup:insert(back1)  
  
back2 = display.newRect (0, 0, \_W, 96)  
back2.x = \_W / 2  
back2.y = \_H - back1.contentHeight / 2 - 96  
back2:setFillColor(215,215,215)   
plangroup:insert(back2)  
  
back3 = display.newRect (0, 0, \_W, 96)  
back3.x = \_W / 2  
back3.y = \_H - back1.contentHeight / 2 - 96 \* 2  
back3:setFillColor(174,174,174)   
plangroup:insert(back3)  
  
back4 = display.newRect (0, 0, \_W, 96)  
back4.x = \_W / 2  
back4.y = \_H - back1.contentHeight / 2 - 96 \* 3  
back4:setFillColor(120,120,120)   
plangroup:insert(back4)  
  
back5 = display.newRect (0, 0, \_W, 96)  
back5.x = \_W / 2  
back5.y = \_H - back1.contentHeight / 2 - 96 \* 4  
back5:setFillColor(65,65,65)   
plangroup:insert(back5)  
end  
  
function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
  
 ----------------------------------player hits ground---------------------------------------------------------------------------  
 if (self.myName == "player1" and event.other.myName == "ground") then  
 canCamMove = true  
 end  
 end  
end   
  
function dynamicCam()  
  
 canCamMove = false  
  
 function cam()  
 if canCamMove == true then -- canCamMove is set to true when board collides with halfpipe.  
 if player1.y \< 100 then   
 masterGroup.y = masterGroup.y - vy/20 -- instead of "1" use velocity of player1  
 --print("works")  
 end  
 end  
  
 if canCamMove == true then -- moves it back into initial position  
 if player1.y \> 250 then  
 if masterGroup.y \< 0 then  
 masterGroup.y = masterGroup.y + vy/20  
 --print (masterGroup.y)  
 end  
 end  
 end  
  
 end  
  
 Runtime:addEventListener("enterFrame", cam)   
end  
  
function startGame()  
 physObjects()  
 background()  
 dynamicCam()  
end  
  
startGame()  

[import]uid: 67933 topic_id: 16175 reply_id: 60333[/import]

About problem #2:

masterGroup keeps sinking because player1 bounces a little bit higher every time, therefore having higher vertical velocity.

According to http://developer.anscamobile.com/content/game-edition-physics-bodies :

You may find that an object with a bounce set to exactly 1.0 will rebound somewhat higher than its original height, due to numerical approximations in Box2D. Bounce values slightly below 1.0, possibly combined with some linear damping, may be used for a closer simulation of “same height” behavior over short periods of time. Over long periods of time, it may be difficult to avoid runaway or decaying bounce.

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16175 reply_id: 60454[/import]

Miu, thanks for the reply.

I know Box 2D can be a little problematic. The example of the bouncing box is not really representative of the gameplay in my final app, it’s just an example of an object moving up and down, in order to demonstrate the problem with the “camera” code.

Whilst I appreciate the reason for the box bouncing a little higher each time, do you know of a way I can get the masterGroup to return to it’s initial position whilst the box is falling?

Thanks [import]uid: 67933 topic_id: 16175 reply_id: 60466[/import]

I tried a couple of ideas but none of them worked as I expected.

It’s weird that the blue box keeps bouncing even if the ground is not visible on screen.

When I print the coordinates of the ground inside the collision listener I alyas get the same number but clearly the green box is below the y-limit of the screen, I haven’t found a solution yet, I’ll let you know if I come up with something.

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16175 reply_id: 60622[/import]