A problem with math - formula needed

Started by InCreator, Thu 11/11/2004 13:23:43

Previous topic - Next topic

InCreator

I'm not sure if it's approriate place to ask this, but...



I have two points. A and B.
Point A has defined direction. If we'd treat points as objects or characters in a game, point A had variables

A.x
A.y
A.direction

Point B needs only x and y.

And there's global-like variable, which shows distance between points A and B. Let's call it 'distance'.

What I need is a formula to calculate
x and y coordinates for point B, so it's always at the point where arrow at point A is pointing at - so formula must take point A's direction account. It also should maintain correct distance from point A.

The green circle on the picture marks the trajectory where point B could be, depending on direction of point A.

Now, It's actually really simple problem, It's just that I'm really weak at sinus and cosinus stuff - which most likely - IMO - should do the trick... if used properly.

So, whatever A.x, A.y and A.direction are,
what's

B.x =
B.y =

As a final result, when I need to change distance and direction of point A, point B should always get to the right position.

Radiant

#1
Distance a to b = square root of ((a.x - b.x) * (a.x - b.x) * (a.y - b.y) * (a.y - b.y))
(by Pythagoream theorem)

Direction a to b = inverse tangent of (abs (a.x - b.x) / abs (a.y - b.y))
(by basic goniometry)

However, to my knowledge AGS doesn't do square root or inverse tangents. But there is a math plugin floating around the web somewhere.

Edit: your post wasn't entirely clear but maybe you meant something else... given delta as an angle from the upwards direction, going clockwise, then b.x = sine of delta, and b.y = cosine of delta. Again this needs the math plugin, or a lookup table.

InCreator

#2
Well, it's hard to understand your reply.

Let me put whole thing into a puzzle:

-----------
Coordinates of point A are (0,0)
Coordinates of point B are (4,5)
Point B lies at the direction of 35 degrees from point A
Distance between point A and B is 30 units (pixels maybe)
-----------
What would be coordinates of point B if direction changes into 170 degrees, for example (distance remains same) ?

Well that's it. I need a formula to calculate coordinates
for B(x,y).

Depending on given direction and distance.

-----------------
It's quite easy, and it's not too much AGS-related, the simpler formula, the better. I even managed to find this out once! But forgot the formula and I'm not good enough at math to know how or where to search the answer...

How to convert it into use for AGS will be my own quest later, it's not important.

EDIT:
Ah, I even got that far...


y-axis goes up by 1, not down - I know this should be other way, but this doesn't matter right now

Let's say that all dots and circles are relative to red dot at the center, which I also assign coordinates, and 4 units away.

Putting this into C-like code,
logical base I managed to find is:

purple dot (pD):
(direction = 0)
{
pD.x = center.x + distance;
pD.y = center.y;
}

orange circle (oC):
(direction = 45)
{
oC.x = center.x + distance;
oC.y = center.y +distance;
}

green dot (gD):
(direction = 90)
{
gD.x = center.x;
gD.y = center.y + distance;
}

blue circle (bC):
(direction = 135)
{
bC.x = center.x - distance;
bC.y = center.y + distance;
}

...and so on.

But if direction was - for example - 32.1 degrees?

This is where my intelligence runs out.
I feel being so close to the answer... but no. Someone has to show me the way and unlock the stupidity blocking my mind.

Radiant

Okay. If distance from A to B equals X, and angle between A-B and a line from A going straight up equals R, then
B.x = sine of R times X
B.y = cosine of R times X

Pod

#4
I'll try and figure it out tomorrow morning.

I'm unsure of what information you KNOW here, as in your puzzle you ask for the co-ords of B, yet you plainly state that they are (4,5).

TerranRich

This is hardly a Beginner question. Moved. ;)
Status: Trying to come up with some ideas...

Pod

#6
If he told us exactly what he's doing, what information he knows, and what information he needs to find out, we could probably help. As it is he's really confusing.

Radiant already gave you the answer, but I thought I'd show you why, as it also helped me a tiny wenny bit.



Code: ags

SIN 5 = c/30
:. 30.Sin 5 = c
:. c=2.614672282

COS 5 = b/30
:. 30.Cos 5 = b
:. b=29.88584094

B(x,y) => B(29.88584094,2.614672282)


A general formula is this:

Code: ags

B.x = 'r'*(Sin PHI)
B.y = 'r'*(Cos PHI)


To work out PHI you'll need to use something like the CAST diagrams. Edit2: You won't need to work it out at all. How could I forget that the Sin of Theta is the Sin of Phi? grr.

Edit: grr. Silly Anti-llama-typing features messed up the code. It keeps rendering 'R' as 'ARE' :)

InCreator

Thanks for help so far.
I'm starting to think that I discovered some really difficult problem... but It's not true. It's just very difficult to explain something you don't know...

I'm sorry for failing on explaining my objective, and not thinking of the following example earlier - it's actually really simple:
----------------------------------------------------------------------
Best example I could bring out is the arrow near player character in Grand Theft Auto 1 game.

In game, it was pointing towards mission objective.

Now let's assume that arrow always points at the same direction character is facing at. (It actually did, under bridges and other places where player could not be seen).

* arrow has it's own direction, x and y
* player character has it's own direction, x and y
* arrow must always be at some specified distance from player, not ON him, but few pixels away

Now, how did game programmers make game to calculate always right x and y for arrow to meet the requirements, while player character moved around (his x and y changed all the time)?

This is most perfect example of what I would want to know.

stuh505

x2 = x1 + Length*sin(theta)
y2 = y1 + Length*cos(theta)

(x1,y1) = character coordinates (start of arrow)
(x2, y2) =coordinates of end of arrow

theta = direction (angle) of arrow, 0 is up

Snarky

#9
The formulas posted by stuh505 are correct, with one minor error. Since the Y axis on our screen goes down (0 is at the top, 200 at the bottom), theta=0 should be down.

Also, you must measure the angle clockwise to get the right result (again because the Y axis goes down). And make sure you're using the right units for theta (probably radians) that are needed for sin and cos.

If you want to follow the standard convention, and measure theta counterclockwise from the X axis going right, use this variation:

X = X[a] + L*cos(theta)
Y = Y[a] - L*sin(theta)

stuh505

And you'll need to download the math plugin to use sin/cos.

InCreator


SMF spam blocked by CleanTalk