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

Rotate a stick around a ball ?

lightlight Member
edited June 2015 in Game & application design
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.


  • ar2rsawseenar2rsawseen Maintainer
    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

    From top of my head:

    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
    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 :)

    More info on similar problems:
  • lightlight Member
    edited June 2015
    Thanks , i have done something like that and it works . But got stuck to new problem , to hit the cueball with stick i have to pull back stick in that particular line of direction .Dont know how to pull back stick ?

    I have seen this kind of behaviour in prismatic joint , but dont know how to apply it here.
  • ar2rsawseenar2rsawseen Maintainer
    Well you can calculate distance on vector changing 100 to larger value to change cure position further from center, and on release apply impulse into the way of the ball
  • lightlight Member
    edited June 2015
    Oh yes it was right there . i mean logic
Sign In or Register to comment.