Moving A Display Group With Physics

Hi guys,

I’m currently working on a top down aircraft shooter game and trying to implement my own camera function.

The basic function of this camera is that the screen will shift left / right when the plane is moving towards the edge of the screen.

Below is the code I’m currently using. 

 if (mainCharacter and mainCharacter.x \>= (displayWidth / 2 + 80)) then if (backgroundLayer.x \>= -20) then backgroundLayer.x = backgroundLayer.x - 0.2 cloudLayer.x = cloudLayer.x - 0.3 enemyobstaclesLayer.x = enemyobstaclesLayer.x - 0.3 end elseif (mainCharacter.x \<= (displayWidth / 2 - 80)) then if (backgroundLayer.x \<= 20) then backgroundLayer.x = backgroundLayer.x + 0.2 cloudLayer.x = cloudLayer.x - 0.3 enemyobstaclesLayer.x = enemyobstaclesLayer.x + 0.3 end end

The problem is that (as you might have guessed), shifting the position of a display group will cause the physics position to go off course. Is there any way around this?

I know there’s a Perspective Camera library available but I was unable to get it to work the way I want.

Any help is very much appreciated. Thanks in advance :slight_smile:

Download my answer packs:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015%20Answer%20Packs.zip

Unzip and run the answer pack for: July

Run the example #5 (see numbers on button):  

Look at the code under: ~/answers/cameraYOnly (can be expanded to X and Y)

The key is to

A. Use a nested display group structure like this:

World 

        |

        |–> Layer 1

        |

        |–> …

        |

        |–> Layer N

B. Place all objects in one of the layers.

C. Track one specific object (in the layers; usually the player) and note its last position versus current position each frame.

D. Move the parent group ‘World’ by the opposite of the player delta.

This will keep all of the objects (in the layer) aligned while the player ‘seems’ to stay in the center of the screen.

Download my answer packs:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015%20Answer%20Packs.zip

Unzip and run the answer pack for: July

Run the example #5 (see numbers on button):  

Look at the code under: ~/answers/cameraYOnly (can be expanded to X and Y)

The key is to

A. Use a nested display group structure like this:

World 

        |

        |–> Layer 1

        |

        |–> …

        |

        |–> Layer N

B. Place all objects in one of the layers.

C. Track one specific object (in the layers; usually the player) and note its last position versus current position each frame.

D. Move the parent group ‘World’ by the opposite of the player delta.

This will keep all of the objects (in the layer) aligned while the player ‘seems’ to stay in the center of the screen.