Hey All, A Little Math Help And Some Advice If Possible? :)

Hi,

I’ve been trying to learn Corona for a few days now, I’ve sat thru these ones;

Corona SDK Game 01: Orb Smasher
Corona SDK Game 02: Balloon Burst

Lesson 02: Creating an Analog Clock App

Lesson 03: Create an Accelerometer Driven Application

Lesson 04: Develop an Entertaining Magic Ball Application

And a few others about Storyboard. Now I’ve intensly used GameSalad before and I’m so-so at C,C++ programming but all I did on those languages was file editing or data storing jobs, so not much to do with game design.

I’m trying to port 1 of the games I made in GameSalad to Corona. So my first *problem* I couldn’t tackle is;

I have a ball on X2,Y2 coordinates. And when I touch somewhere on the screen (X1,Y1) I want the ball to move along that line until it reaches out of the visible screen. To do that on GameSalad I built something that “Finds the equation of the line ( (y2-y1/x2-x1) = (y-y2 / x - x2) ) and makes the new point coordinates (X, TargetY) and calculates X according to pre-set TargetY --> i.e. (X, 2000)”

Which means, I write the line equation, set Y to something off-screen (like 2000) and calculate the X. It worked flawlessly but I can not implement that on Corona. And here is an image of what I’m trying to do;

q.png

And my failed code;

local Y2 = 10 local X2 = display.contentWidth/2 local function onScreenTouch(event) if (event.phase == "began") then X1 = event.x Y1 = event.y m1 = (Y2 - Y1)/(X2 - X1) TargetX = (2000 - Y2 + ( m1 \* X2 ) ) / m1 print("\nevent.x = " .. event.x .. "\nevent.y = " .. event.y .. "\nm1 = " .. m1 .. "\nTargetX = " .. TargetX) circle = display.newCircle( X2, Y2, 10 ) circle.enterFrame = Move Runtime:addEventListener ( "enterFrame", circle ) end end function Move(obj) if (obj.x \< TargetX) then obj.x = obj.x + 5 end if (obj.y \< 2000) then obj.y = obj.y + 5 end end Runtime:addEventListener( "touch", onScreenTouch )

And of course, this failed miserably :slight_smile:

And besides that, I’m having a hard time with learning Corona even though I have programming experience prior to that. Any advice on what should I do to speed up my learning curve? And I’m guessing I’ll have 100s of problems like this when I’m making a game, I can’t come running to forums each time… So… What should I do?

Hi there, and welcome to Corona!  One of the great things about it is that the forum community is very active.  That doesn’t guarantee you’ll get an answer to every question you have, but there’s no need to be shy about asking questions (even if it’s 100s…!)

For your question above, I think the basic problem you’re having is that you’re adding the same value (5) to both the x and y coordinate of the circle.  That’s a recipe to make the circle always move at a 45-degree angle.  Instead, you need to use the rise-over-run concept of a line.  Try changing “obj.y = obj.y + 5” to “obj.y = obj.y + 5*m1” and see if that helps.

You might also be interested in trying the physics library.  You can make your circle a physics object, apply a velocity to it in the direction you want, and then let the physics simulation handle the movement (instead of an enterFrame listener).

Hope this helps.

  • Andrew

Hi cbt,

One of the easiest ways (coding) to make things happen over time is the corona transition.to() function.

Basically, you pass it an object, and the fields you want to change over time (and a time period), and it transitions the variables for you… You can do it with x,y coords, alpha, or whatever you want. It also will call back a function of yours when it completes.

Although I’m not coding a “twitch” game, there’s still things it’s useful for, and your particular case sounds like yet another ideal use of the feature. (I wouldn’t be surprised if this feature is doing a lot of the heavy lifting on some games)

Give it a try.

Thanks for the answers guys!

I made it a physics Body and experimented with “setLinearVelocity” but I couldn’t come up with the right X and Y speeds. :frowning:

Oh wow! I remember the time they added “interpolate” to GS, it was a huge relief! :smiley:

I tried it, and it works awesome but I have a little problem, I want my ball (circle) to move at the same speed all the time. I mean, wherever I tap on the screen, it should go with the same speed.

I tried to divide the “time” param on “transition.to” with some different variables and equations to get what I wanted but my math failed me. Any advice on this?

Thanks again! :slight_smile:

To calculate distance, isn’t it sqrt(deltaX^2 + deltaY^2) ?

So, you could calculate the new delta distance, and multiply that by some millisecond speed factor (per unit distance), and use that for the time factor in the transition.

Yeah! I was thinking I tried that, but after your post I checked it again and I was doing a severe case of *wrong parantheses*

So here is the fully working code;

 circle = display.newCircle( X2, Y2, 10 ) physics.addBody ( circle, "dynamic", {density=1, friction=0.5, bounce=0.3} ) m2 = math.sqrt( ((2000 - Y2) \* (2000 - Y2)) + ((TargetX - X2) \* (TargetX - X2)) ) --The distance? transition.to( circle, { time=m2\*5, alpha=1, x=TargetX, y=2000} )

And it seems like it is working. Thanks everyone!

Hi there, and welcome to Corona!  One of the great things about it is that the forum community is very active.  That doesn’t guarantee you’ll get an answer to every question you have, but there’s no need to be shy about asking questions (even if it’s 100s…!)

For your question above, I think the basic problem you’re having is that you’re adding the same value (5) to both the x and y coordinate of the circle.  That’s a recipe to make the circle always move at a 45-degree angle.  Instead, you need to use the rise-over-run concept of a line.  Try changing “obj.y = obj.y + 5” to “obj.y = obj.y + 5*m1” and see if that helps.

You might also be interested in trying the physics library.  You can make your circle a physics object, apply a velocity to it in the direction you want, and then let the physics simulation handle the movement (instead of an enterFrame listener).

Hope this helps.

  • Andrew

Hi cbt,

One of the easiest ways (coding) to make things happen over time is the corona transition.to() function.

Basically, you pass it an object, and the fields you want to change over time (and a time period), and it transitions the variables for you… You can do it with x,y coords, alpha, or whatever you want. It also will call back a function of yours when it completes.

Although I’m not coding a “twitch” game, there’s still things it’s useful for, and your particular case sounds like yet another ideal use of the feature. (I wouldn’t be surprised if this feature is doing a lot of the heavy lifting on some games)

Give it a try.

Thanks for the answers guys!

I made it a physics Body and experimented with “setLinearVelocity” but I couldn’t come up with the right X and Y speeds. :frowning:

Oh wow! I remember the time they added “interpolate” to GS, it was a huge relief! :smiley:

I tried it, and it works awesome but I have a little problem, I want my ball (circle) to move at the same speed all the time. I mean, wherever I tap on the screen, it should go with the same speed.

I tried to divide the “time” param on “transition.to” with some different variables and equations to get what I wanted but my math failed me. Any advice on this?

Thanks again! :slight_smile:

To calculate distance, isn’t it sqrt(deltaX^2 + deltaY^2) ?

So, you could calculate the new delta distance, and multiply that by some millisecond speed factor (per unit distance), and use that for the time factor in the transition.

Yeah! I was thinking I tried that, but after your post I checked it again and I was doing a severe case of *wrong parantheses*

So here is the fully working code;

 circle = display.newCircle( X2, Y2, 10 ) physics.addBody ( circle, "dynamic", {density=1, friction=0.5, bounce=0.3} ) m2 = math.sqrt( ((2000 - Y2) \* (2000 - Y2)) + ((TargetX - X2) \* (TargetX - X2)) ) --The distance? transition.to( circle, { time=m2\*5, alpha=1, x=TargetX, y=2000} )

And it seems like it is working. Thanks everyone!