Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Mouse.Where relative angle question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lordroba




PostPosted: Sun Sep 25, 2005 11:47 am   Post subject: Mouse.Where relative angle question

hey

i'm making a game where there is a spaceship in the middle of the screen and you rotate it by using the mouse.

The spaceship will always stay in the middle, but as you move the mouse around the spaceship, the graphic will rotate depending on where the mouse is. The problem is, I just can't figure out how to find the angle between the x,y cordinates of the mouse and the x,y coordinates of the spaceship.

Thanks in advance
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Sun Sep 25, 2005 12:31 pm   Post subject: (No subject)

You'll need to use arctand and check a few cases to do this. I wrote a function for this some time ago, I just can't recall what happened to it. But hey, you wouldn't learn as well if I handed you the answer.

Note: I'm using the functions tand() and arctand() because I prefer to deal with degrees.

Let's start from the basics:
What information do you start off with?
- the position of the space ship (middle of the screen): midx, midy
- the position of the mouse: mousex, mousey

What do we (hopefully) know?
tand(angle) = slope
and therefore:
arctand(slope) = angle
since slope is dy/dx:
arctand((mousey - midy)/(mousex - midx)) = angle

And there you have it! But there are problems with this: most notable, if you're mouse is in the exact middle of the screen, you'll crash the program with your attempt to divide by zero. The other problem is that arctand will only return an angle from -90 < angle < 90 (notice the "<" instead of "<=", this is because of the error described above; you can't describe a vertical line using slope).

So what can you do? Check cases. There are four possible events:

1) mousex = midx and:
1.1) mousey >= midy which means angle = 90 or
1.2) mousey < midy which means angle = -90

Note: you've got to check for the case when the mouse is in the exact middle of the screen, so I added it in with case 1.1

2) [i]mousex > midx
: this is the best thing that could happen. As long as this is what happens, you don't really have any extra work to do. The problem is with:

3) mousex < midx: Draw a line with a direction that goes 1 unit left and 1 unit down. Now draw a line with a direction that goes 1 unit right and 1 unit up. Oops, they're the same! When you know this is happening, do the same calculation to get the angle as before, except this time add 180 degrees to get the correct measurement.

And that's really it (unless I've forgotten something). But there are still problems here. You're getting angles from -90 to 270 degrees. Weird. In the case of your game this may not be an issue, but if you'd prefer 0 to 360, just add 360 when the angle is negative.

That's all I have to say right now, because I've got a pounding headache and I'm willing to bet much of what I've said already is confusing enough. Please let me know if I can clear some of that up for you or if you need (specific) help writing a function for it.
codemage




PostPosted: Mon Sep 26, 2005 11:38 am   Post subject: (No subject)

Here's a rough little bit of code that I ripped from a program I made a while ago.

If you can figure out what the variables are (it's not too complicated) - you'll get the idea of what they're doing here.

It's not 100% accurate - I didn't care about the difference of a degree or so in order to avoid trig errors (div0 and such)... but it does the trick.

code:


    var mousex, mousey, button : int
    Mouse.Where (mousex, mousey, button)

    var deltax : int := mousex - playerx
    var deltay : int := mousey - playery

    %tweak virtual mouse position to avoid div by zero error
    if (mousex = playerx) then
        mousex := mousex - 1
    end if

    % correct angle from player to mouse cursor | angle is positive 0 to 360
    cursordirection := arctand ((mousey - playery) / (mousex - playerx))
    if deltax <= 0 then
        cursordirection := cursordirection + 180
    elsif deltax > 0 and deltay < 0 then
        cursordirection := cursordirection + 360
    end if
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: