How to make an Object travel at an Angle.

Hey Renato I got yours to work but it only made the movement one time and I couldn’t make it do anything more. It wouldn’t move every time I clicked. And Brent I think I got everything right with yours from that post just to see exactly what it does and try to understand it but right now I have nothing working… no errors. I’ll post what I have below… I’ll go ahead and clarify what it is I’m looking for. I want my rocket object to be moved around by a touch on the screen. If you hold and drag the touch I want it to follow it or if you click the top and then click the bottom I want it to go straight to that touch without disappearing. The object will never spin or change direction it will always point forward. If you have any more questions let me know and I appreciate all the help so far.
[lua]function screenTouch (event)
local xVal = event.xStart - event.x
local yVal = event.yStart - event.y

if event.x <= 150 then
if (math.abs(yVal) > 2) then
local factor = 2 / math.abs(yVal)
xVal = xVal * factor
yVal = yVal * factor

elseif (math.abs(xVal) > 2 ) then
local factor = 2 / math.abs(xVal)
xVal = xVal * factor
yVal = yVal * factor
end
print(“Works”)
end
end
rocket:applyLinearImpulse(xVal, yVal)
Runtime:addEventListener(“touch”, screenTouch)[/lua] [import]uid: 20272 topic_id: 35556 reply_id: 141578[/import]

Hi mkjt88.

Yes, I didn’t think that you wanted it to follow the touch. You can make your rocket follow your touch by just changing the if statement from the event to:

[lua] if event.phase == “began” or event.phase == “moved” then[/lua]

Now, your rocket you follow your touch, but if you stop moving the rocket will go thru your direction.

I did not understand the touch top/bottom. Do you want the rocket to go from it original position to the top touch position and then go to the bottom touch position (It would be like you set a path for rocket)? If so, you will need to store the top/bottom touch location and then say to your rocket go to the top position using the “transition.to” function and use the onComplete param in that transition function to send the rocket to your down touch position (also using the transition.to function).

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35556 reply_id: 141581[/import]

Hi @mkjt88,

  1. Is there a reason you’re only detecting touches that are x <= 150 ?
  2. You shouldn’t use linear *impulses* here. Use something more constant like setting the direct linear velocity.

Brent [import]uid: 200026 topic_id: 35556 reply_id: 141601[/import]

Ill link to this image to help explain what I’m wanting. http://imageshack.us/photo/my-images/201/screendwi.jpg/ The red part is representing my 150 part. I want that area to be detected for a touch and move the rocket with the touch. I only want the rocket to move so far and then be stopped… thats why I have that part in there. I want the 150px section to be my move controller for the rocket. If i scroll my finger up and down or back and forth on the red section then I want the rocket to follow it. I would want a way to be able to delay it or slow down so the rocket moves a certain speed and doesn’t keep up 100% of the speed of the touch. If i drag my finger from the top left of the red section to the bottom right then I want the rocket to follow that. Also If i touch the top left of the red section and let my finger go and touch the bottom right… I also want it to follow the same path as if I drug the finger across the screen to that point. If you still have any questions on what I mean let me know. Thanks. [import]uid: 20272 topic_id: 35556 reply_id: 141613[/import]

Did you set the rocket to “.isFixedRotation = true” at some point after you declare its body? [import]uid: 200026 topic_id: 35556 reply_id: 142339[/import]

No, I never figured a way to get it working using any kind of joint still… I’m lost… [import]uid: 20272 topic_id: 35556 reply_id: 142443[/import]

OK, I was in the mood to hack out some code tonight. :slight_smile:

Here’s a basic routine with a touch joint, and an attached object following it. You can turn on physics hybrid view to see what’s going on. And, reference on the touch joint is contained here:
http://developer.coronalabs.com/content/game-edition-physics-joints

local rocket = display.newRect(100,100,60,30) ; rocket:setFillColor(255)  
rocket.x = display.contentWidth/2 ; rocket.y = display.contentHeight/2  
physics.addBody( rocket, "dynamic" )  
rocket.isFixedRotation = true  
  
local startTouchX = display.contentWidth/2  
local startTouchY = display.contentHeight/2  
  
rocket.tJoint = physics.newJoint( "touch", rocket, rocket.x, rocket.y )  
--these properties determine the joint properties (pull force, damping)  
rocket.tJoint.maxForce = 0.1  
rocket.tJoint.dampingRatio = 2  
  
local function moveTouch()  
 --randomly move the touch point around on timer iteration  
 startTouchX = startTouchX+math.random(-160,160)  
 startTouchY = startTouchY+math.random(-160,160)  
  
 --show "tracking dots" (where "touch" occurs)   
 local dot = display.newCircle(startTouchX,startTouchY,10)  
 dot:setFillColor(255,0,0)  
 local function clearDot( thisDot )  
 display.remove(thisDot) ; thisDot = nil  
 end  
 transition.to( dot, { alpha=0, time=500, onComplete=clearDot } )  
  
 rocket.tJoint:setTarget( startTouchX, startTouchY )  
end  
--run the "moveTouch" function forever, each 400 milliseconds  
timer.performWithDelay(400,moveTouch,0)  

Brent [import]uid: 200026 topic_id: 35556 reply_id: 142465[/import]

Bump… From what I have explained and with this code I’ll show what I’m shooting for. This code I have works exactly how I want it besides the speed being adjusted. Now this code probably isn’t the best way to go about this and was why I was trying for something different. But with this code it moves with my touch besides the +50 on the x axis so the rocket won’t be directly under your finger.

[lua]
function onScreenTouch (event)

if event.x <= 150 then
if rocket.x ~= event.x then
rocket.x = event.x + 50
end

if rocket.y ~= event.y then
rocket.y = event.y
end
end
end
Runtime:addEventListener (“touch”, onScreenTouch)
[/lua] [import]uid: 20272 topic_id: 35556 reply_id: 141906[/import]

Hi @mkjt88,
If you need some kind of “delayed follow” of the rocket to the finger touch, you’ll probably want to experiment with a physics touch joint. This effectively links the rocket to your touch point (or an offset point, or any point for that matter) with a theoretical “stretchy string”. The “string” can be adjusted in how much it pulls the joined object toward it. This feature is great for the kind of movement that isn’t a direct “move finger 10 pixels left, object moves 10 pixels left”. With some tinkering on the position of the “touch” and how strongly the joint pulls, this might give you the exact effect you want.

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

Brent [import]uid: 200026 topic_id: 35556 reply_id: 141981[/import]

Appreciate the suggestion Brent but I seem to have an issue I can’t solve alone… From what reading I’ve done the closest thing I think I could use would be to start with Touch Joint which I still don’t know where to begin. From what I am seeing with physics joints is that everything seems to roll around like it’s hanging and flopping when you move it… not exactly what I’m looking for as I never want the rockets rotation to change as it’s being moved around. If I move my finger lets say - 75x and +50 Y I want the rocket to travel to that point in a straight line with a certain speed. Also while I’m holding my finger down and moving it the rocket should follow the path of my finger. But if I release my finger and touch an opposite corner of the screen then the rocket should travel straight to those coordinates. I can’t seem to find a way to make a joint do this right. Is there anything I’m missing here or will the physics joints not do what I need? [import]uid: 20272 topic_id: 35556 reply_id: 142053[/import]

Hi Brent… With that my rocket just falls and the dot touch just randomly flashes… I can’t seem to have the rocket follow that touch… [import]uid: 20272 topic_id: 35556 reply_id: 142970[/import]

@mkjt88: I think that was meant to be translated into a touch function:
[lua]
local rocket = display.newRect(100,100,60,30) ; rocket:setFillColor(255)
rocket.x = display.contentWidth/2 ; rocket.y = display.contentHeight/2
physics.addBody( rocket, “dynamic” )
rocket.isFixedRotation = true

rocket.tJoint = physics.newJoint( “touch”, rocket, rocket.x, rocket.y )
–these properties determine the joint properties (pull force, damping)
rocket.tJoint.maxForce = 1000
rocket.tJoint.dampingRatio = 0.5

local function clearDot( thisDot )
display.remove(thisDot) ; thisDot = nil
end

local dot

local function moveTouch(event)
clearDot(dot)

dot = display.newCircle(event.x, event.y, 10)
dot:setFillColor(255,0,0)

rocket.tJoint:setTarget(event.x, event.y)
end

Runtime:addEventListener(“touch”, moveTouch)
[/lua] [import]uid: 147322 topic_id: 35556 reply_id: 142991[/import]

Ah… Sorry guys it was getting late when I tried to get it working… This makes perfect sense now that I see it Caleb. I appreciate all the help guys and sorry for the seemingly obvious questions… This is my first real program besides basic calculators or command line code in C++ and I struggle a lot while I’m trying to learn. Thanks again. [import]uid: 20272 topic_id: 35556 reply_id: 143019[/import]

No problem :slight_smile:

C [import]uid: 147322 topic_id: 35556 reply_id: 143022[/import]

Did you set the rocket to “.isFixedRotation = true” at some point after you declare its body? [import]uid: 200026 topic_id: 35556 reply_id: 142339[/import]

No, I never figured a way to get it working using any kind of joint still… I’m lost… [import]uid: 20272 topic_id: 35556 reply_id: 142443[/import]

OK, I was in the mood to hack out some code tonight. :slight_smile:

Here’s a basic routine with a touch joint, and an attached object following it. You can turn on physics hybrid view to see what’s going on. And, reference on the touch joint is contained here:
http://developer.coronalabs.com/content/game-edition-physics-joints

local rocket = display.newRect(100,100,60,30) ; rocket:setFillColor(255)  
rocket.x = display.contentWidth/2 ; rocket.y = display.contentHeight/2  
physics.addBody( rocket, "dynamic" )  
rocket.isFixedRotation = true  
  
local startTouchX = display.contentWidth/2  
local startTouchY = display.contentHeight/2  
  
rocket.tJoint = physics.newJoint( "touch", rocket, rocket.x, rocket.y )  
--these properties determine the joint properties (pull force, damping)  
rocket.tJoint.maxForce = 0.1  
rocket.tJoint.dampingRatio = 2  
  
local function moveTouch()  
 --randomly move the touch point around on timer iteration  
 startTouchX = startTouchX+math.random(-160,160)  
 startTouchY = startTouchY+math.random(-160,160)  
  
 --show "tracking dots" (where "touch" occurs)   
 local dot = display.newCircle(startTouchX,startTouchY,10)  
 dot:setFillColor(255,0,0)  
 local function clearDot( thisDot )  
 display.remove(thisDot) ; thisDot = nil  
 end  
 transition.to( dot, { alpha=0, time=500, onComplete=clearDot } )  
  
 rocket.tJoint:setTarget( startTouchX, startTouchY )  
end  
--run the "moveTouch" function forever, each 400 milliseconds  
timer.performWithDelay(400,moveTouch,0)  

Brent [import]uid: 200026 topic_id: 35556 reply_id: 142465[/import]

Hi Brent… With that my rocket just falls and the dot touch just randomly flashes… I can’t seem to have the rocket follow that touch… [import]uid: 20272 topic_id: 35556 reply_id: 142970[/import]

@mkjt88: I think that was meant to be translated into a touch function:
[lua]
local rocket = display.newRect(100,100,60,30) ; rocket:setFillColor(255)
rocket.x = display.contentWidth/2 ; rocket.y = display.contentHeight/2
physics.addBody( rocket, “dynamic” )
rocket.isFixedRotation = true

rocket.tJoint = physics.newJoint( “touch”, rocket, rocket.x, rocket.y )
–these properties determine the joint properties (pull force, damping)
rocket.tJoint.maxForce = 1000
rocket.tJoint.dampingRatio = 0.5

local function clearDot( thisDot )
display.remove(thisDot) ; thisDot = nil
end

local dot

local function moveTouch(event)
clearDot(dot)

dot = display.newCircle(event.x, event.y, 10)
dot:setFillColor(255,0,0)

rocket.tJoint:setTarget(event.x, event.y)
end

Runtime:addEventListener(“touch”, moveTouch)
[/lua] [import]uid: 147322 topic_id: 35556 reply_id: 142991[/import]

Ah… Sorry guys it was getting late when I tried to get it working… This makes perfect sense now that I see it Caleb. I appreciate all the help guys and sorry for the seemingly obvious questions… This is my first real program besides basic calculators or command line code in C++ and I struggle a lot while I’m trying to learn. Thanks again. [import]uid: 20272 topic_id: 35556 reply_id: 143019[/import]