Results 1 to 15 of 15

Thread: Directions

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225

    Directions

    Hiyas,

    I have a tile-based game, and I'm trying to make the player look in a certain direction (N, NE, E, SE, S, SW, W, NW) according to a mouse click.

    I figured out how to do part of it (break up the degrees into 8 directions...and the rest), but how am I supposed to calculate the degrees of the click from the player's position? I have the X/Y point of the person, and X/Y point of the click.

    Thanks,

    -Git

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    depends how you want the player to walk:
    Code:
      /----->B
     /
    A
    to get from A to B, in NE and then E, or just straight to B, that may look stupid unless you have 16 or preferably 32 directions. For 8 directions you can simply use > = < comparation between the coordinate components. For more free movement you can use arctangent for offsety/offsetx , it all depends how you want.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    It's only a simple 2D grid game. I've figured out how most of the sight code will work, but the only problem is I can't figure out how to mathematically calculate the amount of degrees the click X/Y is from the source X/Y (eg. if the click is above and to the left, the click X will be bigger, click Y smaller, and it'll be north-east).

    How do I do that?

    -Git

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    as said, you can do it with arctangent, take a look at
    http://forums.vb-world.net/showthrea...=anglebyoffset
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    I'd do it another way:

    Code:
    'How to store the direction:
    
    Dim PlayerDirX As Long, PlayerDirY As Long
    
    ...
    
    'To calculate the direction:
    
    'This will return the sign of the mouse's position relative to the center of the screen
    PlayerDirX = Sgn(ScreenWidth / 2 - MouseX)
    PlayerDirY = Sgn(ScreenHeight / 2 - MouseY)
    
    ...
    
    'To move the player:
    
    'PlayerSpeed is the speed of the player, in pixels
    PlayerX = PlayerX + PlayerDirX * PlayerSpeed
    PlayerY = PlayerY + PlayerDirY * PlayerSpeed
    Hope that works. It's the more efficient way to store it in my opinion, the computer doesn't need to know if it's left or right, just to make it work
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Alrighty, I finally figured this out. Basically I needed to find the radians using Atn((X1 - X2) / (Y1 - Y2), and then convert it to degrees...

    I ending up figuring out most of the rest...except for one thing. How do I convert a specific angle (preferably in degrees, but also maybe in radians), to X/Y vectors?

    Thanks,

    -Git

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    As a side issue, unless you want the user to directly interact using degrees as angle unit, i'd suggest you use radians only. This is because the trigonometry units in vb are in radians.
    To go back to vector components from angle:
    X = cos(angle)
    Y = sin(angle)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Thanks. =)

    I will still need degrees to sort the angle into one of the 8 directions. There's probably a quicker way to do it, but then it just gets too hard (and I really hate trig... ).

    I'll just put the degrees/radians angles into two different variables.

    Thanks again for your help.

    -Git

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    if you want to directly convert to 8'direction variable (from radians) the formula is:
    d8=8*rad/(2*pi)

    simplified:
    d8=1.27323954473516*rad
    if you need a phase offset so that you can adjust the direction to not switch when perpendicular to screen, you add a sixteenth revolution
    d8=1.27323954473516*rad + 0.392699081698724
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Interesting... I'll have a play with that code in a sec. =)

    Btw, with the vector code you supplied (which uses Sin/Cos), is the velocity equal to 1? (the distance from origin points to the origin points + vectors) I'm assuming it is, but I'm not sure, because I don't know very much about this kind of math...

    Thanks,

    -Git

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yes, i forgot to mention that you will get the unit vector with that formula, By multiplying both components with the original magnitude you get the original vector. The magnitude of a vector is
    sqr(x*x+y*y)
    In other words, a vector can be interpreted as a direction and a magnitude (angle, length) AKA polar coordinate, as well as dimension components (x,y) AKA cartesian coordinate.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Well, the way you're saying is good only if you want more than 8 directions... but that doesn't seem to be the case, my way handles 8 directions very well and trig is slooow in VB if you use it too much, you know? (I'm not saying your way doesn't work, but since I haven't learned trig in school I had to find another way using simple math )
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Comparatively yes, but there's no extensive use of it anyway so doesn't matter, anyways seems like he wants to work with vectors and only convert to 8 direction for the image sequences.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Ok, no problem

    Hey Kedaman, why do you have "4569030913323659107" instead of your member "place" (sorry but I can't find the word right now)? It doesn't seem to happen with anyone else
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    if you can find out, you'll get the cool prize http://www.vbforums.com/showthread.p...threadid=76526
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width