How to create a camera that is focused on the player

How can I create a camera that sort of centers itself on where the character is moving? Also, how can I program it so that it does not center itself when the character reaches the edge of the map?

My main interest here is for tutorials that can help walk me through this. (A Corona Geek Hangout would be optional)

Thank you!

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/camera/#tracking

See the SSK2 code for the tracking camera.

https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/easyCamera.lua

PS - There are many camera solutions out there, but since you have SSK2 I’m pointing you in that direction.

The trick of all cameras is:

  • Use a group to contain ‘world objects’
  • Specific one object as the ‘target’ or ‘focus’ of the camera.  This object is in the ‘world’ group.
  • When the target moves, move the ‘world group’ in the opposite direction.
  • Typically (in Corona) this is done with a enterFrame listener.

Again, see my code to understand this fundamental gaming concept.

Thanks!

OMG, so much new stuff.

Quick question, then this topic will be wrapped up for good, is a factory similar to a function such as display.newCircle, just with custom properties? 

 
I assume you are referring to my ‘display object’ factories: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/
 
If so, my ‘factories’ are really replacement builders that take parameter tables to configure the objects.  
 
These ‘factories’ internally call the corona functions to do the work, but the ‘factories’ do a lot of bookkeeping and extra work so my code can be short.
 
I do this, because I HATE typing long sequence of code to do the same things over and over.
 
See this page for examples of ‘factory’ calls calls:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_samples/
 
While I don’t show the long hand versions of the code, let me break one down for you.
 

  1. Look here: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_samples/#bouncing-block-simple-physics
     

  2. Here is the SSK2 code:

    local group = display.newGroup() newImageRect( group, centerX - 100 , centerY - 50, “images/kenney/physicsAssets/yellow_round.png”, { size = 40 }, { radius = 20, bounce = 1, gravityScale = 0.2 } ) newImageRect( group, centerX - 100, centerY + 100, “images/kenney/physicsAssets/stone/square2.png”, { size = 40 }, { bodyType = “static” } )

  3. Here is the long-hand Corona equivalent:

    local physics = require “physics” local group = display.newGroup() local ball = display.newImageRect( group, “images/kenney/physicsAssets/yellow_round.png”, 40, 40 ) ball.x = display.contentCenterX - 100 ball.y = display.contentCenterY - 50 physics.addBody( ball, { radius = 20, bounce = 1 ) ball.gravityScale = 0.2 local stone = display.newImageRect( group, “images/kenney/physicsAssets/stone/square2.png”, 40, 40 ) stone.x = display.contentCenterX - 100 stone.y = display.contentCenterY + 100 physics.addBody( stone, “static” )

Question 1:  Which one would you rather type over and over?
 
Question 2:   Which one would you rather maintain?
 
Question 3:   Which one would is more legible and easier to understand?
 
If you said the ‘first one’ then you agree with me and that is why I use SSK2.
 
 
Is is worth noting I have other factories too:

PS - All of my display object factories have a consistent signature unlike the Corona methods which vary a little. This is another plus IMHO.

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/camera/#tracking

See the SSK2 code for the tracking camera.

https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/easyCamera.lua

PS - There are many camera solutions out there, but since you have SSK2 I’m pointing you in that direction.

The trick of all cameras is:

  • Use a group to contain ‘world objects’
  • Specific one object as the ‘target’ or ‘focus’ of the camera.  This object is in the ‘world’ group.
  • When the target moves, move the ‘world group’ in the opposite direction.
  • Typically (in Corona) this is done with a enterFrame listener.

Again, see my code to understand this fundamental gaming concept.

Thanks!

OMG, so much new stuff.

Quick question, then this topic will be wrapped up for good, is a factory similar to a function such as display.newCircle, just with custom properties? 

 
I assume you are referring to my ‘display object’ factories: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/
 
If so, my ‘factories’ are really replacement builders that take parameter tables to configure the objects.  
 
These ‘factories’ internally call the corona functions to do the work, but the ‘factories’ do a lot of bookkeeping and extra work so my code can be short.
 
I do this, because I HATE typing long sequence of code to do the same things over and over.
 
See this page for examples of ‘factory’ calls calls:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_samples/
 
While I don’t show the long hand versions of the code, let me break one down for you.
 

  1. Look here: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_samples/#bouncing-block-simple-physics
     

  2. Here is the SSK2 code:

    local group = display.newGroup() newImageRect( group, centerX - 100 , centerY - 50, “images/kenney/physicsAssets/yellow_round.png”, { size = 40 }, { radius = 20, bounce = 1, gravityScale = 0.2 } ) newImageRect( group, centerX - 100, centerY + 100, “images/kenney/physicsAssets/stone/square2.png”, { size = 40 }, { bodyType = “static” } )

  3. Here is the long-hand Corona equivalent:

    local physics = require “physics” local group = display.newGroup() local ball = display.newImageRect( group, “images/kenney/physicsAssets/yellow_round.png”, 40, 40 ) ball.x = display.contentCenterX - 100 ball.y = display.contentCenterY - 50 physics.addBody( ball, { radius = 20, bounce = 1 ) ball.gravityScale = 0.2 local stone = display.newImageRect( group, “images/kenney/physicsAssets/stone/square2.png”, 40, 40 ) stone.x = display.contentCenterX - 100 stone.y = display.contentCenterY + 100 physics.addBody( stone, “static” )

Question 1:  Which one would you rather type over and over?
 
Question 2:   Which one would you rather maintain?
 
Question 3:   Which one would is more legible and easier to understand?
 
If you said the ‘first one’ then you agree with me and that is why I use SSK2.
 
 
Is is worth noting I have other factories too:

PS - All of my display object factories have a consistent signature unlike the Corona methods which vary a little. This is another plus IMHO.