[Resolved] moving player at touch event position at constant speed!

Hi guys, I am trying to move player at screen touch event direction, and actually i got it some far, here are few lines.

local function movebox(event)
Player:setLinearVelocity( event.x - Player.x, event.y - Player.y )
print(“move box”)
end

Runtime:addEventListener(“tap”, movebox)

With this i am able to move player in touched direction, the only problem is speed.
For eg. if i tap on the screen 100 pixels away from players position players will move at the speed of say 100. and if i touched screen 200 pixels away from player then player will move at the speed of 200. Like it seems like player start moving faster at longer distance.

What i want to do is i want to keep the speed amount constant.

Thanks in advance [import]uid: 83799 topic_id: 26388 reply_id: 326388[/import]

Hey.

Here is some code I am using to move a player in a given direction. I believe it was initially posted by Graham Ransom in response to another post, so I hope he doesn’t mind my reposting it here. (thanks Graham!)

The problem with your code is that when you are setting the velocity, you’re only feeding in a direction, and not the speed to move by. The code below works, but you should note that my character rotates around, and the angle of rotation is used to calculate the direction of motion.

[code]
vel = 100 – this is the arbitrary speed of player motion.
p1Rads = player1.rotation * math.pi / 180

local velX = math.cos (p1Rads) * vel
local velY = math.sin (p1Rads) * vel

player1:setLinearVelocity(velX,velY)
[/code] [import]uid: 67933 topic_id: 26388 reply_id: 107003[/import]

With this player is moving only at one direction… How to use this code for all direction, can u please explain? i am very new to corona…

Thanks in advance [import]uid: 83799 topic_id: 26388 reply_id: 107250[/import]

Sure. I didn’t understand it at first either :wink:

vel = 100 -- this is the arbitrary speed of player motion, you could control this using another function elsewhere.  
 p1Rads = player1.rotation \* math.pi / 180 -- p1Rads converts the angle of the objects rotation into radians, (which is a value between -1 and 1, and is how corona works out angles)  
   
 local velX = math.cos (p1Rads) \* vel -- this equation generates the value velX, which is the x direction of motion \* the velocity variable above  
 local velY = math.sin (p1Rads) \* vel -- this works as above, but for the Y velocity.  
  
 player1:setLinearVelocity(velX,velY)-- this sets the final x and y velocity of the object.  

I hope that helps explain a little how the above code works?

Thinking about your problem, I think it’d be easier to complete the following tutorials and use the concepts in them. They do exactly what you want, and are simpler than the code I posted.

http://www.youtube.com/watch?v=8AqAUyHdhqI
http://www.youtube.com/watch?v=pR37-szANDo&feature=relmfu

Also, if you’re new to Corona, there is a brilliant site, www.learningcorona.com

Hope that helps?

Dan [import]uid: 67933 topic_id: 26388 reply_id: 107338[/import]

Hey spider_newgent thanks for the explanation…
Now its much more easier to understand the logic

Thank you once again [import]uid: 83799 topic_id: 26388 reply_id: 107364[/import]

I know its a long time… but finally i got the idea how to achieve above result. So i am posting my sample code here, this might be useful to someone else… (I mean for a new developers like me)

I got this code and logic from this link…
http://aabweber.com/corona/distance-between-two-objects/

[code]
_W = display.contentWidth
_H = display.contentHeight

local frog = display.newRect(0,0,50,50)
frog:setReferencePoint(display.CenterReferencePoint)
frog.x = _W / 2 ; frog.y = _H / 2
physics.addBody(frog,{friction = 0, bounce = 0})

local function moveFrog(event)

distance = math.sqrt((event.x-frog.x)^2+(event.y-frog.y)^2)
speed = 0.1

transition.to(frog,{time = (distance / speed),x = event.x , y = event.y})

end

Runtime:addEventListener(“tap”, moveFrog) [import]uid: 83799 topic_id: 26388 reply_id: 110239[/import]

Hey there, thanks so much for updating with your solution, I’m sure it will help others in the future :slight_smile: [import]uid: 52491 topic_id: 26388 reply_id: 110257[/import]