rotating two pieces to face each other
Blitz3D Forums/Blitz3D Programming/rotating two pieces to face each other
| ||
ok, here's a good puzzle for you... I have two lego pieces . (shown below). I need them to snap together. ![]() The red pieces snap together (the red dot is the parent of each piece, and the cube is the child). So far I have been successful in getting piece a to interpolate to piece b. The problem is that once the red dots have snapped together, I need the rest of the cube to be rotated correctly, so that it's a "mirror" of cube b. This means that the blue dots have to line up also. My code is going to take up a lot of space and may not be neccessary to read, but I'll post it anyway: ;align the centerpivot with the center the user has selected align(p\centerpivot,p\center,0) EntityParent p\centerpivot,0 EntityParent p\mesh,p\centerpivot EntityType(p\centerpivot,0);set collision to zero for warp ;destcenter is destination center. Local ax#=EntityX(destcenter,True) Local ay#=EntityY(destcenter,True) Local az#=EntityZ(destcenter,True) axx#=EntityPitch(destcenter,True) ayy#=EntityYaw(destcenter,True) azz#=EntityRoll(destcenter,True) ;interpolate the cube piece into its position ; If ax > EntityX(p\centerpivot) Then difx = 1 Else difx = -1 End If ; If ay > EntityY(p\centerpivot) Then dify = 1 Else dify = -1 End If ; If az > EntityZ(p\centerpivot) Then difz = 1 Else difz = -1 End If ;get the rawdistance between the points distx# = (ax-EntityX(p\centerpivot)) disty# = (ay-EntityY(p\centerpivot)) distz# = (az-EntityZ(p\centerpivot)) ;get the rawrotation between the points Local rotix# = (axx-EntityPitch(p\centerpivot)) Local rotiy# = (ayy-EntityYaw(p\centerpivot)) Local rotiz# = (azz-EntityRoll(p\centerpivot)) ;divide up the distance to be traveled into increments on each axis. Local timeslice =25 incx# = distx/timeslice incy# = disty/timeslice incz# = distz/timeslice incix# = rotix/timeslice inciy# = rotiy/timeslice inciz# = rotiz/timeslice Local counter=0 While counter < timeslice PositionEntity p\centerpivot, EntityX(p\centerpivot)+incx*1, EntityY(p\centerpivot)+incy*1,EntityZ(p\centerpivot)+incz*1, True RotateEntity p\centerpivot, EntityPitch(p\centerpivot)+incix,EntityYaw(p\centerpivot)+inciy, EntityRoll(p\centerpivot)+inciz, True RenderWorld Flip counter = counter + 1 Wend RotateEntity p\centerpivot,axx,ayy,azz,True ;at this point, the red dots are perfectly aligned "on top" of each other ;now here's the problem...how do I get the blue dots to align? |
| ||
I don't know if I've really got what your question was, anyway, did you try with EntityParent command ? If you parent the blue dots to the red one, before to make any rotation to it, they should then rotate accordinlgy, as you rotate the red dot one. I mean: EntityParent blue_dot, red_dot TurnEntity red_dot ;now the blue_dot entity turns like the red one. Wait a moment; I guess I've got it, you need then to rotate a cube until the cube A blue dot is in front of the cube B blue dot, right ? So, you may add a sphere_A to the cube_A blue_dot entity, at the blue_dot position, and parent to it; make the same with the cube_B. Once you have the two cubes aligned on the red dot, and (assuming that the z axis is the axis which passes through the red dot) turn one cube around the z axis , 90 degrees each step, and repeat it until sphere_A collides with sphere_B. At this point, the blue dot of cube_A should be aligned with the blue dot of cube_B. Now remove the shperes and you're done. Hope it helps, Sergio. |
| ||
Sergio Once you have the two cubes aligned on the red dot, and (assuming that the z axis is the axis which passes through the red dot) turn one cube around the z axis , 90 degrees each step, and repeat it until sphere_A collides with sphere_B. I tried that yesterday actually (funny how we think alike)....the problem is that when the spheres collide, it's before the pieces are perfectly mirrored, no matter how small the spheres are. Also, I found that it's possible for the two spheres to collide when the centers are aligned, but PERPENDICULAR to the angle I need. I tried to do a check to see if meshes were intersecting in that situation, but still I'm not getting good results. I don't know if this is a good idea after all. I'm looking into other ways to doing it. You see, my puzzle is spinning around in world space so the rotations are always different with every snap. And some pieces will require to rotate on other axes other than z, depending on which two nodes the user has selected to 'SNAP' together. It's a complicated project, much more complicated than it appears to be . |
| ||
I have an idea. the problem is that when the spheres collide, it's before the pieces are perfectly mirrored, no matter how small the spheres are. First rotate the cube 90 degrees. Then, check for the shpere collision. If the shpere don't collide, then rotate the cube again 90 degrees, and once the cube has been completely rotated, then check again for sphere collision. Theoretically it should work... Sergio. |