Detect ball speed to slow camera

Hi guys, i have a ball rolling with physics and a camera that follows it, All good except i want to slow and stop the camera depending on the speed of the ball.

Any ideas how i would acheive this?

My code for the camera based on an exmample i found on here…

local function moveCamera(event)
if ball.x > 100 then
group.x = group.x - 1
end
end
Runtime:addEventListener( “enterFrame”, moveCamera )

Thanks

[import]uid: 162458 topic_id: 29613 reply_id: 329613[/import]

Meh, i hate dealing with cameras.

Assuming your ball is in group i think the code below will make the camera truly follow ball on the X axis no matter what the speed. I don’t really have a project to test it on atm but I believe it’s what I used for the base of my own camera needs.

[lua]local function moveCamera(event)
if ball.x > 100 then
group.x = -ball.x + group.x
end
end
Runtime:addEventListener( “enterFrame”, moveCamera )

Thanks[/lua]
[import]uid: 147305 topic_id: 29613 reply_id: 118948[/import]

I’m not that good at cameras either but maybe you can somehow determine the speed of the rolling ball (distance moved over time) and then if it dips below a certain speed threshold stop the camera movement(group.x position). As far as “slowing down” the camera maybe you can have the group.x position slowly start to lag behind the rolling ball’s x coord again maybe based off speed?
[import]uid: 58885 topic_id: 29613 reply_id: 118957[/import]

thanks budershank but that causes the screen to shoot off far right.

Bit stuck with this to be honest.

Not sure how to calculate the acutal speed the ball is moving?

Thanks [import]uid: 162458 topic_id: 29613 reply_id: 119012[/import]

So it does, I’m home now so looked up what I actually use instead of me trying to remember.

[lua]local function moveCamera(event)
group.x = -ball.x + display.contentWidth / 2
end
Runtime:addEventListener( “enterFrame”, moveCamera )[/lua]

As mentioned in my other reply, all this does is keep your camera centered on the ball on the X axis. This means that tracking the ball speed itself is irrelevant.

Even so, knowing how to get velocity is important. If you are using physics then you can just use the getLinearVelocity method which will return the x and y velocity.

http://docs.coronalabs.com/api/type/Body/getLinearVelocity.html [import]uid: 147305 topic_id: 29613 reply_id: 119032[/import]

Awesome. Thanks very much, it works great. [import]uid: 162458 topic_id: 29613 reply_id: 119044[/import]