Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Rotate a stick around a ball ? — Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Top Posters

• ar2rsawseen 6991
• SinisterSoft 4001
• atilim 2892
• hgy29 2836
• keszegh 2641
• antix 2134
• OZApps 1983
• totebo 1778
• oleg 1695
• hgvyas123 1412
• techdojo 1321
• MoKaLux 1297
• gorkem 1186
• phongtt 1029
• Mells 1024
• MikeHart 1020
• john26 995
• GregBUG 962
• pie 962
• talis 925

# Rotate a stick around a ball ?

edited June 2015
In my billiard game , m showing projection of cue ball on mouse events . Simultaneously i also want to move the billiard stick around the cue ball in the direction of projection on mouse event . How can i do that ?
I have done by calculating slope and some more lengthy stuff .
Just wanted to know, is there an easier way to achieve this.

• edited June 2015 Accepted Answer
You basically need to calculate two things
1) x,y position of constant distance away from the ball at provided angle
2) angle in which to rotate the stick so it will always be directed to ball

So lets say your ball is at x1,y1 and mouse is at x2, y2 coordinate.

Calculate a vector
dx = x1-x2
dy = y1-y2

1) Find a pointX, pointY which, let's say is 100 pixels far from ball on dx,dy vector
 ```local fromDistance = 100 local ratio = math.sqrt((fromDistance*fromDistance)/(dx*dx + dy*dy)) local pointX = x1 + dx*ratio local pointY = y1 + dy*ratio```
So you need to position stick at pointX,pointY

2) Find angle of vector
 ```local length = math.sqrt(dx*dx + dy*dy) local angle = math.acos(dy/length)   --this part depends on how your original image is rotated if(dx > 0) then angle = -angle end```
If stick by default was rotated to 0 degree, then simply apply angle to stick, note that angle is in radians, you need to convert it to degrees to use on sprite with math.deg(angle)

have not tried the code, so may contain errors, but logic should be somewhat correct • 