3d Slope Maths
Blitz3D Forums/Blitz3D Programming/3d Slope Maths
| ||
----------------------- terrain height = 0 O | O | | y1 y2 Not too sure if the above makes sense, but.. I've got this car which is facing to the right, the back wheel is at height y1 - the front wheel y2 As you can see its on a downwards slope, at the mo both points are below 0 on the terrain. y1= -0.3465 y2= -0.4562 I'd like to work out the angle that I've got to rotate the car downwards. (x Axis) at the mo the car body is fixed to the back wheel - so I need to rotate the body to the angle of the slope. I think I've got to use the Atan2 command, but I'm having problems with the result since its in the negative. (and counting down not up !) Please Help !!!. |
| ||
You should be able to use simple trig. Tan angle = opp/ adj. opp = the difference in height between y1 and y2 adj = the difference in x posistion between y1 and y2 so i think angle = atan(opp/adj) Been a while since i've done this stuff but just search for trig on google and you'll get it. |
| ||
atan2 (dy,dx) |
| ||
** how about the don't get dirty method ** Put a pivot at each point. pointEntity p1,p2 ang#=entitypitch(p1) |
| ||
Can we just broaden this discussion, as I've spent all day tearing my hair out over this. a) I implemented Mark's 'Driver' demo code, but with over 50 cars it slowed down to 1 fps or less, so scratch that. b) Each of my cars has a main pivot (with body, camera, sounds connected etc), then I added a front and back pivot to the main pivot. I wanted to linepick down (to a 'road' mesh) from each to calculate the necessary 'slope' angle, but found that linepicking down returned integers, which were no use. Are you all saying that such pivots should (not follow a master but) align to the terrain/mesh, then calculate the angle? |
| ||
John, Line pick is also a slow command. Its probably okay for one or a few objects but 50! hmm. Phone me on Thursday I have an cool idea which will defiantely help! ;) IPete2. |
| ||
LinePick can return floats; I do it all the time. You probably are doing things wrong in your code (my guess would be calling PickedX etc. without the pound sign.) |
| ||
My guess is he used a = PickedX() instead of a# = PickedX() |
| ||
Thanks Pete. Can I share it with everyone? (All I want is a simple method of aligning cars to a mild slope, so they don't stick out the front, and I suspect this is what PaulJG is describing). Anyway Pete, what are you doing online at this time? The missus'll kill you! |
| ||
http://www.blitzbasic.com/codearcs/codearcs.php?code=67 is what I use. It works fine on terrains, and/or custom terrains if you have a function which will return the height of the terrain at any point. I guess it could be used fine with linepicks also (although on my old machine linepicks are very slow). |
| ||
John: I'm using linepick to position the pivots above the terrain. (main back wheel pivot, front pivot, finally the body shell - all coming off the main back pivot) So yes.. its exactly the same problem as you, just gotta get the body shell rotated down the angle of the slope. (havent tried the above ideas yet) I had a shed load of problems with the child objects, alot of the commands will take the local coords of the child object instead of the global. ( Cheers Malice for pointing that out in the other thread!. :) ) |
| ||
Ok. So I've spoken to IPete2 and he basically uses Mark's code for one test car, drives the car, and saves all positions and orientations to a file. Like recording, then playback. Thanks Pete, but not quite what I'm looking for. Dreamora, I take your point about linepick floats, but it seems that they are too slow anyway. I tried to match the pitch of my pivot to the angle towards to the next waypoint, but unless the waypoint is at the exact height of the pivot then as the car approaches it dips its nose down (or up). PaulJH, I think you've again summed up the problem succinctly. Any luck yet? |
| ||
Yeap.. sorted ! :) 3 pivot points + the main body shell (Ya need the extra third pivot to counter the angle in all directions) Here's my external functions if it helps: Function fixentity_height(ent,ofx,ofy,ofz) Pick=LinePick(EntityX#(ent,1),EntityY#(ent,1)+20,EntityZ#(ent,1),0,-30,0) offset#=-(EntityY#(ent,1)-PickedY#()) MoveEntity ent,ofx,offset#+ofy,ofz End Function Function fixentity_rotation_height(ob1,ob2_f,ob3_s,ob4) fixentity_height(ob1,0,0,0) fixentity_height(ob2_f,0,0,0) fixentity_height(ob3_s,0,0,0) yo3#=ATan2 ( EntityY#(ob1)-EntityY#(ob2_f,1),0.4) yo4#=ATan2 ( EntityY#(ob1)-EntityY#(ob3_s,1),0.2) RotateEntity ob4,yo3#,0,-yo4# End Function Eventually I'll do away with the line picks, I know the Y height of quad patches - so I'll find out the vertexes of the nearest quad to the car, find out the difference between the height of the 2 points.. do a calculation to find the car position: finally times the remainder (how far the car has travelled across a quad) by the height difference per width unit. Should work... I think ! |
| ||
Back to the Driver demo: but with over 50 cars it slowed down to 1 fps or less Hmm! Quad2 was based on a rewritten version of this code and it ran fine with 128 cars on a modern computer. It depends on what spec machine youre using..? |
| ||
@Damien: "it ran fine with 128 cars on a modern computer" Spec is: Pentium 4 1.6G, GeForce4 Ti4200. I understand what you're saying about newer machines, but if linepick is _that_ critical.... @PaulJG: Elegant code! I'm busy in the middle of rewriting multi-resolution rebooting at the moment. I'll implement your code and test it as soon as I can. Any apparent slowdowns? How many cars are you using? |
| ||
John: Not cars - tanks !, so I dont really have a speed limit problem as you would with a shed load of cars. Still, at the moment my tanks are quite happely travelling up and down the lumps and bumps in the terrain. btw.. the '0' in the "RotateEntity ob4,yo3#,0,-yo4#" is where you'd put the objects current yaw. (didnt add that bit till later) |
| ||
Thanks Paul, as I said at the moment I'm implementing user-choice of screen-res, which means that I now have to go back and rejig all display coordinates for 2D objects. I'll get back to cars as soon as I can. Please keep posting if you make any changes, or finding any new angles (-hah!) |