How to make physics object draggable?

Hi,
 
I “pinned” some physics object(name cross) on the screen.
How to make them draggable?
I try change the bodyType to kinematic but it doesn’t work.
If I change the bodyType to dynamic, it works, but it’s not what I want, the objects is not “pinned”.
 
 
Looks like I make some stupid mistake that I didn’t realize.
 
Somebody help. Thank you.
 
Soo

(sorry, I try to the code below between[lua] but it looks mess.)


main.lua


_W = display.viewableContentWidth;
_H = display.viewableContentHeight;

local physics = require(“physics”);
physics.setDrawMode(“hybrid”);
physics.start();

– basic setting
local background = display.newImageRect(“images/background.png”, 320, 480);
background.x = _W * 0.5;
background.y = _H * 0.5;

local ground = display.newImageRect(“images/ground.png”, 320, 10);
ground.x = _W * 0.5;
ground.y = _H - 5;

local celling = display.newImageRect(“images/ground.png”, 320, 10);
celling.x = _W * 0.5;
celling.y = 5;

local leftWall = display.newImageRect(“images/leftWall.png”, 10, 480);
leftWall.x = 0;
leftWall.y = _H * 0.5;

local rightWall = display.newImageRect(“images/rightWall.png”, 10, 480);
rightWall.x = _W;
rightWall.y = _H * 0.5;

physics.addBody(ground, “static”, {friction = 0.2, bounce = 0.3});
physics.addBody(celling, “static”, {friction = 0.2, bounce = 0.3});
physics.addBody(leftWall, “static”, {friction = 0.2, bounce = 0.3});
physics.addBody(rightWall, “static”, {friction = 0.2, bounce = 0.3});
– end of basic setting

local ball = display.newImageRect(“images/ball.png”, 100, 100);
ball.x = _W * 0.5-10;
ball.y = 40;
ball.xScale = 0.25;
ball.yScale = 0.25;

physics.addBody(ball, {friction = 0.3, bounce = 0.5, radius = 12.5});
– ball editing

–smallCross function
local NewSmallCross = {};
NewSmallCross.new = function(params)

smallCross = display.newImageRect(“images/cross.png”, 10, 10);
smallCross.x = cross.x;
smallCross.y = cross.y;

physics.addBody(smallCross,“kinematic”, {friction = 0.2, bounce = 0.1});

return smallCross;
end

–cross function
local NewCross = {};
NewCross.new = function(params)

cross = display.newImageRect(“images/cross.png”, 50, 50);
cross.x = _W * 0.5;
cross.y = _H * 0.5;

function cross:touch(e)
if(e.phase == “began”) then
display.getCurrentStage():setFocus(self);
self.hasFocus = true;

oldX = self.x;
oldY = self.y;

print(“beginning…”);

elseif (self.hasFocus) then
if(e.phase == “moved”) then

self.x = (e.x - e.xStart) + oldX;
self.y = (e.y - e.yStart) + oldY;

print(“moving…”);
elseif(e.phase == “ended” or e.phase == “cancelled”) then

display.getCurrentStage():setFocus(nil);
self.hasFocus = false;
print(“ending…”);
end
end

return true;
end

cross:addEventListener(“touch”, cross);

local pBody = {};

pBody.cross = {
{
friction = 0.2,
bounce = 0.2,
shape = {-5,-25, 5,-25, 5,-5, -5,-5}
},
{
friction = 0.2,
bounce = 0.2,
shape = {5,-5, 25,-5, 25,5, 5,5}
},
{
friction = 0.2,
bounce = 0.2,
shape = {5,5, 5,25, -5,25, -5,5}
},
{
friction = 0.2,
bounce = 0.2,
shape = {-5,5, -25,5, -25,-5, -5,-5}
}
};

physics.addBody(cross, “dynamic”, unpack(pBody.cross));

return cross;
end

crossTable = {};
smallCrossTable = {};
pJoints = {};

for i = 1, 3 do

crossTable[i] = NewCross.new();
print(#crossTable);

crossTable[i].x = (i * 80);
crossTable[i].y = _H * 0.5;

smallCrossTable[i] = NewSmallCross.new();
print(#smallCrossTable);
smallCrossTable[i].x = crossTable[i].x;
smallCrossTable[i].y = crossTable[i].y;

pJoints[i] = physics.newJoint(“pivot”, smallCrossTable[i], crossTable[i], smallCrossTable[i].x, smallCrossTable[i].y);
pJoints[i].isMotorEnabled = false;
pJoints[i].motorSpeed = 100;
pJoints[i].maxMotorTorque = 100;
print("pJoints = "…#pJoints);

end

function ball:tap(e)

ball.x = _W * 0.5 + 10;
ball.y = 50;
end
ball:addEventListener(“tap”, ball);

Hi,

check this tutorial out:

http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

I use that code in one of my games and it works.

Best regards,

Tomas

Hi,

Thanks for your reply,

In my main.lua code, I already add the touch:addeventlistener to the object. But it still won’t work.

In the corona document said that if I want to drag a physic object, I have to set it’s bodytype to kinematic or dynamic.

I tried both of the setting, still don’t work.

Thank you

You don’t need a physic body to run the method described in the tutorial.

In the tutorial the writer adds an touch event listener to his object:

myObject:addEventListener( "touch", myObject )

And when a touch event is being invoked this code is runned:

function myObject:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end

If you still don’t understand please show us some important parts of your code for us to exam.

Best regards,

Tomas

Hi,

Sorry, maybe my poor english confuse you.

I have included the main.lua file inside my question.

The code( touch addEventListener) you mentioned about is already in my code.

The question is:

when I set the object’s bodyType to “dynamic” , the code works, means that the object can be drag and move, liked you said.

But when I set the bodyType to “kinematic”, simulator shows that the codes is running, but the object just won’t move.

So it must have something to do with physics, is it?

If you want the user to drag physics objects user a ‘touch’ joint. Trying to move a physics object like a regular display object will cause problems with the physics engine.

http://developer.coronalabs.com/content/game-edition-physics-joints#Touch_joint

.

Hi,

check this tutorial out:

http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

I use that code in one of my games and it works.

Best regards,

Tomas

Hi,

Thanks for your reply,

In my main.lua code, I already add the touch:addeventlistener to the object. But it still won’t work.

In the corona document said that if I want to drag a physic object, I have to set it’s bodytype to kinematic or dynamic.

I tried both of the setting, still don’t work.

Thank you

You don’t need a physic body to run the method described in the tutorial.

In the tutorial the writer adds an touch event listener to his object:

myObject:addEventListener( "touch", myObject )

And when a touch event is being invoked this code is runned:

function myObject:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end

If you still don’t understand please show us some important parts of your code for us to exam.

Best regards,

Tomas

Hi,

Sorry, maybe my poor english confuse you.

I have included the main.lua file inside my question.

The code( touch addEventListener) you mentioned about is already in my code.

The question is:

when I set the object’s bodyType to “dynamic” , the code works, means that the object can be drag and move, liked you said.

But when I set the bodyType to “kinematic”, simulator shows that the codes is running, but the object just won’t move.

So it must have something to do with physics, is it?

If you want the user to drag physics objects user a ‘touch’ joint. Trying to move a physics object like a regular display object will cause problems with the physics engine.

http://developer.coronalabs.com/content/game-edition-physics-joints#Touch_joint

.