Jump to content
  • Advertisement
Sign in to follow this  
GillaMobster

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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).

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!