Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Physics isn't working properly!
Started by InfiSnyp Jun 02 2015 12:56 PM

- - - - -
18 replies to this topic

Best Answer InfiSnyp , 04 June 2015 - 05:56 PM

ya sorry i was just getting really confused... im marking this one solved...

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Ok so my physics just doesnt want to work for one image!

 

heres the code to the lua file

local composer = require( "composer" )
local scene = composer.newScene()


local _W = display.contentWidth
local _H = display.contentHeight

local physics = require("physics" )
physics.setScale( 30 )
physics.setGravity( 0, 0 )
physics.start( )

physics.setDrawMode( "hybrid" ) 









function scene:create( event )

   local sceneGroup = self.view
   physics.start()
----------------------------
local BackGround = display.newImageRect("BackGround.png", 1080, 1920)




 sceneGroup:insert( BackGround )
 


end




function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
      physics.start()




  



    elseif ( phase == "did" ) then
      physics.start()
        local circle = {}


local dragCircle = function( event )
   local target = event.target
   local phase = event.phase

   if ( event.phase == "began" ) then
      local parent = target.parent
      display.getCurrentStage():setFocus( target ) 
      target.isFocus = true
      target.x0 = event.x - target.x
      target.y0 = event.y - target.y
      target.xStart = target.x
      target.yStart = target.y
      target:toFront()
   elseif ( target.isFocus ) then
      if ( phase == "moved" ) then
         target.x = event.x - target.x0
         target.y = event.y - target.y0
      elseif ( phase == "ended" or phase == "cancelled" ) then
         display.getCurrentStage():setFocus( nil )
         target.isFocus = false
    transition.to( event.target, {time=500, x=event.target.xOrig, y=event.target.yOrig} )
      end
   end
  return true
end



local spawnCircles = function ()
    local FallDown = math.random(display.contentWidth * 0.2, display.contentWidth * 0.8 )
    circle = display.newImageRect("circle.png", 31, 31 )
    circle.x = FallDown
    circle.y = -70
    physics.addBody( circle, "dynamic", {density = .01, friction = 0.5, bounce = 0.1 })
    circle.gravityScale = 0.1 
    physics.setGravity( 0, 10 )
    circle:addEventListener( "touch", dragCircle )
    
    

    end

  


myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time 
    






    end
return true
end


-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene

and if i add 

local floor = display.newImageRect("floor.png", 351, 32 )
  floor.x = display.contentWidth * 0.5
  floor.y = 520
  physics.addBody(floor, "static")

if i add the floor to create scene it starts to duplicate the circles falling down .. and las out corona sdk

 

if i add it into the "will" part then is stops duplicating my circles and physics doesnt work

 

and if i put it into the "did" part its starts dulpicating the cirlces and lags out corona.. but it does start physics..

 

 

please help my on this.. and when physics doesnt work for that floor it gives me this error code in the console...

physics.addBody cannot be called when the world is locked and in the middle of number crunching such as during a collision event

 

if you guys would like me to make a youtube vid explaining all this i can in a little bit.. ive been stuck for weeks and cant figure it out.. thanks :(  :D



[TOPIC: post.html]
#2

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

and btw the dragCirlce doesnt affect anything



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Please do the following and I'll take a look:

 

1. Get rid of the massive code dump at the top.

 

2. Write a new short example in a separate main.lua file with just these parts:

  • physics initialization
  • drag listener (seems like you want to do this)
  • object creation (no PNGs, just rects and circles)
  • addEventListener() calls

i.e. Minimize the problem so I don't have to copy all your code, modify it, etc. just to look at the problem.  

 

3. Then, if you haven't found the problem at that point, paste the code back into your original post and make sure it is well formatted and indented.  

  • remove tabs before pasting and replace with three spaces
  • no long sections of blank lines
  • proper indentation so I can logically separate functions, if statements, etc. w/o needing to read every line

 

4. Post below saying its updated. 

 

I'll be watching.


Edited by roaminggamer, 02 June 2015 - 05:57 PM.


[TOPIC: post.html]
#4

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

thanks for the reply !! well see if i put the code into main.lua without the composer setup it work perfect.. as soon as i put in composer and try adding physics to the floor it starts messing things up..



[TOPIC: post.html]
#5

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Look ive made it a simple as i possibly can and its still not working correctly ...

local composer = require( "composer" )
local scene = composer.newScene()


local physics = require("physics" )
physics.setScale( 30 )
physics.setGravity( 0, 0 )
physics.start( )

physics.setDrawMode( "hybrid" ) 




function scene:create( event )

    local sceneGroup = self.view
local BackGround = display.newImageRect("BackGround.png", 1080, 1920)

local floor = display.newImageRect("floor.png", 351, 32)
    floor.x = display.contentWidth * 0.5
    floor.y = 510
    physics.addBody("static")


end







-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene

if you remove "physics.addBody("static")" then theres no error but if you add it it gives an error in the cosole... and the physics doesnt work



[TOPIC: post.html]
#6

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

if i put this code into the main.lua it gives me an error for this

local physics = require("physics" )
physics.setScale( 30 )
physics.setGravity( 0, 0 )
physics.start( )

physics.setDrawMode( "hybrid" ) 



local BackGround = display.newImageRect("BackGround.png", 1080, 1920)

local floor = display.newImageRect("floor.png", 351, 32)
    floor.x = display.contentWidth * 0.5
    floor.y = 510
    physics.addBody("static")    <----------- that



[TOPIC: post.html]
#7

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 469 posts
  • Corona SDK

In your shortened example, the error you are getting is because you don't specify a object to add a physics body to. Instead of
physics.addBody("static")
try
physics.addBody(floor, "static")


Now that may or may not fix your original problem, but at the very least it's an issue that would cause an error in your shortened test scenario.

Keep plugging away - these frustrating situations often yield the best learning experiences, so don't let it get you down!

[TOPIC: post.html]
#8

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Dang!  I missed responding, but Jason is right, got tell it what object to add the body to.

 

-Ed



[TOPIC: post.html]
#9

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

well the

 

physics.addBody(floor, "static")

 

worked in the main.lua but didnt in the

local composer = require( "composer" )
local scene = composer.newScene()


local physics = require("physics" )
physics.setScale( 30 )
physics.setGravity( 0, 0 )
physics.start( )

physics.setDrawMode( "hybrid" ) 




function scene:create( event )

    local sceneGroup = self.view
local BackGround = display.newImageRect("BackGround.png", 1080, 1920)

local floor = display.newImageRect("floor.png", 351, 32)
    floor.x = display.contentWidth * 0.5
    floor.y = 510
    physics.addBody("static")


end







-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene


[TOPIC: post.html]
#10

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Three things:

 

1. Please re-edit after posting code and remove the extra blank lines.  This will make code posts more legible.

 

2. What editor are you using?  I'm seeing lots of code pastes like this (extra lines) and I suspect the common factor is the editor folks are using.

 

3. You made the same mistake:

physics.addBody("static")

should be:

physics.addBody( floor, "static")


[TOPIC: post.html]
#11

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

local composer = require( "composer" )
local scene = composer.newScene()

--------------------------------------------
local physics = require("physics" )
physics.setScale( 30 )
physics.setGravity( 0, 0 )
physics.start( )

physics.setDrawMode( "hybrid" ) 
---------------------------------------
function scene:create( event )

    local sceneGroup = self.view
local BackGround = display.newImageRect("BackGround.png", 1080, 1920)

local floor = display.newImageRect("floor.png", 351, 32)
    floor.x = display.contentWidth * 0.5
    floor.y = 510
    physics.addBody(floor, "static")


end
------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene

is that good? this is what i tried in my game.lua and it still doesnt start physics for the floor..

and im using Sublime Text 2



[TOPIC: post.html]
#12

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

ok so i figured it out.. i put the floor image into my "did" part of my scene on the show scene and it worked but now its duplicating my objects.. like scene show is duplicating objects heres the code...

local composer = require( "composer" )
local scene = composer.newScene()

local physics = require( "physics" )
physics.start( )

physics.setDrawMode( "hybrid" ) 

function scene:create( event )

    local sceneGroup = self.view
local BackGround = display.newImageRect("BackGround.png", 1080, 1920)

end

function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
      
    elseif ( phase == "did" ) then
       local floor = display.newImageRect("floor.png", 351, 32)
    floor.x = display.contentWidth * 0.5
    floor.y = 510
     physics.addBody( floor, "static" )
-----------------------------------------------
local circle = {}
local spawnCircles = function ()
    local FallDown = math.random(display.contentWidth * 0.2, display.contentWidth * 0.8 )
    circle = display.newImageRect("circle.png", 31, 31 )
    circle.x = FallDown
    circle.y = -70
    physics.addBody( circle, "dynamic", {density = .01, friction = 0.5, bounce = 0.1 })
    circle.gravityScale = 0.1 
    physics.setGravity( 0, 10 )   

    end  

myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time
    end
return true 
end
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene

im also attaching images so you can try the code youself..

 

if you prefer i make a video please tell me thanks!

Attached Thumbnails

  • Background.png
  • circle.png
  • floor.png


[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

Three things:

 

1. Don't init physics in a composer scene unless you absolutely need to.  Most times you can do it in main and simply adjust gravity or pause and restart physics later.

 

2. Be sure you'e not starting physics multiple times and also that you're not accidentally stopping it somewhere.  Starting multiple times is probably fine, but bad practice.

 

3. This code should work:

 

main.lua

-- First init physics in main.lua
local physics = require("physics" )

physics.start() -- Gotta start before ...

physics.setGravity( 0, 0 ) -- ... setting gravity  (you've been doing it backwards)

physics.setDrawMode( "hybrid" ) 

---------------------------------------------------
-- Do what you normally do to load your scene here
---------------------------------------------------


yourscene.lua

local composer = require( "composer" )
local scene    = composer.newScene()
local physics  = require("physics" ) -- still need to require in composer to use it.

function scene:create( event )
   local sceneGroup = self.view

   --Note: Another issue.  You were not inserting your objects into the sceneGroup

   local BackGround = display.newImageRect( sceneGroup, "BackGround.png", 1080, 1920 )

   local floor = display.newImageRect( sceneGroup, "floor.png", 351, 32 )
   floor.x = display.contentWidth * 0.5
   floor.y = 510

   physics.addBody( floor, "static" )
end 

... rest of scene contents goes below


Edited by roaminggamer, 04 June 2015 - 10:00 AM.


[TOPIC: post.html]
#14

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

local composer = require( "composer" )
local scene = composer.newScene()

local physics = require( "physics" )
physics.start( )

physics.setDrawMode( "hybrid" ) 

function scene:create( event )

    local sceneGroup = self.view
local BackGround = display.newImageRect("BackGround.png", 1080, 1920)

end

function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
      
    elseif ( phase == "did" ) then
       local floor = display.newImageRect("floor.png", 351, 32)
    floor.x = display.contentWidth * 0.5
    floor.y = 510
     physics.addBody( floor, "static" )
-----------------------------------------------
local circle = {}
local spawnCircles = function ()
    local FallDown = math.random(display.contentWidth * 0.2, display.contentWidth * 0.8 )
    circle = display.newImageRect("circle.png", 31, 31 )
    circle.x = FallDown
    circle.y = -70
    physics.addBody( circle, "dynamic", {density = .01, friction = 0.5, bounce = 0.1 })
    circle.gravityScale = 0.1 
    physics.setGravity( 0, 10 )   

    end  

myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time
    end
return true 
end
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene

well this code works for me and the only thing wrong with it is that it duplicates the balls... do you knwo why and how to fix it?



[TOPIC: post.html]
#15

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,641 posts
  • Corona SDK

It duplicates them because Show, phase "will' is called every time you open the scene.  If you want to create once, create in the create() method of the scene manager.

 

 

Oh, and you're still using an infinite timer?  I thought I mentioned that is a bad practice.

 

Call the spawner infinitely == spawn infinitely.

timer.performWithDelay( 2000, spawnCircles, -1) 

Edited by roaminggamer, 04 June 2015 - 12:25 PM.


[TOPIC: post.html]
#16

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

can you explain better how to stop it from duplicating? and then we will fix the timer?



[TOPIC: post.html]
#17

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

heres a video of whats happening 

 

https://youtu.be/S2Xv6lJahsk

 

@roaminggamer



[TOPIC: post.html]
#18

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,613 posts
  • Enterprise

It seems to me you have multiple versions of this thread going on.  I just responded to a similar thread.  In the future, only as your question once and keep a single thread to get your answers.  When you have a different problem, start a new thread. 

 

Rob



[TOPIC: post.html]
#19

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

  Best Answer

ya sorry i was just getting really confused... im marking this one solved...




[topic_controls]
[/topic_controls]