A maths question.

Previous topic - Next topic

Hatonastick

School was a long time ago, I don't require this sort of thing normally, and well lets face it -- this sort of maths was never my strong point anyway, so much so I don't even know what sort of maths this is (except maybe vector related).

I have a 2D plane with x,y co-ordinates where x and y start at 0 and go up, so not the usual 4 quadrant plane you get in so many examples where x and y are capable of being negative in value.  In this case they are always going to be positive.

On that plane I have a position P with x, y value that has a known facing (an angle) and magnitude (distance/speed) to another point T, and I want to calculate the x,y coordinates of point T.

So.
                             Magnitude/Distance/Speed                   
P--------------------------------------------------------------->T
x,y                                                                              x,y?

Does that make sense or do I need to draw an actual diagram?  All the data I've given is all I have to work with.  I know I've done this sort of thing in the past, but currently I can't for the life of me work out how to do it and it's driving me up the wall...  I'm sure there's a formula that handles this.

Edit:  Ok I've attached a diagram that explains it better.  Is it just me or is the angle orientation a little weird?  This isn't in GLB btw, hence why this isn't being asked in the main section.  It's for a game engine I'm doing a mod for and I've given up trying to get a useful response from people on their forum.  Making a mod because a) I play this game a lot to relax but theres many things I want to fix and b) I'm using it to learn 3D modelling as I'd hoped (but it's not working out that way) that I could concentrate more on doing 3D objects/graphics and less on programming.

[attachment deleted by admin]
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Moebius

First of all, please let me know if I've misinterpreted this - it doesn't seem that hard anyway...

The 'vector' (aka. distance along multiple dimensions) you need to 'modify' to your starting point P by is described by a distance and an angle.  Because you are working with X and Y values for your points, you have to convert the "distance+angle" vector into an X-distance and a Y-distance.  Once you have these distances, if you add them to the starting point P, you will get out the (X,Y) coordinates for ending point T.

So, you just need to use Sin() and Cos() in the right ways to get the sides of the 'triangle' that are the 'Width' and 'Height' components (aka. X amount and Y amount) of the movement.  Hopefully I've made sense so far...

I can't explain why properly without a diagram, but see the comments here:
Code (glbasic) Select
DX = COS( Angle + 90 ) * Distance   //Distance-X is determined by Cosine of the angle, and because '0 degrees' points down, we need to add 90 degrees to 'rotate' the angle.
DY = SIN( Angle + 90 ) * Distance   //Distance-Y is determined by Sine of the angle, and again we need to add 90 degrees.


In most cases, COS is used for X, and SIN is used for Y.  These functions have their '0 degree mark' pointing right, unlike down in your diagram, so you need to add 90 degrees to rotate this 0 mark down to the bottom.

After you've got the X and Y components of the movement, just add them to the starting point:
Code (glbasic) Select
TYPE Point
    X
    Y
ENDTYPE

LOCAL P AS Point
LOCAL T AS Point
LOCAL Angle, Distance

//........

// Get T:
T.X = P.X + ( COS(Angle + 90) * Distance )
T.Y = P.Y + ( SIN(Angle + 90) * Distance )


Hopefully I've understood what you've asked and you can somehow understand me  :whistle:
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Hatonastick

#2
Aha!  Yes that's the maths I was looking for.  And yes the angles are a bit weird.  I've attached a more normal x,y orientation diagram, but the angle orientation still isn't what you'd expect (and I didn't).

Edit:  Whoops, left out x and y.  Fixed.

Aaargh!  One reason why you don't see me here much anymore is the amount of times I have to try to get something to post.  I've been plagued by timeouts with the GLB forums for ages now.

Anyway, thanks VERY much mate.  You are right on the money.  Just been testing it and it works brilliantly.  :good:

[attachment deleted by admin]
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

matchy

#3
Cleaner example:
Code (glbasic) Select

TYPE _vector; x; y; speed; angle; ENDTYPE
GLOBAL player AS _vector
// ..
INC player.x, COS(player.angle) * player.speed
INC player.y, SIN(player.angle) * player.speed

XanthorXIII

Quote from: matchy on 2011-Mar-21
Cleaner example:
Code (glbasic) Select

TYPE _vector; x; y; speed; angle; ENDTYPE
GLOBAL player AS _vector
// ..
INC player.x, COS(player.angle) * player.speed
INC player.y, SIN(player.angle) * player.speed


Show off! :P

Seriously though pretty good stuff there.
Owlcat has wise

matchy

XanthorXIII, cool but it's general simple stuff.

What I don't understand is why quote code blocks (don't answer that!).