# x, y cords of a triangel (trig?)

This topic is 5453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay here we go, Im, trying to do a 2D Z targeting system for a game, think The Legend of Zelda: Ocarina of Time, except overhead like the good old 2D Zelda's. Basically you target an object on screen (enenmy) and you can sidestep around them in a circle and move towards of away from them. Now what im having trouble whith is figuring out your character's x, y cordinates when you sidestep around him. So whith a triangel I have point a - your characters current x, y, and b - the object you currently have selected's x, y . Also the angle abc. I also have the length of the line segment ab - your characters x,y to the object you have selected's x, y. And the length of the line segment bc - the object you currently have selected's x, y to where your you x,y will be if you sidestep in that direction. That's probably fairly confusing, no thanks to my explaination so heres a picture of the triangel im describing. a(20,15) a(20,15) l c(?,?) b(20,5) l / c(?,?) l / line seg ab(10)pixils l / line seg bc (10) pixils l / angle abc(35) degrees l / l / l/ b(20,5) So what I'm trying to figure out is point c (x,y) If any of the other line segs, ac, or angles, acb, bac of are importance, I dont have a value of them, if they are neccisary it would be nice of you to tell me how to get them as well. Well I've tried my best to make this easy to read, but if it's not sorry in advanced for all spelling errors, also any questions please ask.

##### Share on other sites
Wow sorry that trianlge came out nothing like it did when i was typing it.

If anyone tells me how to get a picture in here, detailed steps please, then ill put a picture of the triangle up.

thanks

##### Share on other sites
a(20,15)                 a(20,15)     l       c(?,?)      b(20,5)     l      /            c(?,?)     l     /             line seg ab(10)pixils     l    /              line seg bc (10) pixils     l   /               angle abc(35) degrees     l  /     l /     l/b(20,5)

actually a would be (20, 5) and b would be (20, 15).
on a computer, the coordinates start at the most upper-left corner at (0, 0).
x increases in this (--------------->) direction.y increases in this (|                     |                     |                     |                     |                    \/ ) direction,

from (0, 0).

thanks

##### Share on other sites
By projecting C onto AB, you get a right triangle, so you can use sin to get the distance from AB to C, and cos to get the distance from B to the projection of C onto AB.

Alpha_ProgDes: < nitpick > this is wrong in some languages or some APIs (such as DirectX, where 0,0 is in the center and y increases upwards). < /nitpick >

##### Share on other sites
I'm assuming you're already storing the distance from a to b, since your movement sceme is based on it. Otherwise it would be a waste of time to calculate it, and ToohrVyk's method is fastest (no square root).

Otherwise, here you go:

d is the distance between a and b
d2 is the different distance between b and c (like, your character moved in or out this frame)
ang will be the angle between a, b, and the axis
theta is the angle you're rotating by
ang2 will be the final angle of c from b
ang=atan2(a.x-b.x,a.y-b.y);ang2=ang+theta;c.x=cos(ang2)*(d+d2);c.y=sin(ang2)*(d+d2);

Hopefully it's clear why this works.

##### Share on other sites
Hey thanks for the help really appretiated.

A few questions though, I proabably should have mentioned that this was for my final project in grade 11 comp science, and the language we're using is Turing.

So I'd imagine that some of the math may be a bit different becuse the x-0, and y-0 are at the bottom left of the screen not the top left like some other languages.

So I've been trying to work with Tubular's explanation, so just a few questions about that.

1.In the line ang=atan2
what value would I put in for a?
is it the position of it (x,y)?
2.(a.x-b.x,a.y-b.y);
This line subtracts a's x with b's x and same for the y's but wont there bee any error in the equasion when i compile due to the comma.
Remember im using turing, so maybe this is perfectly all right in C++.

Thats's all, thanks again for the help.

##### Share on other sites
GillaMobster: my method is a general method, it works in any language, regardless of the coordinate system.

Concerning Tubular's solution, what he does is:

Compute b.x - a.x, store into E1
Compute b.y - a.y, store into E2
Compute arctangent E1 E2, store into ang (atan2 == arctangent)
Compute ang + angle between AB and BC, store into theta
Compute d + d2, store into E3
Compute cos(ang), store into E5
Compute sin(ang), store into E6
Compute E5 * E3, store into c.x
Compute E6 * E3, store into c.y

In the end, you have got the coordinates c.x and c.y, now it's up to you to rearrange all this.

Note: if AB is always vertical with B at the bottom, and the lengths of AB and BC are equal, then you automatically have:

theta = angle between AB and BC
E3 = d

So you can skip anything needed to compute these values in the above program.

##### Share on other sites
Right, sorry about the language stuff.

atan2(x,y) is a function that just does
tan-1(x/y)
but it also handles special cases when y is 0. You can replace this with the tangent function and some if statements for when y is too close to zero. Naturally the value of the angle in that case depends on the sign of x.

I do a.x-b.x and a.y-b.y to calculate the position of the player (a) relative to the target (b). .x and .y being their x and y coordinates respectively.

And of course, ToohrVyk's suggestions are correct. You should get rid of / simplify the ang and theta stuff or the d and d2 stuff if you can.

##### Share on other sites
Thanks everyone, its really clear now and i'm fairly confident i can get this to work.
Thanks.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46