Oh, finally! Problem solved!
Well, let me explain. I have to body - dynamic (hero) and static (wall). When I press a joystic I count an angle, and then dX and dY (proepries of triangle). Then I set linearVelocity to hero, and he is moving. But I need him to be in a center. So I count a distance between the center of the screen and hero Position. These new dX and dY I apply to a hero as hero:translate(dX, dY) and also to a wall - wall:translate(dX, dY). And here we go! Hero only touch a wall and cannot to go through it and so on. Here is a sample code:
-- SAMPLE COLLISION FILTERS local heroCollisionFilter = { categoryBits = 1, maskBits = 2 } local objectsCollisionFilter = { categoryBits = 2, maskBits = 1 } -- HELPS TO LOCATE OBJECTS window\_width = display.actualContentHeight window\_height = display.actualContentWidth -- PHYSICS ENGINE ON local physics = require ("physics") physics.start() physics.setGravity (0, 0) -- because it up view game -- File with general functions. In this sample only getAngle() and getVector() functions used require ("function") -- In project this group consist information about all objects in the game local world = display.newGroup() -- Initialize objects for this project local circleMove, moveArea, hero, topBorder function move (obj, dX, dY) obj:translate(dX, dY) end function moveWorld (event) local dX, dY = getVector (hero.direction, hero.speed) -- how far to push hero:setLinearVelocity (dX, dY) -- move hero as a physical object dX = window\_width / 2 - hero.x -- count distance from screen center dY = window\_height / 2 - hero.y move (hero, dX, dY) -- and move hero and border move (topBorder, dX, dY) --print (hero.x, window\_width / 2 - hero.x) end function world:startMove () world.enterFrame = moveWorld Runtime:addEventListener ("enterFrame", world) end function world:stopMove () hero:setLinearVelocity (0, 0) -- stop a movement Runtime:removeEventListener ("enterFrame", world) end -- When touch joystick calculate the angle, and give this information to moveWorld() function function onMoveTouch (event) hero.direction = getAngle (moveArea.x, moveArea.y, event.x, event.y) circleMove.x = event.x circleMove.y = event.y if event.phase == "began" then world:startMove () end if event.phase == "cancelled" then print ("CANCELL") end if event.phase == "ended" then circleMove.x = moveArea.x circleMove.y = moveArea.y world:stopMove () end end -- Visualisation of sample inteface local background = display.newImage("background.png") local moveGroup = display.newGroup () moveArea = display.newRect (moveGroup, 0,0,10,10) moveArea:setFillColor (0,0,0,0) moveArea.height = window\_height / 2 moveArea.width = moveArea.height moveArea.x = moveArea.width / 2 moveArea.y = window\_height - (moveArea.height / 2) local circleMoveBorder = display.newImageRect (moveGroup, "circle.png", moveArea.width, moveArea.height) circleMoveBorder.x = moveArea.x circleMoveBorder.y = moveArea.y circleMove = display.newImageRect (moveGroup, "circle.png", moveArea.width/4, moveArea.height/4) circleMove.x = moveArea.x circleMove.y = moveArea.y moveArea:addEventListener ("touch", onMoveTouch) hero = display.newCircle (0,0, 10) hero:setFillColor (120, 44, 220) hero.x = window\_width / 2 hero.y = window\_height / 2 hero.speed = 80 -- this variable goes to the getVector() function hero.direction = 0 topBorder = display.newRect (-100, 100, window\_width + 200, 10) topBorder:setFillColor (0,0,0) -- The main trouble right here. What to do with collisions, should I use sensor or what? hero.bodyType = "dynamic" hero.physicsOptions = { density = 1, radius = 10, isSensor = false, filter = heroCollisionFilter, } physics.addBody (hero, hero.bodyType, hero.physicsOptions) topBorder.bodyType = "static" topBorder.physicsOptions = { density = 30, friction = 1.0, bounce = 0, isSensor = false, filter = objectsCollisionFilter } physics.addBody (topBorder, topBorder.bodyType, topBorder.physicsOptions) local function onCollision (self, event) -- MEET A HERO if (event.other.physicsOptions.filter.categoryBits == 1) then print (event.phase) -- to see is it over or not. end end topBorder.collision = onCollision topBorder:addEventListener ("collision", topBorder)