Select and move grid snapped obj problem

Blitz3D Forums/Blitz3D Programming/Select and move grid snapped obj problem

Hi all,

I hope someone can help me, been scratching my head and want to share the problem :)

Ok, what I'm trying to do is select and object (single click), once selected, mouse movement moves the obj around on its x/z axis but movement is snapped to a grid. This pretty much works.

When you select the object, I want the obj to stay still until you move the mouse to move it, however when in certain positions (because of the snap, I think) the obj 'jumps' initially, but not always.

The code below is set up so from the off you just point at the face of the white box nearest to the cam and click the mouse, and it shows what I mean, the object moves but I don't want it to. If you move around the obj to somewhere else and select it, sometimes it works fine (ie. doesn't move on first click).

I reckon I'm missing something obvious with the snapping! Ideas welcome :)

Ps. the code below is a cut down, thrown together to get the point across. The freelook function is from the forums, not my code but thanks to the author!

Graphics3D 800,600,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

;Camera
Global Cam = CreateCamera()
CameraClsColor Cam, 0, 0, 255
CameraFogMode Cam, True
CameraFogColor Cam, 0, 0, 255

;Light
RotateEntity CreateLight(), 45, 45, 0

Global Cube = CreateCube()
ScaleEntity Cube,2,0.25,2
PositionEntity Cube,0,0,10
EntityPickMode Cube,2

Cube2 = CreateCube()
PositionEntity Cube2,0,0,10
EntityColor Cube2,0,255,16
ScaleEntity Cube2,0.25,2,0.25

PositionEntity Cam,10,-1.6,9.3
PointEntity Cam,Cube

Global held = False

Global picked_ent

Global snapx = 2
Global snapy = 2
Global snapz = 2

Global held_count = 0

MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
FlushMouse()
While Not KeyHit(1)
	
	FreeLook(Cam, .1)
	
	old_held = held
	
	If MouseHit(1) = True Then
		CameraPick(Cam,GraphicsWidth()/2,GraphicsHeight()/2)
		picked_ent = PickedEntity()
		If picked_ent > 0 Then
			held = Not held
			If held = True Then
				PointEntity Cam,picked_ent
			End If
		End If
	End If
	
	If held = False Then
		MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2		
	End If
	
	If held = True Then
		picked = CameraPick(Cam,GraphicsWidth()/2,GraphicsHeight()/2)
		If picked > 0 Then
			px# = PickedX#() : py# = PickedY#() : pz# = PickedZ#()
			pxs = (Int(px#)/snapx)*snapx : pzs = (Int(pz#)/snapz)*snapz
			PositionEntity picked_ent, pxs, 0, pzs
		End If
	End If
	
	; Reset
	If KeyDown(19) = True Then
		PositionEntity Cube,0,0,10
		held = False
	End If
	
	RenderWorld
	
	Text 0,40,"PickedX: "+PickedX#() 
	Text 0,60,"PickedY: "+PickedY#() 
	Text 0,80,"PickedZ: "+PickedZ#() 
	Text 0,100,"PickedNX: "+PickedNX#() 
	Text 0,120,"PickedNY: "+PickedNY#() 
	Text 0,140,"PickedNZ: "+PickedNZ#() 
	Text 0,160,"PickedTime: "+PickedTime#() 
	Text 0,180,"PickedEntity: "+PickedEntity() 
	Text 0,200,"PickedSurface: "+PickedSurface() 
	Text 0,220,"PickedTriangle: "+PickedTriangle() 
	Text 0,240,"picked_ent: " + picked_ent
	Text 0,250,"camx#: " + EntityX#(Cam)
	Text 0,260,"camx#: " + EntityY#(Cam)
	Text 0,270,"camx#: " + EntityZ#(Cam)
	
	Flip
Wend
End

Global FreeLookXS#, FreeLookZS#, FreeLookRotXS#, FreeLookRotYS#
Function FreeLook(camera, sp# = .1)
	If sp# > 0 Then
		FreeLookXS# = (FreeLookXS# + ((KeyDown(32) Or KeyDown(205)) - (KeyDown(30) Or KeyDown(203))) * sp#) * .75
		FreeLookZS# = (FreeLookZS# + ((KeyDown(17) Or KeyDown(200)) - (KeyDown(31) Or KeyDown(208))) * sp#) * .75
		MoveEntity camera, FreeLookXS#, 0, FreeLookZS#
	EndIf
	FreeLookRotXS# = ((MouseXSpeed() - FreeLookRotXS#) * .2 + FreeLookRotXS#) * .9
	FreeLookRotYS# = ((MouseYSpeed() - FreeLookRotYS#) * .2 + FreeLookRotYS#) * .9
	If EntityPitch(camera) + FreeLookRotYS# < -89 pitch# = -89 ElseIf EntityPitch(camera) + FreeLookRotYS# > 89 pitch# = 89 Else pitch# = EntityPitch(camera) + FreeLookRotYS#
			yaw# = -FreeLookRotXS# + EntityYaw(camera)
			RotateEntity camera, pitch#, yaw#, 0
			MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
End Function


Cheers,
LW.

hi

i was looking other thing in the code archives and see that

http://www.blitzbasic.com/codearcs/codearcs.php?code=1923

probably related to your ask
if not (i'm sorry)

Juan

Ah cool - thank you.

Cheers,
LW.