I'm asking help about another 'math' problem using "setLinearVelocity()"

Hi there,

I’ve been trying to do this with Corona;

q.png

I managed to do it by setting a high Y value like 2000, calculating X according to line equation and using transition.to() to move to that X,Y position on that route. But when I do it like this I’m not getting the collision and bounce results I want. So I’m trying to do this by physics and setLinearVelocity() so that I can get better collision physics.

But I have no idea how to calculate that X and Y value in setLinearVelocity(). I tried some formulas but none worked. Any of you can help me? Thanks in advance.

*I always get stuck on the easiest math problems :frowning:
 

Hi @cbt,
Since the setLinearVelocity() API accepts just x and y (not an angle and velocity on that vector), you can just subtract the target touch x and y from the origin x and y. So if the ball starts at x=100 and the touch x is 200, 200-100 = 100 to apply to the x value of setLinearVelocity(). Same idea for the y value.

Now, you might want to factor the values down by an equal ratio, if the speed is more than you want. In that case, just divide both values by 4, 5, 10, or whatever seems right in the game.

Does that help?
Brent

Hello,

If I do what you say with touch values, the speed of the ‘ball’ is dependent on where the touch happened. So I used the arbitary Y value ‘2000’ and the X value calculated from that Y value according to where touch happened.

So I did something like this; (X2 and Y2 are the ball’s starting coordinates)

ball:setLinearVelocity((TargetX - X2), -(2000 - Y2))

It does work but now the route the balls take are slightly off then where I touch. Thanks for the help tho I’ll try to tinker this to my own needs.

Why are you using “2000”?

Why not just TargetY?

C

I’m trying to send the balls out of the screen to a far location like (xxxx,2000).

Since I know the touch(x,y) and the ball(x,y) I can calculate where would ball be on X (TargetX) axis, on that line, when its Y is 2000.

In that case, you can take 2000 and factor it into both directions. Let’s imagine sample X/Y velocities of -50, 100. If your “distant Y” is 2000, calculate 2000 / 100 (the Y value) to get 20. And then multiply 20 by -50 (the X value) to get -1000. The new values would be -1000, 2000, which are in equal ratio to your original values.

Brent

Double post.

Still, it does not work…

@cbt

  1. Calculate the tween vector

  2. Normalize it

  3. Scale it

  4. Apply it

The following code will always give you a velocity vector with a consistent value of 100 pixels/second (assuming an unscaled display)…

-- WARNING: This code may have typos... -- 1. local X1 = mouseX local Y1 = mouseY local X2 = circle.x local Y2 = circle.y local vx = X1 - X2 local vy = Y1 - Y2 -- 2. local mSqrt = math.sqrt local len = mSqrt(vx \* vx + vy \* vy) local nx = vx/len local ny = vy/len -- 3. local scale = 100 local sx = nx \* scale local sy = ny \* scale -- 4. circle:setLinearVelocity( sx, sy )

That was the most perfect answer I could imagine, thanks a lot!

Hi @cbt,
Since the setLinearVelocity() API accepts just x and y (not an angle and velocity on that vector), you can just subtract the target touch x and y from the origin x and y. So if the ball starts at x=100 and the touch x is 200, 200-100 = 100 to apply to the x value of setLinearVelocity(). Same idea for the y value.

Now, you might want to factor the values down by an equal ratio, if the speed is more than you want. In that case, just divide both values by 4, 5, 10, or whatever seems right in the game.

Does that help?
Brent

Hello,

If I do what you say with touch values, the speed of the ‘ball’ is dependent on where the touch happened. So I used the arbitary Y value ‘2000’ and the X value calculated from that Y value according to where touch happened.

So I did something like this; (X2 and Y2 are the ball’s starting coordinates)

ball:setLinearVelocity((TargetX - X2), -(2000 - Y2))

It does work but now the route the balls take are slightly off then where I touch. Thanks for the help tho I’ll try to tinker this to my own needs.

Why are you using “2000”?

Why not just TargetY?

C

I’m trying to send the balls out of the screen to a far location like (xxxx,2000).

Since I know the touch(x,y) and the ball(x,y) I can calculate where would ball be on X (TargetX) axis, on that line, when its Y is 2000.

In that case, you can take 2000 and factor it into both directions. Let’s imagine sample X/Y velocities of -50, 100. If your “distant Y” is 2000, calculate 2000 / 100 (the Y value) to get 20. And then multiply 20 by -50 (the X value) to get -1000. The new values would be -1000, 2000, which are in equal ratio to your original values.

Brent

Double post.

Still, it does not work…

@cbt

  1. Calculate the tween vector

  2. Normalize it

  3. Scale it

  4. Apply it

The following code will always give you a velocity vector with a consistent value of 100 pixels/second (assuming an unscaled display)…

-- WARNING: This code may have typos... -- 1. local X1 = mouseX local Y1 = mouseY local X2 = circle.x local Y2 = circle.y local vx = X1 - X2 local vy = Y1 - Y2 -- 2. local mSqrt = math.sqrt local len = mSqrt(vx \* vx + vy \* vy) local nx = vx/len local ny = vy/len -- 3. local scale = 100 local sx = nx \* scale local sy = ny \* scale -- 4. circle:setLinearVelocity( sx, sy )

That was the most perfect answer I could imagine, thanks a lot!