Move, rotate and resize using multi-touch

Hi,

I’m trying to implement this listener function for the specified feature in subject, everything is working find except for the rotate, the object actually rotates with a strange behavior (like it receive a -0, +180, -0).

Attached below the “incriminating” code, someone can help me with this issue or have an already made function that implements the 3 specified features ?

Thanks in advance

 local function calculateDelta( previousTouches, event )  
 local id,touch = next( previousTouches )  
 if event.id == id then  
 id,touch = next( previousTouches, id )  
 assert( id ~= event.id )  
 end  
  
 local dx = touch.x - event.x  
 local dy = touch.y - event.y  
 return dx, dy  
 end  
  
 function imageEvent ( self, event )  
 local t = event.target  
 local pi = math.pi   
 local atan2 = math.atan2   
 local newRotation  
  
 local result = true  
  
 local phase = event.phase  
  
 local previousTouches = self.previousTouches  
  
 local numTotalTouches = 1  
 if ( previousTouches ) then  
 -- add in total from previousTouches, subtract one if event is already in the array  
 numTotalTouches = numTotalTouches + self.numPreviousTouches  
 if previousTouches[event.id] then  
 numTotalTouches = numTotalTouches - 1  
 end  
 end  
  
 if "began" == phase then  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
  
 -- Store initial position of finger  
 t.x1 = event.x  
 t.y1 = event.y  
  
 if ( not self.isFocus ) then  
 -- Subsequent touch events will target button even if they are outside the contentBounds of button  
 display.getCurrentStage():setFocus( self )  
 self.isFocus = true  
  
 previousTouches = {}  
 self.previousTouches = previousTouches  
 self.numPreviousTouches = 0  
 elseif ( not self.distance ) then  
 local dx,dy  
  
 if previousTouches and ( numTotalTouches ) \>= 2 then  
 dx,dy = calculateDelta( previousTouches, event )  
 end  
  
 -- initialize to distance between two touches  
 if ( dx and dy ) then  
 local d = math.sqrt( dx\*dx + dy\*dy )  
 if ( d \> 0 ) then  
 self.distance = d  
 self.xScaleOriginal = self.xScale  
 self.yScaleOriginal = self.yScale  
 print( "distance = " .. self.distance )  
 end  
 end  
 end  
  
 if not previousTouches[event.id] then  
 self.numPreviousTouches = self.numPreviousTouches + 1  
 end  
 previousTouches[event.id] = event  
  
 elseif self.isFocus then  
 if "moved" == phase then   
  
 if ( self.distance ) then  
 local dx,dy  
 if previousTouches and ( numTotalTouches ) \>= 2 then  
 dx,dy = calculateDelta( previousTouches, event )  
 end  
 t.x2 = event.x   
 t.y2 = event.y  
  
 angle1 = 180/pi \* atan2(t.y1 - t.y, t.x1 - t.x)  
 angle2 = 180/pi \* atan2(t.y2 - t.y, t.x2 - t.x)  
 rotationAmt = angle1 - angle2  
  
 --rotate it  
 newRotation = t.rotation-rotationAmt  
 t.rotation = newRotation  
  
 t.x1 = t.x2  
 t.y1 = t.y2  
  
 if ( dx and dy ) then  
 local newDistance = math.sqrt( dx\*dx + dy\*dy )  
 local scale = newDistance / self.distance  
  
 if ( scale \> 0 ) then  
 self.xScale = self.xScaleOriginal \* scale  
 self.yScale = self.yScaleOriginal \* scale  
 end  
 end  
 else  
 -- Move Object  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
 end  
  
 if not previousTouches[event.id] then  
 self.numPreviousTouches = self.numPreviousTouches + 1  
 end  
 previousTouches[event.id] = event  
  
 elseif "ended" == phase or "cancelled" == phase then  
 if previousTouches[event.id] then  
 self.numPreviousTouches = self.numPreviousTouches - 1  
 previousTouches[event.id] = nil  
 end  
  
print(self.x+(self.width/2))  
 if self.x+(self.width/2) \< 300 then self:removeSelf() end  
  
 if ( #previousTouches \> 0 ) then  
 -- must be at least 2 touches remaining to pinch/zoom  
 self.distance = nil  
 else  
 -- previousTouches is empty so no more fingers are touching the screen  
 -- Allow touch events to be sent normally to the objects they "hit"  
 display.getCurrentStage():setFocus( nil )  
  
 self.isFocus = false  
 self.distance = nil  
 self.xScaleOriginal = nil  
 self.yScaleOriginal = nil  
  
 -- reset array  
 self.previousTouches = nil  
 self.numPreviousTouches = nil  
 end  
 end  
 end  
  
 return result  
 end   

[import]uid: 21692 topic_id: 15155 reply_id: 315155[/import]

up
[import]uid: 21692 topic_id: 15155 reply_id: 56235[/import]

This is very long and you are more likely to get a member of the community willing to donate their time and effort if you give them something plug and play to mess with :wink:

Peach [import]uid: 52491 topic_id: 15155 reply_id: 56257[/import]

Hi Peach,

yep i hoped someone got the same problem… I’ll prepare a package with an example =)
[import]uid: 21692 topic_id: 15155 reply_id: 56264[/import]

Someone likely has, but probably using different code - that’s the tricky part :wink:

If you prepare an example people will have an easier time, either way - and if I’m not too busy off cleaning up spam or answering emails I’ll try to take a look, too.

Peach :slight_smile: [import]uid: 52491 topic_id: 15155 reply_id: 56360[/import]

Hey Peach, that’s true =)

Below attached an example file, I hope someone could help me with this issue.
http://www.mediafire.com/?qa85mky89g9mzew [import]uid: 21692 topic_id: 15155 reply_id: 56509[/import]

up =) [import]uid: 21692 topic_id: 15155 reply_id: 57237[/import]