[SOLVED] Drag object and PREVENT rotation?

I have some code here it’s plug and play.
It’s a red box on a screen, with green ground (its a magical universe, use your imagination :slight_smile: )
When I pick up the box via touch by the corner of the box, it will always be affected by gravity.

I know on tilt controls you can limit the X or Y so it’s just one or the other.

I’d like to do that with this as well. However, I want to have the ability to drag the box in ANY Direction, but not have it rotate.

I’ve tried things like fixed rotation, etc but I think that applies to other types of objects, seems when things get dragged that changes?

Anyway, this is code that is slightly modified from the “many crates” example ( I think it’s called debug draw now). The drag code provides perfect x and y touch dragging meaning there is no snapping.

Of course, use it however you like I just want to know how to prevent the rotation on the object!

thanks!

-ng
[lua] --DECLARE PHYSICS
local physics = require “physics”

physics.start(true)
physics.setGravity( 0, 10)
physics.setDrawMode( “normal”)

–DECLARE WIDTH AND HEIGHT SHORTCUT VARIABLES
local _W = display.contentWidth
local _H = display.contentHeight

local ground = display.newRect (0, 400, _W,20)
physics.addBody(ground, “static”)
ground:setFillColor (0,255,0)

local box = display.newRect (_W/2-50, _H/2, 100,100)
physics.addBody(box, “static”)
box:setFillColor (255,0,0)

–======================================================================================
--------DRAG CODE (X AND Y TOUCH ACCURATE----
–======================================================================================
local function dragBody( event )
–print (“drag started”)
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()

if “began” == phase then

stage:setFocus( body, event.id )
body.isFocus = true
–Change body to dynamic
event.target.bodyType = “dynamic”

– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint( “touch”, body, event.x, event.y )

–THIS PREVENTS ROTATION WHEN DRAGGING
–COMMENT OUT IF YOU DON’T WANT THIS
body.isFixedRotation = true
elseif body.isFocus then
if “moved” == phase then

– Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )

elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
body.isFocus = false
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

–After the event ends, change body back to dynamic OR static.
event.target.bodyType = “dynamic”
–event.target.bodyType = “static”

– Remove the joint when the touch ends
body.tempJoint:removeSelf()
–print (“drag ended”)
end
end

– Stop further propagation of touch event
return true
end

box:addEventListener( “touch”, dragBody )[/lua]
[import]uid: 61600 topic_id: 24601 reply_id: 324601[/import]

body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )  
body.isFixedRotation = true  

adding body.isFixedRotation = true seems to work for me, so i may not completely understand [import]uid: 21331 topic_id: 24601 reply_id: 99617[/import]

TheRealTonyK from the real planet earth speaking real truth.

Yep, that fixed it. I had the body.isFixedRotation = true in a different location. Not sure why I didn’t try at that place in the code.

That will teach me.

I updated the code to include it so it may help others!

ng [import]uid: 61600 topic_id: 24601 reply_id: 99659[/import]