   Align car to simple 45° slope?
  I figure it will be dead easy to align a car to a slope, that always slopes up at 45 degrees, and faces exactly North, South, East, or West (0, 90, 180, and 270 degrees)But I don't know how to yet, whilst still retaining the direction the vehicle is already driving in   Can anyone come up with a simple solution for that?My problem is that quaternions, matrixes, linear and bilinear interpolation, are way way over my head at the moment.So I thought I could make it VERY easy for myself by restricting my terrain... I didn't bother explaining everything here, just the part on slopes.(The software I'm using - Jamagic from www.clickteam.com, uses a &#106avascript based language and provides functions for positioning 3D objects - GetXAngle, GetYAngle, GetZAngle, and SetAngle(x,y,z), all work in radians    .The other functions provided are a set of "turn" and "roll" functions (I think they work in radians too), that are relative to the object, so if you roll right 90 degrees, then turn left 90 degrees, you'd be facing the ground.Any solutions that work using those functions, would be a great bonus for me.)[edited by - Domarius on January 27, 2004 10:40:46 PM][edited by - Domarius on January 27, 2004 10:42:05 PM]
  SlopeAngle = 45.0f / 180.0f * Pi;SetXAngle(SlopeAngle)?that would be a start ;  think about what you want to do Domarius. You have two rotations. The one that rotates the car to its direction, and one that aligns it to the slope. At the beginning you have a car parallel, lets say to the x axis (z is height). You also know its direction (z angle). The first thing you have do is align it to the surface, i think you know many easy ways for this. from this process you get two angles: x angle and y angle (the alignment must not alter z anlge). You final step is to draw your car with the just calculated x,y and z angles. But dont forget: these rotations must always be object-relative (rotations about the object''s x,y and z axes).   "Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin ;  I had a similar problem. I had a model that I imported and I wanted it to turn around when I pressed the relevant keys.Here's the code: it's in JavaI'm using OpenGL, so I designed this around using glRotatef().//gets the minimum number of directions the character has to pass to get to it's location e.g N to NE is one tick//hpos : where the character was facing, East, North, etc..//cpod : where you want the character to faceprivate final String[] AXIS = {"north","northeast","east","southeast","south","southwest","west","northwest"};private String[] getNumberOfTicks(String hpos, String cPos){int startPos = 0;int side1 = 0;int side2 = 0;String[] values = new String[2];for(int i = 0; i < AXIS.length; i++){if( AXIS.equals(hpos) )startPos = i;}for(int i = startPos+1; true; i++){if( i == AXIS.length )i = 0;side1++;if( AXIS.equals(cPos) )<br>break;<br><br>}<br><br>for(int i = startPos-1; true; i–)<br>{<br>if( i == -1 )<br>i = AXIS.length-1;<br><br>side2++;<br><br>if( AXIS.equals(cPos) )<br>break;<br>}<br><br>if( side1 > side2 )<br>{<br>values[0] = side2+"";<br>values[1] = "+";<br>}<br>else if( side1 < side2 )<br>{<br>values[0] = side1+"";<br>values[1] = "-";<br>}<br>else<br>{<br>values[0] = side1+"";<br>values[1] = "+";<br>}<br><br>return values;<br>}<br><br>//turns the model<br>//history : global variable to keep track of where the character is facing<br>//posistion : global variable of the where the chracter wants to be facing<br>//completed : &#111;nly calculate the next rotation if the previous has finished<br><br>private String[] tick = new String[2];<br>privat float rotateY = 0.0f;<br>private float stopAmount = -1.0f<br>private float speed = 15.0f;<br>//how long it takes to do &#111;ne tick<br>private final float TICKAMOUNT = 45.0f;<br><br>private void rotate()<br>{<br>if( completed == true )<br>{<br>if( history.equals(position) )<br>rotateY+=0.0f;<br>else<br>{<br>tick = getNumberOfTicks(history, position);<br><br>stopAmount = TICKAMOUNT*Integer.parseInt(tick[0]);<br><br>}<br><br>history = position;<br>}<br><br>if( stopAmount > 0 )<br>{<br>if( tick[1].equals("+") )<br>rotateY+=speed;<br>else<br>rotateY-=speed;<br><br>stopAmount-=speed;<br><br>completed = false;<br><br>}<br>else<br>completed = true;<br><br><br>}<br></code><br><br>Then when you rotate your object you do somthing like this<br><br><code><br>public render()<br>{<br>  GL.glLoadIdentity();<br>  <br>  GL.glRotatef(rotateY, 0.0f, 1.0f, 0.0f);<br>    <br>  //my 3D model<br>  model.draw();<br><br>  rotate();<br>}<br></code>    </i><br><br><SPAN CLASS=editedby>[edited by - Mr_Ridd &#111;n January 29, 2004 1:33:11 AM]</SPAN>   <br><br><SPAN CLASS=editedby>[edited by - Mr_Ridd on January 29, 2004 1:34:00 AM]</SPAN>;  Thanks everyone, for the clear and simple replies, especially Mr_Ridd for the code!  This is going to be fun.(Excuse my lack of mathematical knowledge; I left highschool 2 years early, and am learning what I need to know for games off the net) ;  Okay, I have no problem setting the X angle of the object to match the slope.  Jamagic object angles are all relative to the object.My real problem is how to retain the direction the car is driving in, so you can drive up the slope at an angle if you want.My original idea was to store the direction the player is facing (the Y angle), and then apply the slope alignment, and restore the direction, eg: - yAngle = playerObject.GetYAngle() - Face PlayerObject in the direction of the slope (n,s,e or w) - Set playerObject X angle to match angle of the slope - Then set playerObject Y angle to yAngle I originally tried storing the Y angle, but found that when I restored it it wasn't always correct. Mostly if I turned more than 180 before restoring. As I recently learned in the thread http://www.clickteam.com/CTforum/showflat.php3?Cat=&Board=jamagic&Number=225037&page=0&view=collapsed&sb=5&o=&part=all at www.clickteam.com, I have to restore ALL the angles as they were (don't know if this is inherent with rotating objects in 3D using Radians, or a quirk of Jamagic), which means I would then lose the slope alignment.   [edited by - Domarius on January 29, 2004 6:45:15 AM];  look like a typical Pitch-Yaw-Roll problemfirst, reset all angles to (0, 0, 0)set the Yangle to the car steering direction. That will be like steering the car on a flat plane.then, it''s a combination of roll and pitch, to set the car with the slope alignement. for example, if the car is perpendicular to the slope, after you steered it, then the roll (Xangle) will be the slope angle. If the car if going up the slope, then the pitch (Xangle) is the slope angle. if the car is at an angle on the slope, then the pitch (Xangle) and the roll (Zangle) are at an angle. The problem is, how much the slope and the current direction of the car influence both.;  oliii, that sounds EXACTLY like what I need.So, if the problem is how much to affect the Roll and the Pitch, depending on the Yaw of the car, how do I work that out?Maybe see the difference between facing the slope and being perpendicular to it (a turn of one quadrant) as a percentage, and that will tell me how much percent of pitch to transfer to roll? ;  Something like that. it's a combination of the direction of the car, and the direction of the slope. Say, you have a normal at the slope, your percentage could beVector cardir  (cos(yaw), 0.0f, sin(yaw));Vector slopedir(slopenormal.x, 0.0f, slopenormal.z);slopedir.Normalise();float percentage = fabs(cardir.x * slopedir.x + cardir.z * slopedir.z);pitch = percentage * slope_angle;roll  = slope_angle - pitch;note that, I have no clue if this would work at all But frankly, if you had matrices, it would be done in two minutes.   [edited by - oliii on February 3, 2004 4:23:33 AM];  Cool, thanks for your help oiii.Well maybe I will just get into matrices enough to just do what I want to do without understanding how it works.Can you describe "how to align car to slope" breifly?  The language I'm using has a "matrix" object I can create, using 4 vectors, or a 3x4 array to 'get the values from'... (no idea what it's all about here).Then there are heaps of cool methods it has;Set,Reset,SetIdentity,GetValue,SetValue,MakeRotationMatrix,MakeScaleMatrix,MakeTranslationMatrix,AddMatrix,SubMatrix,MulMatrix,Invert,Normalize,Transpose,AddAngle,GetEulerAngleX,GetEulerAngleY,GetEulerAngleZI see it has a Normalise method, and you mentioned that before, maybe it's appropriate.All I know is that the normal of a polygon is the direction the flat surface of it is 'facing'.  I'm guessing it's what I want.   [edited by - Domarius on February 6, 2004 7:31:07 PM] [edited by - Domarius on February 6, 2004 7:45:47 PM]