move 3d object w mouse

Blitz3D Forums/Blitz3D Programming/move 3d object w mouse

ryan scott(Posted 2004) [#1]
i can't seem to figure out how to start this.

i have a level editor that lets me pick a game object, a wall segment, or gate or something. that works.

i'd like to be able to move the mouse and have the object follow it. my camera could be facing any direction, so how can i move the object to the users left if the mouse moves left?

i'd even be happy if i could have the user press left arrow to have something move left, no matter which way he's facing the object. right now if you turn around and press left it'll just do something like x=x-1, which is different looking depending on which side of the object the camera is on.

objects and cameras can be turned any which way.

anyone know how i'd go about doing this?


sswift(Posted 2004) [#2]
If you want to move an object along the ground, create a plane, and make it pickable, then use camerapick to pick the point under the mouse, The resulting pick location is the point where the ray from the mouse intersects the plane in the world, and that is where you should place yout object if the mouse has clicked on the object and the mouse is being held down.

If however you don't want to just move objects around on the "floor" but instead want to move them around other planes you'll have to build a quad mesh for that.

Alternatively, do this:

Dx#, Dy#, Dz# is the distance you desire to move the object on each axis of the camera. ie, Dy# = 1 means move the object one unit "up" realtive to the camera's face direction.

TFormVector Dx#, Dy#, Dz#, Camera, 0
Dx# = TFromedX()
Dy# = TFromedY()
Dz# = TFromedZ()

Dx#, Dy#, and Dz# are are now in global space, and if you move your object like so, it will move in the desired direction the desired amount.

TranslateEntity Entity, Dx#, Dy#, Dz#, 0


jhocking(Posted 2004) [#3]
I wrote this code based on a function from the code archives:

; -----------------------------------------------------------------------------
; Positions an entity in 3D from given 2D coordinates
Function PositionEntityFrom2D(usecam,entity,x2d#,y2d#,height#=0,camZoom#=1)
gw=GraphicsWidth()
gh=GraphicsHeight()
x#=-((gw/2)-x2d)
y#=(gh/2)-y2d
parent=GetParent(entity)
EntityParent entity,usecam
z#=Abs(EntityZ(entity))
div#=(gw/(2/camzoom))/z
x=x/div
y=y/div

;z distance determined by a straight line from camera, through xyz coordinates, to height
TFormPoint x,y,z,usecam,0
y3d#=height
x3d#=TFormedX()
z3d#=TFormedZ()
EntityParent entity,parent
PositionEntity entity,x3d,y3d,z3d
End Function
;-----------------------------------------------------------------------------


The code from the archives moved 3D objects on a plane parallel to the camera but I wanted to move the objects around on the ground plane. The parameters are pretty simple: usecam is the camera, entity is the object being moved, x2d is the mouse's x position, y2d is the mouse's y position, and the optional parameters are height to define the plane to move on and camZoom is the camera's zoom setting.


ryan scott(Posted 2004) [#4]
thanks, this is great info!