Hi,
I have a game I am working on that “Sucks” things into the center of the screen. I am doing this by using setLinearVelocity(). I have this working as intended. Once this is done, I want to give the things varying speeds. To do this, I was trying to use density and applyLinearImpulse(). According to the API, “If the target point is the body’s center of mass, it will tend to push the body in a straight line; if the target is offset from the body’s center of mass, the body will spin about its center of mass”. So, I chose the object.x and object.y but it doesn’t seem to be working as I had inspected. Some of them continue to the center as hoped, but many of them are slightly off or completely off when using the applyLinearImpulse().
Thoughts?
local function createPixel() local randPixel = math.random ( 5 ) --used to decide which size/color/speed local randColor = math.random ( 5 ) local newPixel local pixelW local pixelH if ( randPixel == 1) then newPixel = display.newImageRect( mainGroup, "imgs/bluePixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=.1 } ) newPixel.isSensor = true elseif (randPixel == 2 ) then newPixel = display.newImageRect( mainGroup, "imgs/redPixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=.3 } ) newPixel.isSensor = true elseif (randPixel == 3 ) then newPixel = display.newImageRect( mainGroup, "imgs/greenPixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=.7 } ) newPixel.isSensor = true elseif (randPixel == 4 ) then newPixel = display.newImageRect( mainGroup, "imgs/purplePixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=1 } ) newPixel.isSensor = true elseif (randPixel == 5 ) then newPixel = display.newImageRect( mainGroup, "imgs/yellowPixel.png", screenW/17.5, screenW/17.5 ) physics.addBody( newPixel, "dynamic", { density=200 } ) newPixel.isSensor = true end table.insert( pixelsTable, newPixel ) newPixel.myName = "pixel" newPixel.isBullet = true local whereFrom = math.random( 4 ) local toX local toY --Set direction to the center of the screen (WORKING) if ( whereFrom == 1 ) then -- From the left newPixel.x = -60 newPixel.y = math.random( screenH ) toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) elseif ( whereFrom == 2 ) then -- From the top newPixel.x = math.random( screenW ) newPixel.y = -60 toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) elseif ( whereFrom == 3 ) then -- From the right newPixel.x = display.contentWidth + 60 newPixel.y = math.random( screenH ) toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) elseif ( whereFrom == 4 ) then -- From the bottom newPixel.x = math.random (screenW) newPixel.y = screenH + 60 toX = centerX-newPixel.x toY = centerY-newPixel.y newPixel:setLinearVelocity( toX, toY ) end --Speed it up (not working, changes direction from center) newPixel:applyLinearImpulse(1, 1, newPixel, newPixel.y) -- speeds them up based on density end