Touch Scaling problem

Heya fellow Corona developers !

I have a serious problem !

I would like to grab a part of an image like a clamp and stretch it in the upper y direction wherever the image is positioned in the screen.

here is the code and my problem:

display.setStatusBar( display.HiddenStatusBar )  
   
local \_W = display.contentWidth  
local \_H = display.contentHeight  
   
--set the graphic  
local head = display.newImage("head.png")  
head.x = \_W \*.5  
head.y = \_H \*.5  
   
--set the reference point to the bottom left to be able to stretch the upper part of the head  
head:setReferencePoint(display.BottomLeftReferencePoint)  
   
local function onTouch(event)  
-------------  
local t = event.target  
local phase = event.phase  
local stage = display.getCurrentStage()   
--------------   
if "began" == phase then  
 -- Make target the top-most object  
 local parent = t.parent  
 stage:setFocus(t)  
  
 t.isFocus = true  
   
elseif t.isFocus then  
----------------  
if "moved" == phase then  
   
 -- I want the scale to follow my touch coordinate, but this line of code is not accurate since it doesn't track the touch efficiently  
  
t.yScale = 1.5 \* (1-(event.y/display.contentHeight))  
   
   
 elseif "ended" == phase or "cancelled" == phase then  
   
 display.getCurrentStage():setFocus( t, nil )  
 t.isFocus = false  
   
 end  
end  
   
return true  
   
end  
  
head:addEventListener("touch", onTouch)  
  

I want the scale to follow my touch coordinate, but this line of code is not accurate since it doesn’t track the touch efficiently, so is anyone have a solution for this ?

I would like to move eventually the “head” object with the accelerometer and be able to scale it with my touch wherever the image is on the screen. I just need to know how to be able to scale a point in the image and that this point is scaling or “stretching” with my touch coordinates…

Thanks to anyone who can help me !

Ray [import]uid: 20617 topic_id: 11368 reply_id: 311368[/import]

Hey Ray,

Read this before posting again; http://developer.anscamobile.com/forum/2011/05/05/forum-rules-and-guidelines

You can’t create multiple threads for one thing; it clutters the forum. You also need to post your code in Lua tags because no one can easily read it and help you otherwise.

Kindly familiarize yourself with the rules.

Thanks,
Peach [import]uid: 52491 topic_id: 11368 reply_id: 41259[/import]

Hi Peach !

I’ve edited the post as you told me ! I just forgot how to put code into Lua script ! Thanks for the info !
Would you have an idea for my problem ? I think I can use the local to content function in order to fix my problem and make the origin of my transformation inside my image instead of screen coordinate… [import]uid: 20617 topic_id: 11368 reply_id: 41281[/import]

Damn, I’ve post twice the same post… I will be more vigilant next time :wink: [import]uid: 20617 topic_id: 11368 reply_id: 41282[/import]

When you say you want the scale to follow your touch coordinate, do you mean the position of the head should change to remain realistic or that it isn’t sizing correctly?

Peach :slight_smile: [import]uid: 52491 topic_id: 11368 reply_id: 41343[/import]

Imagine that your touch is acting like a clamp, who can stretch the head in the direction you are “pulling” it. I can do this by putting first the reference point to the bottom left of the image and then make the touch scale it in the y direction. My problem is that I can’t figure out how to “clamp” the touch coordinate to the image. Does it seem a little more clear for you? [import]uid: 20617 topic_id: 11368 reply_id: 41351[/import]

So, is anyone have come up with an idea on how to do this? [import]uid: 20617 topic_id: 11368 reply_id: 41713[/import]

When I imagine it like a clamp I’m still not totally sure of your issue; you said you can’t “clamp” the touch coordinate to the image; how are you trying to do it at present? You just want the image to stay in the correct place, yes? [import]uid: 52491 topic_id: 11368 reply_id: 41870[/import]

Hi Peach,

my initial goal with this is that I want to make a ragdoll character that I will be able to move with the accelerometer around the screen. I would like to design the character like if his skin is made of elastic material, so the user can at any time stretch it in the direction it want. So in a sense, yes, the image will have to move eventually because of the use of the accelerometer, but the example I provided is just a design prototype to see if the stretchable body can act like it should in a fixed position. I’ve tried different approach with this and I got a possible solution I can use for my project. Could you try this code and see if there is another way to achieve the same result with better use of variables, fonctions,etc… that would be very appreciated since I’m leaning corona and that I’m very poor in maths !

*** I’m sorry for my english, I try to make a lot of effort since i’m french ^^, hope you’ll understand, but I sincerely want to thank you for taking some of your time trying to help me with my problem :wink: ***

display.setStatusBar( display.HiddenStatusBar )  
   
local \_W = display.contentWidth  
local \_H = display.contentHeight  
   
--set the graphic  
local head = display.newImage("head.png")  
head.x = \_W \*.5  
head.y = \_H \*.5  
  
--set the reference point to the bottom left to be able to stretch the upper part of the head  
head:setReferencePoint(display.BottomLeftReferencePoint)  
   
local function onTouch(event)  
-------------  
local t = event.target  
local phase = event.phase  
local stage = display.getCurrentStage()  
--------------   
if "began" == phase then  
 -- Make target the top-most object  
 local parent = t.parent  
 stage:setFocus(t)  
  
 t.isFocus = true  
  
elseif t.isFocus then  
----------------  
if "moved" == phase then  
--scale the image in the right direction if the touch is near its bounds...  
if event.y \< t.contentBounds.yMin then  
 t.yScale = t.yScale + 0.04  
elseif event.y \> t.contentBounds.yMin then  
t.yScale = t.yScale - 0.04  
end  
  
elseif "ended" == phase or "cancelled" == phase then  
   
 display.getCurrentStage():setFocus( t, nil )  
 t.isFocus = false  
   
 end  
end  
   
return true  
   
end  
  
head:addEventListener("touch", onTouch)  

[import]uid: 20617 topic_id: 11368 reply_id: 41886[/import]

Hey again,

To me this code looks good - I’ve just tested it out and it is very impressive, especially if you are a newish user? (I’m guessing you are due to the section in which this was posted.)

No worries on the English, you speak very well, it’s just sometimes it’s hard to understand people talking about their code (English or not!) because there are so many specific things one must take into account :slight_smile:

Peach [import]uid: 52491 topic_id: 11368 reply_id: 42084[/import]

Hello,

it seems to work just fine… but if you take my example and you rotate the head from 45 degree, you’ll see that the touch is scaling the head only from its screens coordinates, not from the bounding of the head itself…

It seems that objet.contentBounds only refers to screens coordinate and cannot see the difference if the object has rotated or not…

Is there a way to get object.contentBounds detect the bounding of the head itself, if we take in account that the head could be rotated ? [import]uid: 20617 topic_id: 11368 reply_id: 42111[/import]

Ok, It seems I found the problem ! the touch event would have to respond to event.x AND event.y if the head is moving or rotating…

I’ll post again if there is any problem occurring… [import]uid: 20617 topic_id: 11368 reply_id: 42116[/import]