Camera wont reset its position, (plug and play code provided)

Hi all.

I’ve got a problem with my camera system.

The “cam” function at the bottom should move the background group by the inverse of the players velocity, (which is monitored each frame by the “velocity” function).

Everything works perfectly, but the background will not reset back to its original position. With each bounce the background gets lower and lower.

I’m aware of the problem with box2D, whereby an object will bounce slightly higher each time, but that shouldn’t effect the background moving back to the same position each time.

Spider.

[code]
physics = require(“physics”)
require “sprite”
physics.start()
physics.setGravity(0,9.8)

_H = display.contentHeight;
_W = display.contentWidth;

local masterGroup = display.newGroup()
local backgroundGroup = display.newGroup()
local playerGroup = display.newGroup()

masterGroup:insert(backgroundGroup)
masterGroup:insert(playerGroup)

player1 = display.newRect (0, 0, 30, 30)
player1.x = _W / 2
player1.y = 100
player1:setFillColor(0, 0, 255)
physics.addBody(player1, {density = 1.0, friction = 0, bounce = 1})
playerGroup:insert(player1)

function velocity() – gets the players velocity in x and y
vx, vy = player1:getLinearVelocity()
end
Runtime:addEventListener(“enterFrame”, velocity) --checks the players velocity each frame

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})
playerGroup:insert(ground)

local textObject = display.newText( “Background”, 0, 0, “courier”, 60 )
textObject.x = _W / 2
textObject.y = _H / 2
textObject:setTextColor( 255, 0, 0 )
textObject.rotation = 46
backgroundGroup:insert(textObject)

function cam()
if player1.y < 96 then
repeat masterGroup.y = masterGroup.y - vy/20 until masterGroup.y <= _H
end
end
Runtime:addEventListener(“enterFrame”, cam)
[/code] [import]uid: 67933 topic_id: 21596 reply_id: 321596[/import]

Hello,
I changed your function to:

function velocity() -- gets the players velocity in x and y  
 -- vx,   
 vy = player1:getLinearVelocity()  
end  
  

I don’t think your using vx…
hope it helps…or gets you in the right direction…

Larry
[import]uid: 107633 topic_id: 21596 reply_id: 85639[/import]

Hey Sharp.

Thanks for the prompt reply.

I changed the function as you posted, but unfortunately it stops the camera from working altogether.

vy is the players velocity in the y direction. The background should move down whilst the player object moves up and vice-versa.

The problem is not with the motion of the camers, but with the background not moving back to the same position after each bounce.

Thanks for posting, any other ideas you have would be most welcome?

Spider [import]uid: 67933 topic_id: 21596 reply_id: 85647[/import]

OK, here’s what I don’t understand.

according to the API listing;

vx, vy = myBody:getLinearVelocity()  

will get the x and y velocity per second. In my code above I divide that by 30 to get the amount per frame, (i know it says 20 above, but ignore that), and it still doesn’t work.

How can I get the background to return to it’s original position with each bounce?

Please can someone take a look?

Thanks [import]uid: 67933 topic_id: 21596 reply_id: 85825[/import]