Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 118,692 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,190 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Determining heading on a grid

 
Reply to this topicStart new topic

Determining heading on a grid

rjolitz
post 4 Aug, 2008 - 04:49 PM
Post #1


D.I.C Head

Group Icon
Joined: 17 May, 2008
Posts: 83



Thanked 1 times

Dream Kudos: 75
My Contributions


Evening All.

I'm working on a browser based game for starship combat.

My plan, at this point, is to use a standard x y grid for positioning. What I'm not seeming to be able to figure out is how to figure what direction a ship will be facing based on movement.

using:

CODE


A .. B .. C
D .. .. .. E
F .. G .. H



A ship moving from (1,1) to (5,5) would be at heading H after that move. What I'm having trouble with is figuring out to determine that.

I'm working in PHP on this [suprise, suprise].

Can anyone kinda point me in the right direction on how to tackle this? I don't necessarily want the code [but won't turn it down] but some idea of a way to implement this.

Any help appreciated assuming this isn't clear as mud.

User is offlineProfile CardPM

Go to the top of the page


AdamSpeight2008
post 4 Aug, 2008 - 05:09 PM
Post #2


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


The basics are
CODE

Xd=X1 - X0
Yd=Y1 - Y0
If Xd<0 and Yx<0 Then Return A
If Xd=0 and Yx<0 Then Return B
If Xd>0 and Yx<0 Then Return C
If Xd>0 and Yx=0 Then Return D
If Xd>0 and Yx>0 Then Return E
If Xd=0 and Yx>0 Then Return F
If Xd<0 and Yx>0 Then Return G
If Xd<0 and Yx=0 Then Return H
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 4 Aug, 2008 - 05:19 PM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 37 times

Dream Kudos: 525
My Contributions


Well it helps to look at this mathematically.

your change is x is (5 - 1) = 4 > 0 so you know you are moving easterly (C, E, H).

your change in y is (5 - 1) = 4 > 0 so you know you are moving in... well I assume a northly direction (F G H)

The intersection of the two sets is H.

OK -- so to crystallize this a little:

deltaX = (X_to - X_from);
deltaX = deltaX/abs(deltaX); //normalize
deltaY = (Y_to - Y_from);
deltaY = deltaY/abs(deltaY); //normalize

Now the deltas have 3 possible values -1, 0, 1

your grid is

A = (-1, 1)
B = (0, 1)
C = (1, 1)
D = (-1, 0)
E = (1, 0)
F = (-1, -1)
G = (0, -1)
H = (1, -1)

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/12/08 08:13AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month