
-----------------------------------
metachief
Sun Feb 17, 2008 6:31 pm

rotation
-----------------------------------
Could someone tell me how to make a picture rotate according to the direction my mouse is pointing to?

-----------------------------------
Sean
Sun Feb 17, 2008 6:35 pm

Re: rotation
-----------------------------------
Pic.Rotate to your Mouse X and Y variables?

-----------------------------------
fishtastic
Sun Feb 17, 2008 7:05 pm

Re: rotation
-----------------------------------
ah... i think i know what you mean.
what you need is to figure out the mouse angle.
based on the x,y coordinate of the picture and the coordinate of the mouse.


View.Set ("offscreenonly")
var x2, y2, mc : int
loop
    cls
    mousewhere (x2, y2, mc)
    var d := Math.Distance (x2, y2, maxx div 2, maxx div 2)
    if d > 0 then
        var angle := arccosd ((x2 - maxx div 2) / d)
        if y2 - maxx div 2 < 0 then
            angle := 360 - angle
        end if
        Draw.ThickLine (maxx div 2, maxx div 2, x2, y2, 3,black)
        put angle : 2 : 2
        View.Update
    end if
end loop


-----------------------------------
metachief
Sun Feb 17, 2008 7:11 pm

RE:rotation
-----------------------------------
um..what i meant was , for example: if i have a triangle, i want one of the angles to point at the location of the mouse.
So i know it has somethig to do with Pic.Rotate, but i am not sure.

-----------------------------------
fishtastic
Sun Feb 17, 2008 7:35 pm

RE:rotation
-----------------------------------
1.draw a triangle
2.use pic.new to take a picture
3.figure out the angle of the mouse
4.use pic.rotate to create a rotated picture
5.draw the pic returned by pic.rotate

-----------------------------------
metachief
Sun Feb 17, 2008 7:41 pm

RE:rotation
-----------------------------------
thanks i got the consept , but how do i plug in the mouse coordinates as the angle?

-----------------------------------
fishtastic
Sun Feb 17, 2008 7:43 pm

RE:rotation
-----------------------------------
my code above

-----------------------------------
Sean
Sun Feb 17, 2008 7:43 pm

Re: rotation
-----------------------------------
Your Mouse X  and Mouse Y would equal a position, then you would want one of your rotating angles to equal the mouses.

-----------------------------------
metachief
Sun Feb 17, 2008 7:50 pm

RE:rotation
-----------------------------------
Here's my code:
var gun: array 0..35 of int

    gun (0) := Pic.FileNew ("gun.bmp")

    for angle : 0..35
    gun(angle):= Pic.Rotate (gun (0),angle*10,player_x,player_y)
    end for
    
    Pic.Draw (gun(0), (player_x * 10)+4, (player_y * 20)+5, picMerge)

i don't know how to make the mouse coordinates into the angle of the picture.
PS: The code above doesn't work, because i ca't do math distance for some reason.

-----------------------------------
metachief
Sun Feb 17, 2008 8:20 pm

rotation2
-----------------------------------
Me again. I tried, but i ran into another problem.

This is my code:
  
    Mouse.Where (z,t,button)
    var d := Math.Distance (z, t, maxx div 2, maxx div 2) 
    if d > 0 then 
        var angle := arccosd ((z - maxx div 2) / d) 
        if t - maxx div 2 < 0 then 
            angle := 360 - angle 
        end if  
    gun (0) := Pic.FileNew ("gun.bmp")
    gun(0):= Pic.Rotate (gun(0),round(angle),px,py)
    end if 
    
    Pic.Draw (gun(0), (px * 20)+4, (py * 20)+5, picMerge)
    View.Update

The problem with it is that my picture doesn't stay in the same place, but instead when i move my mouse around it rotates in circles

-----------------------------------
CodeMonkey2000
Sun Feb 17, 2008 8:20 pm

RE:rotation
-----------------------------------
For make a function called getAngle. This function will take the position of the mouse and the position of the picture and get the angle between them. Next when you draw the picture you will draw picture(calculated angle divided by 10).

-----------------------------------
CodeMonkey2000
Sun Feb 17, 2008 8:22 pm

RE:rotation2
-----------------------------------
Please stick to one thread.

-----------------------------------
metachief
Sun Feb 17, 2008 8:25 pm

RE:rotation2
-----------------------------------
what difference does it make?
By the way do u think u could help me

-----------------------------------
CodeMonkey2000
Sun Feb 17, 2008 8:46 pm

RE:rotation
-----------------------------------
Basically you need to find the angle between the player and the mouse. To do this you will need to know some trigonometry. More specifically you need the tangent and tangent inverse functions. Now I assume that you are in grade 10 and thus have a limited knowledge of trig. So being the nice guy I am here is my angle function. fcn angle (px, py, tx, ty : int) : real
    var adjecent : real := tx - px
    var opposite : real := ty - py
    if adjecent = 0 and ty > py then
        result 90
    elsif adjecent = 0 and ty  px then
        result 0
    end if
    if opposite = 0 and tx < px then
        result 180
    end if
    if ty < py then
        if arctand (opposite / adjecent) > 0 then
            result 180 + arctand (opposite / adjecent)
        else
            result 360 + arctand (opposite / adjecent)
        end if
        result 180 + arctand (opposite / adjecent)
    else
        if arctand (opposite / adjecent) < 0 then
            result 180 + arctand (opposite / adjecent)
        else
            result arctand (opposite / adjecent)
        end if
    end if
end angle


Just pass in the player x,y and the mouse x,y and this function will return the angle. I programmed this when I was in grade 10, so this code is really messy and inefficient. And if you still don't know what to do, here is an example.

-----------------------------------
fishtastic
Sun Feb 17, 2008 8:59 pm

RE:rotation
-----------------------------------
metachief I already answered your question, dont drag more people in to answer the same question.

you said "but how do i plug in the mouse coordinates as the angle?" 
i already answered it before you ask that.

CodeMonkey2000 was nice enough to help, but read the reply before posting a new one. dont expect people to give you exactly what you need.

-----------------------------------
ericfourfour
Sun Feb 17, 2008 11:39 pm

Re: rotation
-----------------------------------
I didn't use any test cases so I'm unsure if it will give the correct angle but I think it does the best job explaining how to get the angle.

% CAST rule. This is like grade 10 trig.
%  S|A
%  ---
%  T|C
% Quadrant 1: all positive (x > 0, y > 0)
% Quadrant 2: sin positive (x < 0, y > 0)
% Quadrant 3: tan positive (x < 0, y < 0)
% Quadrant 4: cos positive (x > 0, y < 0)
% The start angle is always tan^-1(abs (y / x)).
% Just add or subtract 180 or 360 to put it in the correct quadrant.

% Translate to the origin then sub in x and y.
fcn getAngle (x, y : real) : real
    if x > 0 then
        if y > 0 then % quadrant 1
            result arctand (abs (y / x))
        else % quadrant 2
            result 180.0 - arctand (abs (y / x))
        end if
    elsif x < 0 then
        if y > 0 then % quadrant 4
            result 360.0 - arctand (abs (y / x))
        else % quadrant 3
            result 180.0 + arctand (abs (y / x))
        end if
    else
        if y > 0 then % on upper y-axis
            result 90.0
        else % on lower y-axis
            result 270.0
        end if
    end if
end getAngle

If you have taken grade 10 math, this is stuff is in your notes. For more information: http://en.wikipedia.org/wiki/Cartesian_coordinate_system.

-----------------------------------
OneOffDriveByPoster
Mon Feb 18, 2008 9:32 am

Re: rotation
-----------------------------------
In many programming languages, there is an atan2(y, x) function for this.  It is important to understand what goes behind it though--not all implementations work the same.

-----------------------------------
metachief
Mon Feb 18, 2008 10:09 am

RE:rotation
-----------------------------------
thank you very much guys, you were alot of help..sorry for making it a big deal, it's just im a beginner at this, and im trying to get ahead fast
