How to make an Object travel at an Angle.

I’ve been working on finding a way to make an object travel at an angle. Right now I have a little set up I think I can use to find the angle of my object to travel.
[lua]local function screenTouch (event)

local O
local A
local angle

if event.x <= 150 then
if event.x < rocket.x and event.y < rocket.y then
A = rocket.x - event.x
O = rocket.y - event.y

angle = math.deg(math.atan(O/A))
print("Angle: " … angle)
end
end
end
Runtime:addEventListener(“touch”, screenTouch)[/lua]

With that how would I go about making my rocket travel the angle touched. I figure I would have to write a few if statements to test where the click is so the rocket can go 360 deg and not just 0-90. Other than that I’ve searched and found nothing on applied forces or anything. Any help on how to accomplish this would be appreciated. [import]uid: 20272 topic_id: 35556 reply_id: 335556[/import]

You just need to set the x-velocity and y-velocity according to the desired angle.

try to use something like:

[lua]velocity = 500 – set your rocket velocity here
local xVelocity = math.cos(angle) * velocity
local yVelocity = math.sin(angle) * velocity
rocket:setLinearVelocity( xVelocity, yVelocity )[/lua]

I did not test it but it should be something like that. The velocity variable is whatever velocity do you want to your rocket and the angle variable is that one that you calculated in your touch event.

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

I have this and nothing happens. No errors but no results…

[lua]O = 0
A = 0
angle = 0
velocity = 500

function screenTouch (event)

if event.x <= 150 then
A = rocket.x - event.x
O = rocket.y - event.y

if event.x < rocket.x and event.y < rocket.y then
angle = O/A
angle = math.deg(math.atan(O/A))
print("TL_Angle: " … angle)

xVel = math.cos(angle) * velocity
yVel = math.sin(angle) * velocity
rocket:setLinearVelocity(xVel, yVel)
end
end
end [/lua] [import]uid: 20272 topic_id: 35556 reply_id: 141369[/import]

You need to create your rocket before and add physics to it.

Also, the angle for math.sin and math.cos must be in radians (i.e, remove the conversion that you make to find the angle in degrees).

I don’t know how is your overall code, but try this code below and see if it is what you want to do. (don’t forget adjust the rocket image filename)

[lua]local physics = require “physics” – include Corona’s “physics” library

physics.start()

local velocity = 500

local rocket = display.newImageRect( “soccer_ball.png”, 50, 50 )

– sets the rocket position in the screen
rocket.x = 150
rocket.y = 350

– adds physics to the rocket
physics.addBody( rocket, “kinematic”, { density=0.1, friction=1.0, bounce=0.4, radius=25 } )

function screenTouch (event)

if event.phase == “began” then
local A = math.abs(rocket.x - event.x)
local O = math.abs(rocket.y - event.y)

local xDirection, yDirection = 1, 1

if rocket.x > event.x then
xDirection = -1
end

if rocket.y > event.y then
yDirection = -1
end

local angle = math.atan(O/A)
print("TL_Angle: " … math.deg(angle))

xVel = math.cos(angle) * velocity * xDirection
yVel = math.sin(angle) * velocity * yDirection
rocket:setLinearVelocity(xVel, yVel)

end

return true
end

Runtime:addEventListener(“touch”, screenTouch)[/lua]

You can improve the code by making the angle tangent signal give the xDirection and yDirection.
Renato
Red Beach Games
Try our new game Puzzle You for free here! [import]uid: 181011 topic_id: 35556 reply_id: 141370[/import]

Hi @mkjt88,
If I get this right, you want the rocket to follow the touch or move in that direction? If so, and since you’re using physics, you don’t need to worry about any angle math. Why? Because the force and velocity APIs work on just X and Y coordinates, not angle. You provide the force or velocity in both directions, not as in “move rocket at 76 degrees with speed of 2.89”.

So basically, you just work out the difference (as in subtraction) of the rocket position versus the touch position, and then apply an inverse velocity based on that. You should also probably cap the values at a certain point, in case the user touches far from the rocket (likely you don’t want the rocket getting shot fast toward that point). You can do this by dividing the max force you’ll accept in any direction, say 10, by the highest calculated inverse force in one direction, say 100. Then use that result to factor down the force in the other direction.

max X / inverse of touch X = factor
10 / 100 = 0.1

inverse of touch Y * factor = new Y force
45 * 0.1 = 4.5

This gives you the new X and Y values to apply for a capped, but ratio-equal, force.

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

I’m still kinda lost… I know how to subtract the touch from the rocket coordinates but after that I still can’t get it set up right. Any help on what my next step is and how to get those calculations written i code? [import]uid: 20272 topic_id: 35556 reply_id: 141537[/import]

Hi @mkjt88,
I discussed a similar solution to this issue a few months back. You can find it in this thread:
https://developer.coronalabs.com/forum/2012/09/24/how-set-maximum-force-applied-object

Hope this helps,
Brent [import]uid: 200026 topic_id: 35556 reply_id: 141547[/import]

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]

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]

You just need to set the x-velocity and y-velocity according to the desired angle.

try to use something like:

[lua]velocity = 500 – set your rocket velocity here
local xVelocity = math.cos(angle) * velocity
local yVelocity = math.sin(angle) * velocity
rocket:setLinearVelocity( xVelocity, yVelocity )[/lua]

I did not test it but it should be something like that. The velocity variable is whatever velocity do you want to your rocket and the angle variable is that one that you calculated in your touch event.

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

I have this and nothing happens. No errors but no results…

[lua]O = 0
A = 0
angle = 0
velocity = 500

function screenTouch (event)

if event.x <= 150 then
A = rocket.x - event.x
O = rocket.y - event.y

if event.x < rocket.x and event.y < rocket.y then
angle = O/A
angle = math.deg(math.atan(O/A))
print("TL_Angle: " … angle)

xVel = math.cos(angle) * velocity
yVel = math.sin(angle) * velocity
rocket:setLinearVelocity(xVel, yVel)
end
end
end [/lua] [import]uid: 20272 topic_id: 35556 reply_id: 141369[/import]

You need to create your rocket before and add physics to it.

Also, the angle for math.sin and math.cos must be in radians (i.e, remove the conversion that you make to find the angle in degrees).

I don’t know how is your overall code, but try this code below and see if it is what you want to do. (don’t forget adjust the rocket image filename)

[lua]local physics = require “physics” – include Corona’s “physics” library

physics.start()

local velocity = 500

local rocket = display.newImageRect( “soccer_ball.png”, 50, 50 )

– sets the rocket position in the screen
rocket.x = 150
rocket.y = 350

– adds physics to the rocket
physics.addBody( rocket, “kinematic”, { density=0.1, friction=1.0, bounce=0.4, radius=25 } )

function screenTouch (event)

if event.phase == “began” then
local A = math.abs(rocket.x - event.x)
local O = math.abs(rocket.y - event.y)

local xDirection, yDirection = 1, 1

if rocket.x > event.x then
xDirection = -1
end

if rocket.y > event.y then
yDirection = -1
end

local angle = math.atan(O/A)
print("TL_Angle: " … math.deg(angle))

xVel = math.cos(angle) * velocity * xDirection
yVel = math.sin(angle) * velocity * yDirection
rocket:setLinearVelocity(xVel, yVel)

end

return true
end

Runtime:addEventListener(“touch”, screenTouch)[/lua]

You can improve the code by making the angle tangent signal give the xDirection and yDirection.
Renato
Red Beach Games
Try our new game Puzzle You for free here! [import]uid: 181011 topic_id: 35556 reply_id: 141370[/import]

Hi @mkjt88,
If I get this right, you want the rocket to follow the touch or move in that direction? If so, and since you’re using physics, you don’t need to worry about any angle math. Why? Because the force and velocity APIs work on just X and Y coordinates, not angle. You provide the force or velocity in both directions, not as in “move rocket at 76 degrees with speed of 2.89”.

So basically, you just work out the difference (as in subtraction) of the rocket position versus the touch position, and then apply an inverse velocity based on that. You should also probably cap the values at a certain point, in case the user touches far from the rocket (likely you don’t want the rocket getting shot fast toward that point). You can do this by dividing the max force you’ll accept in any direction, say 10, by the highest calculated inverse force in one direction, say 100. Then use that result to factor down the force in the other direction.

max X / inverse of touch X = factor
10 / 100 = 0.1

inverse of touch Y * factor = new Y force
45 * 0.1 = 4.5

This gives you the new X and Y values to apply for a capped, but ratio-equal, force.

Brent
[import]uid: 200026 topic_id: 35556 reply_id: 141385[/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]

I’m still kinda lost… I know how to subtract the touch from the rocket coordinates but after that I still can’t get it set up right. Any help on what my next step is and how to get those calculations written i code? [import]uid: 20272 topic_id: 35556 reply_id: 141537[/import]

Hi @mkjt88,
I discussed a similar solution to this issue a few months back. You can find it in this thread:
https://developer.coronalabs.com/forum/2012/09/24/how-set-maximum-force-applied-object

Hope this helps,
Brent [import]uid: 200026 topic_id: 35556 reply_id: 141547[/import]