Members - Reputation: 683
Posted 23 March 2012 - 03:22 PM
I've been trying to get my head around how to do something for ages and have completely gotten myself confused with it! The idea is very simple, as the solution probably is too! But I'll just say beforehand that my math skills are a bit lacking, so maybe that's how I've gotten myself muddled.
I want to create a triangle from a character of a specific distance every time they do something. There are three points to the triangle-
Point 1 is centred on the character
Point 2 is a distance of 5 in front of the character and 5 to it's left
Point 3 is a distance of 5 in front of the character and 5 to it's right
(Point 4 to complete the triangle is obviously just Point 1 again)
Now I can (obviously) work out Point 1, but it's figuring out the other two points that I'm having difficulty with - but I'm sure the solution is really simple! I just can't get my mind to think in a suitable manner.
If anyone could help me get out of this boggle I'd very appreciative! And more so if there's not too many jibes about me being a bit special.
Crossbones+ - Reputation: 6306
Posted 23 March 2012 - 04:27 PM
How do you represent the characters orientation ?
If its 2D and the orientation is an angle(in radians) then the point in front of the character is simply:
pcx = cos(rotation)*5;
pcy = sin(rotation)*5;
to get to p2/p3 you simply add/subtract pi/2 to the rotation and get
p2x = pcx + cos(rotation-pi/2)*5; //to get p3 we add pi/2 instead
p2y = pcy + sin(rotation-pi/2)*5;
(if the angle is in degrees you add/subtract 90 instead of pi/2)
If it is 3D and you represent the position and orientation using a 4x4 matrix you can take points at 5.0,0.0,-5.0 and -5.0,0.0,-5.0 and multiply those with the matrix.
If it is 3D and you represent orientation as forward/up/right(or left) vectors then it is also quite simple as you can just scale those by 5 and then add the scaled forward vector to the position and then add/subtract the scaled right/left vector.
if it is 3D and you're using yaw/pitch/roll values then it is a mess. (allthough it is possible that you only need the yaw in which case its just like the 2D solution)
The voices in my head may not be real, but they have some good ideas!
Members - Reputation: 683
Posted 24 March 2012 - 01:55 AM
My second point was actually your third point (the triangle was appearing the wrong side of the model ) but it follows the character around now.
It is a bit twisted to one side, but I'm sure I can figure that out now you've done the hard work!
Members - Reputation: 910
Posted 24 March 2012 - 09:06 PM
I find them more flexible and they actually fully represent what you want, and more than what you can achieve with a single triangle. I've also written intersection test code for circular sectors and it's not that complicated, althought it did take me a little bit to figure out how to do. It's almost trivial to intersect test against points. Testing against lines, circles, and other circular sectors requires a bit more work, but I didn't find them to be too difficult.