Changing linear speed of an object based on the distance to a touch.

(Please note, I am a novice at Corona and eleven years old)
This could be extremely useful in a number of things. My idea is making a plane that will move towards your touch, but at a speed based on the distance between its central point and the touch itself.

For example, if the distance was very small, the speed of motion towards the touch would be pretty small. If the distance was very big, the speed would be pretty reasonable.

The end result should be that the faster you move your finger, the faster the plane moves to follow it.

Anybody know how this might be done? [import]uid: 82408 topic_id: 14595 reply_id: 314595[/import]

Hi there - welcome to the community :slight_smile:

You would start by calculating how far the plane was from your touch by comparing plane.x and plane.y to event.x and event.y

Next, you’d apply linear impulse using a multiplier based on the difference between plane x/y and event x/y.

Peach :slight_smile: [import]uid: 52491 topic_id: 14595 reply_id: 54045[/import]

A little snip for ya

local Abs = math.abs  
local Sqrt = math.sqrt  
  
function getDistance(x1,y1,x2,y2)  
 local xDist = Abs(x1 - x2)  
 local yDist = Abs(y1 - y2)  
 local dist = Sqrt((xDist \* xDist) + (yDist \* yDist))  
  
 return dist  
end  

This is from memory, so maybe someone can chime in with the SuperOptimized version, but… :slight_smile: Good Luck! [import]uid: 21331 topic_id: 14595 reply_id: 54074[/import]

IMO that is easy.
[lua] distancex = eventx - player.x
distancey = eventy - player.y
player:setLinearVelocity(distancex,distancey)[/lua] [import]uid: 79135 topic_id: 14595 reply_id: 54144[/import]

ya, sorry about that, guess i should have taken the time to post a solution.

FYI: My function strips the vector with Abs(). Just dont want you to loose your direction because of my post. :slight_smile: [import]uid: 21331 topic_id: 14595 reply_id: 54172[/import]

There’s just one issue. My terminal says it doesn’t understand what ‘event.x’ and ‘event.y’ are. I think this is because it doesn’t have any code saying that ‘event.x’ and ‘event.y’ are where the touch events occur. Anybody know a way to tell it that ‘event.x’ and ‘event.y’ are the coordinates for the touch (sorry for being kinda repetitive)? Here’s my code:

[code]
local physics = require (“physics”)
physics.start()
physics.setGravity( 0, 0 )

local plane = display.newImage( “Plane.png” )
plane.x = display.contentWidth / 2; plane.y = display.contentHeight / 1.25
plane.width = 90; plane.height = 70
physics.addBody( plane )

distanceX = event.x - plane.x
distanceY = event.y - plane.y
plane:setLinearVelocity( distanceX, distanceY )

physics.setDrawMode( “hybrid” ) [import]uid: 82408 topic_id: 14595 reply_id: 54463[/import]

hi perseusspartacus.

that needs to be inside a function. so:
[lua]local function move()

distanceX = event.x - plane.x
distanceY = event.y - plane.y
plane:setLinearVelocity( distanceX, distanceY )

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

but that won’t work so you will need to (I don’t know why, I know how) pass the event into the function so use:

[lua] local function move(event) [/lua]

to start the function.

I think that is right :S reply :wink:

[import]uid: 79135 topic_id: 14595 reply_id: 54513[/import]

Thank you everyone. I got it to work. There seems to be a lot of helpful people on Corona. I think we can make a lot of cool stuff with this. [import]uid: 82408 topic_id: 14595 reply_id: 54575[/import]