C++ Learning Community Forum
September 09, 2010, 08:08:45 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: a math question  (Read 2082 times)
*brammie*
my rank:
C++ Freak
***
Posts: 310



View Profile WWW
« on: August 05, 2008, 12:47:39 PM »

i'm trying to find the pitch/roll of a terrain at a certain point.
now i have a little problem, as showed in this diagram:


i have a function to retrieve the height of a particular point in my terrain.
so. how can i achieve my goal?

oh, and by the way, this is what i got  Grin
Code
GeSHi (cpp):
D3DXVECTOR3 Terrain::GetSlope(float x,float y,float size,float zrot,float roll_distance,float pitch_distance)
{
float roll,pitch;
float heights[4]={GetHeight(x+cos(RAD(zrot))*pitch_distance,y-sin(RAD(zrot))*pitch_distance,size),
 GetHeight(x-cos(RAD(zrot))*roll_distance,y-sin(RAD(zrot))*roll_distance,size),
 GetHeight(x-cos(RAD(zrot))*pitch_distance,y+sin(RAD(zrot))*pitch_distance,size),
 GetHeight(x+cos(RAD(zrot))*roll_distance,y+sin(RAD(zrot))*roll_distance,size)};
pitch=atan2(heights[2]-heights[0],pitch_distance*2);
roll=atan2(heights[3]-heights[1],roll_distance*2);
return D3DXVECTOR3(roll,pitch,zrot);
}
 
Created by GeSHI 1.0.7.18
GetHeight() is just a member of my terrain class return the height at a particular point.
and yes, i am using z as up...
« Last Edit: August 05, 2008, 05:38:02 PM by *brammie* » Logged
draco
Geek
*****
Posts: 137


View Profile
« Reply #1 on: August 05, 2008, 06:15:07 PM »

p = atan(a / c) ? You know c right? And A is just the height of the terrain at that point, which you can find, right?
Logged
*brammie*
my rank:
C++ Freak
***
Posts: 310



View Profile WWW
« Reply #2 on: August 05, 2008, 06:19:31 PM »

the problem is,i dont know where a is as i don't know how long b is... Undecided
but i think i can do it like this:


and then p=atan(a/c) is right  Grin Grin
« Last Edit: August 05, 2008, 06:27:11 PM by *brammie* » Logged
*brammie*
my rank:
C++ Freak
***
Posts: 310



View Profile WWW
« Reply #3 on: August 05, 2008, 08:19:07 PM »

hmm.. actually that doesnt work.
it does work for the traingle, but no matter what pitch, a is always the same, but i you calculate b, you are abled to find out the pitch.

so that comes out to this:
Code
GeSHi (cpp):
D3DXVECTOR3 Terrain::GetSlope(float x,float y,float size,float zrot,float roll_distance,float pitch_distance)
{
   float roll,pitch;
   float middle=GetHeight(x,y,size);
   pitch_distance*=((pitch_distance)/(sqrt(pow(pitch_distance*2,2)-pow(middle,2))));
   roll_distance*=((roll_distance)/(sqrt(pow(roll_distance*2,2)-pow(middle,2))));
   float heights[4]={GetHeight(x+cos(RAD(zrot))*pitch_distance,y-sin(RAD(zrot))*pitch_distance,size),
                           GetHeight(x-cos(RAD(zrot))*roll_distance,y-sin(RAD(zrot))*roll_distance,size),
                           GetHeight(x-cos(RAD(zrot))*pitch_distance,y+sin(RAD(zrot))*pitch_distance,size),
                           GetHeight(x+cos(RAD(zrot))*roll_distance,y+sin(RAD(zrot))*roll_distance,size)};
   pitch=atan2(heights[2]-heights[0],pitch_distance*2);
   roll=atan2(heights[3]-heights[1],roll_distance*2);
   return D3DXVECTOR3(roll,pitch,zrot);
}
 
Created by GeSHI 1.0.7.18
i'm still debugging it so don't think it's working atm

(i'm just posting this in case someone needs it)
actually to get 150 posts but that doesnt care =3
« Last Edit: August 05, 2008, 08:45:28 PM by *brammie* » Logged
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 636



View Profile
« Reply #4 on: August 07, 2008, 02:58:17 AM »

(This is from your mathematical description, not try to understand your code.)

From your initial post, you just don't have enough information.



  • b = c cos (A)
  • a = c sin (A)
  • A = tan (a/b)
  • sin(A)/a = sin(B)/b = sin(C)/c
  • a^2 = b^2 + c^2 - 2*b*c*cos(A)
In 4 of the above case, you're missing one piece of information. In the remaining case, you're missing two.

Quote
p = atan(a / c) ?
Um, no! *point to the formulas above*

the problem is,i dont know where a is as i don't know how long b is... Undecided
but i think i can do it like this:


and then p=atan(a/c) is right  Grin Grin
Huh? Then you do know "a"! *Slap* Instead of working with triangle ΔABC, just work with ΔAB'C' (i.e. the smaller triangle).

Similar triangle. The angle A ('p' in your original picture) doesn't change.
Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
FrozenKnight
Extends without bound
Global Moderator
Dr. of C++ology
*****
Posts: 556


Do it yourself it's the only way to learn.


View Profile
« Reply #5 on: January 05, 2009, 05:23:30 PM »

To solve a triangle you need at least pieces of information.
 since you know the length of d
 and we know the triangle is a right angle
 along with the length of side c we have enough to solve the triangle.
since the triangle is cba not cbd we need to multiply d*2 to get a.
now there is a nice formula for solving for a missing length of a triangle.
 A^2 + B^2 = C^2 //where C is the hypotenuse.
so to find side b you can use the following formula.
b = sqrt(c*c - 4*d*d);

to get the angle you can use
arcsin((2*d)/c);

You really need to learn Trig, it's not hard but is very rewarding for this kind of stuff.

[edit] sorry didn't see how old the thread was. I am a little tired right now.
Logged


Imagine the impossible, then make it happen.
Seismosaur
ez::esS::AI::Entity::Brain
C++ Freak
***
Posts: 462



View Profile
« Reply #6 on: January 24, 2009, 05:38:40 PM »

Actually, shouldn't he just have to find where one point on each end of the vehicle is, find the difference in height between the two to get eh slope of the line created, then use that information to build a right triangle on the two points and then find the angle?

Sort of like:

Logged

*brammie*
my rank:
C++ Freak
***
Posts: 310



View Profile WWW
« Reply #7 on: January 25, 2009, 12:23:55 PM »

that's the point..
the vehicle is rotated because of the slope you are going to calculate..
Logged
Seismosaur
ez::esS::AI::Entity::Brain
C++ Freak
***
Posts: 462



View Profile
« Reply #8 on: January 25, 2009, 01:51:18 PM »

So then what's the problem?

All three sides of the triangle are known.
Logged

FrozenKnight
Extends without bound
Global Moderator
Dr. of C++ology
*****
Posts: 556


Do it yourself it's the only way to learn.


View Profile
« Reply #9 on: February 23, 2009, 07:32:10 AM »

LOL, it's not as if all the sides are known, however we do have enough to solve the triangle.

As long as you have 3 values (and the 3 values are not all angles) you can find all values of a triangle. First you need to figure out what type of tangle you have. in this case it's a right triangle so sin cos and tan all work here. now for the tricky trig stuff.

sin p = <opposite side (in this case height or the y value)>/<hypotenuse (in this case the length your cart is traveling)>
cos p = <adjacent side>/<hypotenuse>
tan p = <opposite side>/<adjacent side>

from here it's just simple algebra.
to find the height. we can take sin p and work from there
sin p = <opposite side>/<hypotenuse>
so
<hypotenuse> * sin p = <opposite side>

so in his case
a = sin p * c
b = cos p * c

now that I have explained the basics of trig those of you out there who are geniuses should be able to find all the trig identities. (i know it's possible because with this simple information i was able to do just that.) of course this was after taking pre-calc and i did it just to answer a few questions.
ad for figuring out your carts rotation i suggest you do that before your translation. makes things easier.
Logged


Imagine the impossible, then make it happen.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!