Physics perspective lua movement bug! Help me!

Here is my code:

-- require stuff local composer = require( "composer" ) local ids = require( "ids" ) local worldTable = require( "world" ) local perspective = require( "perspective" ) local physics = require( "physics" ) physics.start() physics.setDrawMode( "hybrid" ) local world = worldTable.world local camera = perspective.createView() -- add variables local x = display.contentCenterX local y = display.contentCenterY local w = display.actualContentWidth local h = display.contentHeight local scene = composer.newScene() -- creates the main scene function scene:create( event ) local view = self.view -- create bg local bg = display.setDefault( "background", 0, 0.5, 1 ) -- load up the world and show it local function loadWorld() -- make world size variables local worldWidth local worldHeight -- gets, sets, prints the world size local function getWorldSize() for i = 1, 1000 do if world[1][i] == nil then worldWidth = i - 1 print( "worldWith = "..worldWidth.."" ) break end end worldHeight = #world print( "worldHeight = "..worldHeight.."" ) end getWorldSize() -- this function creates the world from the table world local function showWorld() -- add variables local startBlockX = x - w / 2 local blockX = startBlockX local blockY = y - h / 2 local blockWidth = 40 local case -- make the blocks to the world for y = 1, worldHeight do for x = 1, worldWidth do case = ids.id[world[y][x]] if case.name ~= "air" then local rect = display.newImage( "assets/"..case.name..".png", blockX, blockY ) rect.width = blockWidth rect.height = blockWidth rect.id = case.id if case.solid == true then physics.addBody( rect, "static", { bounce = 0 } ) rect.name = "rect" end view:insert( rect ) camera:add( rect, 2 ) end blockX = blockX + blockWidth end blockX = startBlockX blockY = blockY + blockWidth end end showWorld() end loadWorld() local function createPlayer() local playerSpawnX = x local playerSpawnY = y local playerWidth = 40 local speed = 10 local dir = 0 local canJump = 1 local playerRect = display.newRect( playerSpawnX, playerSpawnY + 320, playerWidth, playerWidth ) physics.addBody( playerRect, { bounce = 0 } ) playerRect.name = "player" view:insert( playerRect ) camera:add( playerRect, 1 ) camera:setFocus( playerRect ) -- Set the focus to the player camera:track() -- Begin auto-tracking -- this function moves the player local function movePlayer( event ) local key = event.keyName if "down" == event.phase then if key == "a" then -- move left dir = -speed elseif key == "d" then -- move right dir = speed elseif key == "w" and canJump == 1 then playerRect:applyForce(0, -0.8) canJump = 0 end elseif event.phase == "up" then dir = 0 end end Runtime:addEventListener( "key", movePlayer ) local function movePlayer2( event ) playerRect.x = playerRect.x + dir playerRect.rotation = 0 end Runtime:addEventListener( "enterFrame", movePlayer2 ) local function collision( event ) if "began" == event.phase then canJump = 1 end end Runtime:addEventListener( "collision", collision) end createPlayer() end function scene:show( event ) end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

and here is video related to the problem:

https://youtu.be/ULcVPlL-hSg

so as you can see in the video, when I try to move right it moves little bit of the center of the screen to right and starts moving right, and when I release it stops moving and goes in the center of the screen. When I try to move to left it moves little bit left and starts moving and when I stop it goes in the center of the screen.

I want to make it so that when I press move right it goes right while staying in center of the screen and when I press move left it would stay in the middle of the screen.

Please help me if you can! :slight_smile:

Hi @Coder101,

I’m not familiar with how the third-party Perspective library works, but I notice you are doing almost everything for this game inside the “scene:create()” block, including functions for moving the player, detecting collisions, etc. This is going to cause problems later, if the scene view gets discarded (i.e. you exit the scene and then return to it later).

When using physics and Composer, the typical flow should be:

  1. In scene:create(), pause the physics engine as you create your world and objects in it… this ensures that the physical world doesn’t start moving/running while the scene is still off screen. Also, don’t start anything moving here via Runtime listeners, etc… that is best delayed until scene:show().

  2. In scene:show() resume (start) the physics engine again, thus making the scene come to life. Here is also the best place to start (add) Runtime listeners.

  3. In scene:hide() > “did”, pause the physics engine so it doesn’t keep running while the scene is off screen. This is also the time when you should stop  (remove) Runtime listeners that you started in scene:show().

In terms of functions for moving the player, detecting collisions, etc., those should almost always be above and outside any of the Composer-related blocks. If you nest them inside various blocks, objects may not have scope access to them when needed, and you’ll run into all sorts of problems from there.

Hope this helps somewhat (not the camera thing, but at least on some Composer stuff),

Brent

Ok, I will do the things you said but that was not the answer for the weird camera thing that I was looking for. I am though very well appreciated that you gived time and effor for writing me an useful answer. Before this I have always done everything in scene:create.

I have found answer. Instead of moving the physics object by changing it x like obj.x = obj.x + 1, I must applyForce to it. Because it is physics object.

Hi @Coder101,

I’m not familiar with how the third-party Perspective library works, but I notice you are doing almost everything for this game inside the “scene:create()” block, including functions for moving the player, detecting collisions, etc. This is going to cause problems later, if the scene view gets discarded (i.e. you exit the scene and then return to it later).

When using physics and Composer, the typical flow should be:

  1. In scene:create(), pause the physics engine as you create your world and objects in it… this ensures that the physical world doesn’t start moving/running while the scene is still off screen. Also, don’t start anything moving here via Runtime listeners, etc… that is best delayed until scene:show().

  2. In scene:show() resume (start) the physics engine again, thus making the scene come to life. Here is also the best place to start (add) Runtime listeners.

  3. In scene:hide() > “did”, pause the physics engine so it doesn’t keep running while the scene is off screen. This is also the time when you should stop  (remove) Runtime listeners that you started in scene:show().

In terms of functions for moving the player, detecting collisions, etc., those should almost always be above and outside any of the Composer-related blocks. If you nest them inside various blocks, objects may not have scope access to them when needed, and you’ll run into all sorts of problems from there.

Hope this helps somewhat (not the camera thing, but at least on some Composer stuff),

Brent

Ok, I will do the things you said but that was not the answer for the weird camera thing that I was looking for. I am though very well appreciated that you gived time and effor for writing me an useful answer. Before this I have always done everything in scene:create.

I have found answer. Instead of moving the physics object by changing it x like obj.x = obj.x + 1, I must applyForce to it. Because it is physics object.