It looks like you're new here. If you want to get involved, click one of these buttons!
require "box2d" local physicsScale = 30 b2.setScale(physicsScale) local world = b2.World.new(0, 10, true) -- for creating objects using shape -- as example - bounding walls function wall(x, y, width, height, name) local wall = Shape.new() --define wall shape wall:beginPath() --we make use (0;0) as center of shape, --thus we have half of width and half of height in each direction wall:moveTo(-width/2,-height/2) wall:lineTo(width/2, -height/2) wall:lineTo(width/2, height/2) wall:lineTo(-width/2, height/2) wall:closePath() wall:endPath() wall:setPosition(x,y) --create box2d physical object local body = world:createBody{type = b2.STATIC_BODY} body:setPosition(wall:getX(), wall:getY()) body.name= name --body:setAngle(wall:getRotation() * math.pi/180) local poly = b2.PolygonShape.new() poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2) local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.8} wall.body = body wall.body.type = "wall" --add to scene stage:addChild(wall) --return created object return wall end local screenH = application:getContentHeight() local screenW= application:getContentWidth() --create bounding walls outside the scene wall(-dx,screenH/2,10,screenH) --left wall(screenW/2,-dy,screenW+2*dx,10) --top wall(screenW+dx,screenH/2,10,screenH)--right wall(screenW/2,screenH,screenW+2*dx,10) --bottom --create empty box2d body for joint --since mouse cursor is not a body --we need dummy body to create joint local ground = world:createBody({}) --joint with dummy body local mouseJoint = nil local distanceJoint = nil -- create a mouse joint on mouse down function onMouseDown(event) if ball:hitTestPoint(event.x, event.y) then --ball.body:setPosition(event.x, event.y) local jointDef = b2.createMouseJointDef(ground, ball.body, event.x, event.y, 100000) mouseJoint = world:createJoint(jointDef) end end -- update the target of mouse joint on mouse move function onMouseMove(event) if mouseJoint ~= nil then mouseJoint:setTarget(event.x, event.y) end end -- destroy the mouse joint on mouse up function onMouseUp(event) if mouseJoint ~= nil then world:destroyJoint(mouseJoint) mouseJoint = nil end end -- for creating objects using image -- as example - ball function createBall(x, y) --create ball bitmap object from ball graphic local ball = Bitmap.new(Texture.new("ball.png")) --reference center of the ball for positioning ball:setAnchorPoint(0.5,0.5) ball:setPosition(x,y) --get radius local radius = ball:getWidth()/2 --create box2d physical object local body = world:createBody{type = b2.DYNAMIC_BODY} body.name ='ball' print(ball:getX()..' '.. ball:getY()) body:setPosition(ball:getX(), ball:getY()) body:setAngle(ball:getRotation() * math.pi/180) local circle = b2.CircleShape.new(0, 0, radius) local fixture = body:createFixture{shape = circle, density = 1.0, friction = 0.1, restitution = 0.8} ball.body = body ball.body.type = "ball" --add to scene stage:addChild(ball) --return created object return ball end ball =createBall(application:getContentWidth()/2, 100) ball1 =createBall(200, 200) local dj = b2.createDistanceJointDef(ball.body, ball1.body, 0, 0, 0, 0, 100) distanceJoint = world:createJoint(dj) --distanceJoint.setDampingRatio(0) not work function onEnterFrame() -- edit the step values if required. These are good defaults! world:step(1/60, 8, 3) --iterate through all child sprites for i = 1, stage:getNumChildren() do --get specific sprite local sprite = stage:getChildAt(i) -- check if sprite HAS a body (ie, physical object reference we added) if sprite.body then --update position to match box2d world object's position --get physical body reference local body = sprite.body --get body coordinates local bodyX, bodyY = body:getPosition() --apply coordinates to sprite sprite:setPosition(bodyX, bodyY) --apply rotation to sprite sprite:setRotation(body:getAngle() * 180 / math.pi) end end end local debugDraw = b2.DebugDraw.new() world:setDebugDraw(debugDraw) stage:addChild(debugDraw) stage:addEventListener(Event.ENTER_FRAME, onEnterFrame) -- register for mouse events stage:addEventListener(Event.MOUSE_DOWN, onMouseDown) stage:addEventListener(Event.MOUSE_MOVE, onMouseMove) stage:addEventListener(Event.MOUSE_UP, onMouseUp) |
Comments
this line:
anchorAx: (number) the x coordinate of the world anchor point of bodyA
anchorAy: (number) the y coordinate of the world anchor point of bodyA
anchorBx: (number) the x coordinate of the world anchor point of bodyB
anchorBy: (number) the y coordinate of the world anchor point of bodyB
All of them are the coordinates of the world
Other definition is a definition of DISTANCE_JOINT object which is returned by
You provided a really interesting link, I'll create similar examples with Gideros when I'll have time
Edited:
Ok, other definition if you do not want to create joint definition using b2.createDistanceJointDef but create table with described properties and pass it to world:createJoint your self. Don't know, never tried and used it. I always prefer to use proper createSomeJoint method
Thank you @ar2rsawseen, Im thinking work similar to setAnchorpoint ..
Well now learning box2d, and probe doing that examples.
If you want, I can post here ,and you can control it.
I'm really sure, I'll need some times your knowledge )
http://appcodingeasy.com/Gideros-Mobile/Using-box2d-joints-in-Gideros