Need some math help
Blitz3D Forums/Blitz3D Programming/Need some math help
| ||
I should be able to do this but I just can't get it together. Lets say I have a movement allowance of 30 points. For each 4 points I move in the Z diraction I will move 1 point in the X direction.Ok - so I can get the angle from the starting point. But - how do I figure where I end up on the Z axis and where will I end up on the x axis.Must be an easy formula for this. Appreciate any help. |
| ||
You have a 'radius' of 30 units and an angle a where Tan a = 1/4 z = 30 * Cos(1/4) ; generally z = radius * Cos(angle) x = 30 * Sin (1/4) ; generally x = radius * Sin(angle) {unless I have my axes swapped :-P } I assume you should be using TranslateEntity with these z and x values each turn, as MoveEntity would depend on which way you are facing and PositionEntity would always place it 30 units from the origin... not updating each turn |
| ||
Thanks Blackjumper but I don't want to actually move the item.That I can do okay. I just want to know where it would finish if I did move it. There are to many items move to sctually move to find out where the end point would be. Hope this makes sense. |
| ||
For each total movement of 5 (i.e. 4+1 ) the Z direction gets 4 and X gets 1. So Z gets 4/5 of the total and X gets 1/5. |
| ||
Find the vector easy: (4,1) Find the magnitude of that vector M = Sqr(x*x)+(z*z) M = 4.47213 Divide the vector by the magnitude X = 4 / M Z = 1 / M X = 0.89442 Z = 0.22360 The normalized vector is (.89442,.22360) So now say you want to move a total of 30 spaces in this direction? Simply use 30 as the magnitude and multiply by the normalized vector. NewX = NX * M = .89442 * 30 NewZ = NZ * M = .22360 * 30 your new position will be:(26.8326,6.708) And walla! Now if you want to move based on an angle, then you must convert the angle into a vector: Take your angle above (which is 26.5650 degrees) NVX = cos(A) NVZ = Sin(A) This will give you a normalized vector based on the angle (the same vector you have above). To move 30 spaces, use it as the magnitude in the formula above. So, if I am cathing your drift here, you want to move 30 spaces in an artibrary direction? What direction, is it an angle? if so, then plug your angle in as A and your 30 spaces in as M into the following for your new position: NewX = Cos(A) * M NewZ = Sin(A) * M Want to know the angle of movement you just went? Then plug in your X and Z values in below (IE you moved X units accross and Z units forward): M = Sqr(x*x)+(z*z) Angle = ACos(X/M) And there you have the angle by which you moved. Am I off base here? |
| ||
Yeah, I read the question as taking 30 steps, some in the x direction and some the z direction. But I think the real question was how to move 30 units in a particular direction, indicated by x changing 4 for each change of 1 in z. For that I would consider what happens when x changes by 4 and z by 1. The distance traveled is then Sqr( 4*4 + 1*1 ) = Sqr(17). For each unit of distance the change in x is 4/Sqr(17) and the change in z is 1/Sqr(17). To get an arbitrary amount of travel, such as 30, just multiply these by the distance. Graphics 550, 250, 0, 2 x# = 4 z# = 1 dist# = Sqr( x*x + z*z ) dx# = x/dist dz# = z/dist ; For each unit of distance the x and z step sizes are dx and dz. ; Example, to travel 30 units the x and z changes are travel# = 30 xStep# = travel * dx zStep# = travel * dz Print Print "To travel while maintaining x,z in a ratio of 4 to 1 we move" Print dx + " in the x direction and " + dz + " in the z direction" Print "for each unit of distance." Print Print "So to move " + travel + " units we go " + xStep + " in x and " + zStep + " in z." WaitKey : End |
| ||
And walla! Isn't that Voila! ? |
| ||
Isn't that Voila! ? If I had a Germen accent I suppose it would be. |
| ||
Thanks much guys - just the formula I needed. |