Moving an object relative to the camera

Blitz3D Forums/Blitz3D Beginners Area/Moving an object relative to the camera

hockings(Posted 2006) [#1]
My latest project has a small square map (a flat 3d mesh) that can be rotated. What I would like to do is now be able to allow the user to change the view from side on to top view (and everything inbetween). In theory all I have to do is rotate around the X axis.

This works fine. I hit a problem though when I've rotated the map (rotated around the Y axis). If I've turned the square 45 degrees around Y then try to rotate it around X, I expect to see the corner of the square that's now facing me go straight up and over. Instead it rotates based on the original x axis and not what I'm now looking at (ie, it seems to rotate from right over to left).

Does anyone have any ideas on how to have it rotate how I'd like it to? (ie. relative to how it's now seen from the camera?)


Matty(Posted 2006) [#2]
Simple method:


function RotateMapAboutXAxisRelativeToCamera(EntityHandle,AmountToRotateBy#,CameraHandle)

pivot=createpivot()
positionentity pivot,entityx(EntityHandle),entityy(EntityHandle),entityz(EntityHandle)
rotateentity pivot,entitypitch(camerahandle),entityyaw(camerahandle),entityroll(camerahandle)
entityparent EntityHandle,pivot ;set the pivot as the parent of the entity
turnentity pivot,AmountToRotateBy,0,0
freeentity pivot ;free the pivot as no longer need it

end function




then when the user presses a button do something like this:


if keydown(ButtonScanCodeForRotatingForwards)<>0 then RotateMapAboutXAxisRelativeToCamera(MapEntity,1.0,Camera)

if keydown(ButtonScanCodeForRotatingBackwards)<>0 then RotateMapAboutXAxisRelativeToCamera(MapEntity,-1.0,Camera)
 




Sir Gak(Posted 2006) [#3]
FreeEntity EntityHandle

Description
FreeEntity will free up the internal resources associated with a particular entity and remove it from the scene.

This command will also free all children entities parented to the entity.


If, as you suggest, you child the EntityHandle of your entity to the pivot as a parent, when you will free up your pivot, you will also free up the entity that you made a child to the pivot, as per the above descriptionf from the B3D Help.


hockings(Posted 2006) [#4]
Thanks guys, I'll give that a go now!!!


hockings(Posted 2006) [#5]
Thanks SirGak! That's exactly the problem I just found when trying out the code. I'm going to try changing it to "PointEntity" instead and see if that works.


PointEntity entity,target[,roll#]
Parameters
entity - entity handle
target - target entity handle
roll# (optional) - roll angle of entity

Description
Points one entity at another.

If you wish for an entity to point at a certain position rather than another entity, simply create a pivot entity at your desired position, point the entity at this and then free the pivot.




hockings(Posted 2006) [#6]
PointEntity loses the rotation I've put on the object. Any ideas?


hockings(Posted 2006) [#7]
Ahhh - figured it out, I don't need to worry about pivots and PointEntity - all I have to do is use turnentity with the "global" parameter set to 1. I would've figured it out earlier if that had been documented - oh well, thanks all for your help!!!!


Matty(Posted 2006) [#8]
Oops - you are right Sir Gak, I'd forgotten about that (haven't used blitz3d in a while) - you would also need to set the entityparent back to '0' I guess and include a entityparent EntityHandle,0 statement before freeing the pivot.


hockings(Posted 2006) [#9]
Cheers Matty!