|
-
May 13th, 2001, 05:48 AM
#1
Thread Starter
Addicted Member
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
-
May 13th, 2001, 06:57 AM
#2
transcendental analytic
depends how you want the player to walk:
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.
-
May 14th, 2001, 07:28 AM
#3
Thread Starter
Addicted Member
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
-
May 14th, 2001, 07:58 AM
#4
transcendental analytic
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.
-
May 14th, 2001, 09:43 AM
#5
Frenzied Member
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
-
May 15th, 2001, 06:35 AM
#6
Thread Starter
Addicted Member
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
-
May 15th, 2001, 07:14 AM
#7
transcendental analytic
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.
-
May 15th, 2001, 04:48 PM
#8
Thread Starter
Addicted Member
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
-
May 15th, 2001, 07:44 PM
#9
transcendental analytic
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.
-
May 16th, 2001, 02:04 AM
#10
Thread Starter
Addicted Member
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
-
May 18th, 2001, 12:28 AM
#11
transcendental analytic
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.
-
May 18th, 2001, 09:09 AM
#12
-
May 18th, 2001, 10:49 AM
#13
transcendental analytic
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.
-
May 18th, 2001, 11:41 AM
#14
-
May 18th, 2001, 11:57 AM
#15
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|