How to calculate a point with an given center, angle and radius?

Hi all,

Need some help with some math.

How can I do the following in Corona ? 

http://stackoverflow.com/questions/2912779/how-to-calculate-a-point-with-an-given-center-angle-and-radius

Think the y-coordinate system is reversed and the angles are different.

What i want to do is based on a rotation of a rectangle, I want a circle to travel at that same angle 

( Thinking of a cannon shooting a a ball )

Thanks in advance,

Are you trying to make a rotating turret that fires ‘projectiles’?

Hi thanks for the reply,

Something like that, actually a rotating turret that shoots a laser. 

I was thinking the laser would be a skinny rectangle that transitions in height.

But i was thinking that I needed another shape that traveled along the laser’s path because I wanted to detect the x,y coordinates of the tip of the laser at certain points in time.

I see now that there is a way to use trajectories and physics, do you think that should be the way to go?

Thanks in advance,

** FIXED LINKS HTTPS often gets hosed ** 

OK, this is a quick answer, and not precisely what you want, but close enough to get you started.

You’re trying to do this:

  • Calculate a vector from the starting point (origin) of the beam/projectile towards the target (aim point)
  • Calculate an angle from the vector.
  • (for a projectile only) calculate a second vector representing the projectile velocity and direction (from first vector)

This video show that: 

https://www.youtube.com/watch?v=wklDDanKEeQ

This code implements the code in the video:

Note: This uses an OLD version of SSK and I took this code offline for a bit, but you’re the second person to need it i a week, so I put it back up.  I’ll eventually transfer it to the SSK Sampler, but for now it will work fine.

The code at the link has 3 variations on the solution to the original question,  you want to look at the third example and specifically at the fireBullet() code.

From there you can abstract the calculations to your usage.

Thanks a lot!

I will look at the examples you provided!

Are you trying to make a rotating turret that fires ‘projectiles’?

Hi thanks for the reply,

Something like that, actually a rotating turret that shoots a laser. 

I was thinking the laser would be a skinny rectangle that transitions in height.

But i was thinking that I needed another shape that traveled along the laser’s path because I wanted to detect the x,y coordinates of the tip of the laser at certain points in time.

I see now that there is a way to use trajectories and physics, do you think that should be the way to go?

Thanks in advance,

** FIXED LINKS HTTPS often gets hosed ** 

OK, this is a quick answer, and not precisely what you want, but close enough to get you started.

You’re trying to do this:

  • Calculate a vector from the starting point (origin) of the beam/projectile towards the target (aim point)
  • Calculate an angle from the vector.
  • (for a projectile only) calculate a second vector representing the projectile velocity and direction (from first vector)

This video show that: 

https://www.youtube.com/watch?v=wklDDanKEeQ

This code implements the code in the video:

Note: This uses an OLD version of SSK and I took this code offline for a bit, but you’re the second person to need it i a week, so I put it back up.  I’ll eventually transfer it to the SSK Sampler, but for now it will work fine.

The code at the link has 3 variations on the solution to the original question,  you want to look at the third example and specifically at the fireBullet() code.

From there you can abstract the calculations to your usage.

Thanks a lot!

I will look at the examples you provided!

HI roaminggamer,

How can I start to shoot whih an offset of X, regarding the turret center?

The ideia is to see the ball being shooted not from the center but being shooted starting at the turret boundaries.

Thanks

In a nutshell do this:

  1. Calculate the ‘direction vector’ from the turret center to the target.

  2. Convert that vector to a unit vector (normalize it)

  3. Make a copy of the vector from step #2 (you’ll need it  later for step #7).

  4. Scale your original normalized vector (from step #2) by the radius of the turret.  

  5. Add the vector from step #4 to the position of the turret.  

  6. Create your bullet at the < x, y > position specified by the vector from step #5.  (Be sure to add a body)

  7. Take then vector copy from step #3 and scale by the magnitude of the bullet (i.e. how fast you want it to go in pixels per second).

  8. Call set linear velocity with the <x,y> values of the vector from step #7

Thanks for the answer :slight_smile:

My idea is to create a Laser Machine that displays a laser (using the raycast function) and reflect on each obstacle.

My question is: What kind of objects can I use to make the laser beam? Drawing lines …?

Lines, rectangles, image rectangles… all will work, depending on the effect you’re looking for.

Hi.

What is the difference between properties and methods in corona?

For instance: https://docs.coronalabs.com/api/type/Body/index.html

thanks

Is this related to the initial question?  It’s not good to add new questions to a single-topic forum post… having said that:

local bob -- a local variable \_G.bill = 10 -- a global variable billy = 9 -- a global variable local function ted() -- local function end local function sue() -- global function end function susan() -- global function end ------------------------------------------------------------ ------------------------------------------------------------ local obj = {} -- A Table obj.myName = "Bud" -- A user-defined field/property on a table function obj.doit() -- A user-defined function on a table. print("HI") end obj.doit() -- Calling the function function obj.doit2( self ) -- Another user-defined function (with explicit self as first argument) on a table. print("Hi, my name is", self.myName ) end obj.doit2() -- doit2( obj ) called as a function obj:doit2() -- doit2() called as a method function obj:doit3( ) -- A user-defined method (implied self as first argument) on a table. print("Hi, my name is", self.myName ) end obj.doit3() -- doit3( obj ) called as a function obj:doit3() -- doit3() called as a method ------------------------------------------------------------ ------------------------------------------------------------ local circ = display.newCirlce(0,0,100) -- A display object circ.myName = "Bud" -- A user defined field/property on a display object. function circ.doit() -- A user defined function on a display object. print("HI") end circ.doit() -- Calling the function function circ.doit2( self ) -- Another user defined function (with explicit self as first argument) on a display object. print("Hi, my name is", self.myName ) end circ.doit2() -- doit2( circ ) called as a function circ:doit2() -- doit2() called as a method function circ:doit3( ) -- A user defined method (implied self as first argument) on a display object. print("Hi, my name is", self.myName ) end circ.doit3() -- doit3( circ ) called as a function circ:doit3() -- doit3() called as a method

Hi gyus,

When I switch (using composer) from scene1 to scene2, my display objects inserted into scene1 are visible in scene2 … ??

What i’m doing wrong? I’m using scene:insert(…) to add my display objects

Thanks

@luisxpto32,

Not seeing what that has to do with the original question.

HI roaminggamer,

How can I start to shoot whih an offset of X, regarding the turret center?

The ideia is to see the ball being shooted not from the center but being shooted starting at the turret boundaries.

Thanks