x, y cords of a triangel (trig?)

Started by
8 comments, last by GillaMobster 19 years, 6 months ago
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.
Advertisement
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
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).

Beginner in Game Development?  Read here. And read here.

 

thanks
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 >
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.
VideoWorks Softwarehttp://www.3dpoolpro.com
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.
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.
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.
VideoWorks Softwarehttp://www.3dpoolpro.com
Thanks everyone, its really clear now and i'm fairly confident i can get this to work.
Thanks.

This topic is closed to new replies.

Advertisement