Mouse Wheel Camera

Blitz3D Forums/Blitz3D Programming/Mouse Wheel Camera

Ok I am trying to make it so when I roll the mouse wheel forward it moves the camera forward and backward the camera goes backward. For some reason it dont work properly. First of all when the app loads and I try to move the camera nothing happens til after I have rotated the camera somewhere with the mouse (its set up to press and hold right click to pivot the mouse around). The second thing it does is if I roll the mouse wheel to go forward sometimes the camera starts going the wrong direction. Whats wrong with this. Here is the code I got.

mzs = mousezspeed()
if mzs = 1
moveentity camera,0,0,100
endif

Heres the code for the right mouse button deal.
if mousedown(2)
mxs=MouseXSpeed()
mys=MouseYSpeed()
dest_cam_yaw#=dest_cam_yaw#-mxs
dest_cam_pitch#=dest_cam_pitch#+mys
cam_yaw=cam_yaw+((dest_cam_yaw-cam_yaw)/5)
cam_pitch=cam_pitch+((dest_cam_pitch-cam_pitch)/5)
RotateEntity camera,cam_pitch#,cam_yaw#,0
MoveMouse Width/2,Height/2
endif

if mzs = 1

Should be:
if mzs > 0

Or better even (two-directional):
moveentity camera,0,0,mzs ;may want to divide by something

and mzs = mousezspeed()
should be mzs# = mousezspeed()
An integer won't do :D

uhmm that didn't work. Still same problem.

thats weird when I set it to move 10 units it seems to work fine but with 100 it messes up.

I did something like this. Hope it helps.

If MouseZSpeed()<>0 Then
speed#=(MouseZ()+MouseZSpeed())*.1
EndIf

;Graphics3D 640,480
Graphics3D 800,600

AutoMidHandle True
;SetBuffer FrontBuffer()
grid_tex=CreateTexture( 32,32,8 )
ScaleTexture grid_tex,10,10
SetBuffer TextureBuffer( grid_tex )
Color 0,0,64:Rect 0,0,32,32
Color 0,0,255:Rect 0,0,32,32,False
SetBuffer BackBuffer()

plane=CreateTerrain(128)
EntityTexture plane,grid_tex
PositionEntity plane,-64,0,0  

light1=CreateLight()
PositionEntity light1,-15,15,-15
light2=CreateLight(3)
LightConeAngles light2,5,30
LightColor light2,255,100,100
PositionEntity light2,100,15,100
;*********************************************************** 
;sets up the player stuff p_pivot= control point 
;player= a cube for the player mesh, p_cam=the players 
;camera attatched to the player control point.

p_pivot=CreatePivot()
player=CreateCube(p_pivot)
EntityColor player,200,0,0
ScaleEntity player,1,.5,1
p_cam=CreateCamera(p_pivot)
PositionEntity player,0,.55,0
PositionEntity p_pivot,0,0,4
PositionEntity p_cam,0,4,-10
gun=CreateCylinder(16,1,player)
RotateEntity gun,90,0,0
PositionEntity gun,0,0,1
ScaleEntity gun,.5,1,.5
;***********************************************************
Type buttons
	Field x
	Field y
	Field length
	Field height
	Field image
	Field mesage$
	
End Type

Global rotate.buttons = New buttons
;use this code To relocate the rotation graph down Or change rotatex\rotatey anywhere you want
;rotate\x = GraphicsWidth()/2
;rotate\y = GraphicsHeight()/2+(GraphicsHeight()/3)
rotate\x = GraphicsWidth()/2
rotate\y = GraphicsHeight()/2
rotate\length = 130
rotate\height = 130 
rotate\image = CreateImage(rotate\length,rotate\height)
rotate\mesage$ = "Ship Rotation"
SetBuffer ImageBuffer(rotate\image)
	Color 100,100,255
	Oval 0,0,rotate\length,rotate\height,0
	;message$
	Text 0,0,rotate\mesage$ 
	

Global player_point=CreateImage(20,20)
SetBuffer ImageBuffer(player_point)
	
	Color 0,0,250
	Line 10,1,1,20
	
	Color 0,0,250
	Line 10,1,20,20
	
    Color 200,0,0
	Line 1,19,20,19

;pointrotation data____________________________________________________________________________________
Dim point(359); dimention the array to accomidate the rotations.
For loop=0 To 359
    point(loop)=CopyImage(player_point)
	MidHandle point(loop)
	RotateImage point(loop),loop
	DebugLog loop
Next


Global angle#

;make the imiges to be used for the mouse ,mouseimage is the triangle but just a dot for collision
mouseimage=CreateImage(20,20)
mousepoint=CreateImage(2,2)
makemouse(mouseimage,mousepoint)


speed#=0
mode=1
m=1
ClsColor 100,100,100
CameraViewport p_cam,0,0,GraphicsWidth(),500
CameraClsColor p_cam,10,10,100
SetBuffer BackBuffer()

While Not KeyHit(1)
PointEntity light2,plane

p_angle#=-angle#
;If KeyHit(2) mode=mode+1
;If mode=4 Then 
	;mode=1
	;angle#=0
	;RotateEntity p_pivot,0,angle#+anglereset#,0 
;EndIf 


;If mode=1 Then
	;PositionEntity p_cam,0,4,-10
 	RotateEntity p_pivot,0,p_angle#,0
;EndIf 


;If mode=2 Then 
	;RotateEntity player,0,p_angle#,0
	;RotateEntity p_pivot,0,p_angle#,0
;EndIf

;If mode=3 Then 
	;PositionEntity p_cam,0,2,-.5
    ;RotateEntity player,0,p_angle#,0
	;RotateEntity p_pivot,0,p_angle#,0
;EndIf 
;***********************************************************
If MouseZSpeed()<>0 Then 
	speed#=(MouseZ()+MouseZSpeed())*.1
EndIf 

MoveEntity p_pivot,0,0,speed#
;***********************************************************
Cls 

If KeyHit(57)=True  m=m+1
If m=2 Then m=0
If m=1 Then 
 	;Color 255,0,110
	;Text 10,500,"angle"+angle
	;Text 10,530 ,speed#
	;Text 10,550,mode
	Color 200,200,100
	Text 10,510,"Place the mouse inside the rotation image to rotate cam and player."
	Text 10,520,"Hit 1 to change rotation modes. "+"Use the mousewheel to move."
	Text 10,530,"Hit space to clear these mesages and gain fps."
	;Print "hello world"
EndIf 


UpdateWorld()
RenderWorld()

drawhud(mouseimage,mousepoint)
If ImagesOverlap(mousepoint,MouseX(),MouseY(),rotate\image,rotate\x,rotate\y) Then 
	update_mouse()
EndIf

VWait:Flip 
Wend
End

Function drawhud(mouseimage,mousepoint)
	DrawImage rotate\image,rotate\x,rotate\y
	DrawImage mouseimage,MouseX(),MouseY()
	DrawImage mousepoint,MouseX(),MouseY()
	DrawImage point(angle),rotate\x,rotate\y
End Function

Function makemouse(mouseimage,mousepoint)
	SetBuffer ImageBuffer(mouseimage)
	Color 100,60,250
	Line 0,0,20,10
	Line 0,0,10,20
	Line 20,10,10,20
	
	SetBuffer ImageBuffer(mousepoint)
	Color 0,255,0
	Plot 0,0
	HandleImage mouseimage,0,0
	HandleImage mousepoint,0,0
End Function

Function update_mouse()
;This code is Thanks to Ross C 
;If MouseDown(2) Then
	angle=ATan2(rotate\y-MouseY(),rotate\x-MouseX()  ) ; the angle between 
	angle=angle-90 ; subtract 90 from the angle to compinsate for the blitz co-ord system
	If angle>359 Then ; check angle is within 0-359 range
		angle=angle-359 ; if not, wrap it round
	ElseIf angle<0 Then
		angle=angle+359
	End If
;EndIf

End Function



ok since it worked perfect with moveentity entity,0,0,10 I put that in a for loop and loop it depending on how fast I want it to move. works perfect lol. Just gotta impravize.

Did you try
 CameraZoom camera,{ZOOMFACTOR}
Where ZOOMFACTOR is a number up to 2 (I think.) It seems much simpler than moving the camera.

This is in your CODE section in Blitz.
; CreateZoom Example
; ------------------

Graphics3D 640,480
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,1,0

light=CreateLight()
RotateEntity light,90,0,0

ground=CreatePlane()
sand_tex=LoadTexture("media/sand.bmp")
ScaleTexture sand_tex,10,10
EntityTexture ground,sand_tex
EntityColor ground,168,133,55

cactus1=LoadMesh("media/CACTUS2.x")
cactus2=LoadMesh("media/CACTUS2.x")
camel=LoadMesh("media/camel.x")
PositionEntity cactus1,-1,2,10
PositionEntity cactus2,1,2,10
PositionEntity camel,0,1,1000

; Set initial zoom value
zoom#=1

While Not KeyDown( 1 )

If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0
If KeyDown( 203 )=True Then TurnEntity camera,0,1,0

; Change zoom value depending on key pressed
If KeyDown( 208 )=True Then zoom#=zoom#-0.1
If KeyDown( 200 )=True Then zoom#=zoom#+0.1
; ##################################  I ADDED THIS
If MouseZ() > 0 Then 
	zoom#=MouseZ()
     ;zoom# = zoom# + 0.25
EndIf
If MouseZ() < 0 Then
     ;zoom# = zoom# -0.25
	zoom#=MouseZ()
EndIf
anger#=MouseZ()
; ####################################################
; Put a minimum and maximum cap on zoom value
;If zoom#<1 Then zoom#=1
If zoom#>100 Then zoom#=100

; Set camera zoom
CameraZoom camera,zoom#

RenderWorld

Text 0,0,"Use left and right cursor keys to turn around"
Text 0,20,"Use up and down cursor keys to change camera zoom"
Text 0,40,"There is a camel on the horizon, inbetween the cacti. Zoom in to  see it."
Text 0,60,"CameraZoom camera,"+zoom#
Text 0,80,"MouseZ: "+anger#

Flip

Wend

End
I used MouseZ() instead of MouseZSpeed() so the Zoom would work correctly.
-RZ

Joey; Did my code not work for you?

well the zoom wont work becuz I want to just move the camera around the world. And River I havn't gotten to try it yet I been at school. But what part of your code actually moves the camera?

With my code you can move the camera around... If you wanted to use the mouse to turn the camera there are lots of tutorials out there. My point is "Don't reinvent the wheel!" If you want to move the camera around the world use the arrow keys or WADX like everyone else.
-RZ

I am using the mouse for camera control. And the idea I thought worked didn't so I have to figure out another way. Im not re-inventing the wheel just using a method of interface differently and IMO easier than using the arrow keys. Please explain your code RiverRatt. I will look it over when I get home but I couldn't find where you were actually moving the camera.

I did this for my vertical scrolling shooter, but cheated since I used a animated camera in b3d pipeline and the mouse wheel to move through the keyframes

hmm well I made a function that is broke somehow. Anyone got any Ideas?

code/
Function moveobject#(entity, speed)
yaw# = Entityyaw#(entity)
curpitch# = EntityPitch#(entity)
pitch# = 90 - Abs(curpitch#)
If Abs(yaw#) <= 90
beta_angle# = Abs(yaw#)
Endif
If Abs(yaw#) > 90
beta_angle# = 180 - Abs(yaw#)
Endif
If yaw# => 0 And yaw# <= 90
quadx = 1
quadz = 1
Endif
If yaw# => 90 And yaw# <= 180
quadx = -1
quadz = 1
Endif
If yaw# => 180 And yaw# <= 270
quadx = -1
quadz = -1
Endif
If yaw# => 270 And yaw# <= 360
quadx = 1
quadz = -1
Endif
If curpitch# => 0
quady = 1
Endif
If curpitch# < 0
quady = -1
Endif
x0 = entityx#(entity)
y0 = entityy#(entity)
z0 = entityz#(entity)
xz# = Sin#(pitch#) * speed
y1# = (cos#(pitch#) * speed) * quady
z1# = (sin#(beta_angle#) * xz#) * quadz
x1# = (cos#(beta_angle#) * xz#) * quadx

PositionEntity entity, x0-x1, y0-y1, z0-z1

End Function
code*/

Joey;In my code the camera is attatched to the box pivot called p_pivot (short for player pivot) in the line p_cam=CreateCamera(p_pivot).
Then its z position is updated right before the cls command.

If MouseZSpeed()<>0 Then
speed#=(MouseZ()+MouseZSpeed())*.1
EndIf

MoveEntity p_pivot,0,0,speed# ;HERE
cls

In this line
If MouseZSpeed()<>0 Then
Is the z mouse button being turned? MouseZSpeed() will return a value greater that 0 for forword movement and less than 0 for backward movement.
and then this line,
speed#=(MouseZ()+MouseZSpeed())*.1
sets the float variable speed# to how far the mouse z button has bin rotated.
MouseZ() the mouses initial starting position.
MouseZSpeed() can be + or - infinity. Now because leaving it speed#=(MouseZ()+MouseZSpeed()) (without the *.1) results in a moving to fast I multiply it by .1 so the camera will move only 1/10
the value of Mousezspeed().

ok I looked at ur code and its not exactly what I was looking for. I want the camera to move only when the wheel is being turned. I would like to control the camera like they do in TerraEd. This is really making me angry because I can't seem to get this and yet it seems so easy. Why would the camera only do this at higher speeds. And if I increment the speed it seems to not do this at all. Idk what to think/do.

Maybe you should rethink it a bit... use MouseZ() to control the SPEED of the movement then you simply have to call this:

If MouseZ() > 0 Then Movementz = MouseZ()
If MouseZ() < 0 then movementz = MouseZ()

You know... use it like an accelerator pedal.

But then again I really can't see what you are doing... It might be brilliant... Good luck.
-RZ

u know what. ill compile an exe and show you what I got so far. maybe you can help a lil better then.

Do you meen something more like this?
;grid based camera with mouse wheel.
;Todo list 
;first lock a plane to the mouse that has a grid 
	;call it local_plane
;second make another grid that the mousegrid overlay
	;call it global_plane
;Third make a raze and lower function that raises the grid you know like in maplet
;forth make a fuction to create all of blitzs primitives
; if I get this far I'll write fith,sixth ect...

Graphics3D 640,480 
SetBuffer BackBuffer() 
SeedRnd MilliSecs() 
lite=CreateLight()

point=CreateSprite()
EntityColor point,255,0,0
PositionEntity point,0,0,10

Global camera=CreateCamera(point)
PositionEntity camera,0,0,0
;RotateEntity camera,0,0,0

 
local_plane=CreateTerrain(16)
;Load your grid texture here
;grid=LoadTexture("data/Maper_grid.bmp")
;EntityTexture local_plane,grid
EntityColor local_plane,0,255,0
PositionEntity local_plane,0,-5,0






PointEntity camera,point



;***************************************************************************************************
;Start of Main loop*********************************************************************************
;***************************************************************************************************

While Not KeyDown( 1 )

 
PositionEntity point,EntityX(point),0,MouseZ()
SpriteViewMode point,3

;PositionEntity local_plane, EntityX(point),-5,EntityZ(point)

 

If KeyDown( 205 )=True Then TurnEntity point,0,-1,0 
If KeyDown( 203 )=True Then TurnEntity point,0,1,0 
If KeyDown( 208 )=True Then MoveEntity point,0,0,-0.05 
If KeyDown( 200 )=True Then MoveEntity point,0,0,0.05 

UpdateWorld()

RenderWorld 
Text 10,20,"camera position:"+(EntityX(point))+" "+EntityY(point)+" "+EntityZ(point)
 

Flip 

Wend 
;**************************************************************************************************
;End of Mainloop***********************************************************************************
;**************************************************************************************************
End  



Alright thats pretty close but my camera has to be able to rotate around and and idk how to explain it. Here is the link to what I have so far and you will notice the bug in the camera. Got any ideas how to fix this.


http://users.orofino-id.com/joey_gentry/mapbuilder.exe

It has a memory access violation on startup.

The code I posted was moving the sprite, I fixed it to move the camera.

Here then compile this. It does the same thing but with less pretties. Also how I post code on the forums.

width = 1024
height = 768
Graphics3d width, height, 0, 1
Setbuffer Backbuffer()
Global dest_cam_x#,dest_cam_z#,dest_cam_pitch#,dest_cam_yaw#

;Load Camera
camera = Createcamera()
Camerarange Camera,1,5000
Positionentity Camera,0,1000,0

gridtex = Loadtexture("media\grid.bmp")
Scaletexture gridtex,100,100

;load Grid
groundplane = Createplane()
Entitytexture groundplane, gridtex

While Not Keyhit(1)

Gosub mouseLook

Renderworld
Flip
Wend
End

;Sets Up MouseLook For The Main Camera
.mouselook
If Mousedown(2)
mxs=Mousexspeed()
mys=Mouseyspeed()
dest_cam_yaw#=dest_cam_yaw#-mxs
dest_cam_pitch#=dest_cam_pitch#+mys
cam_yaw=cam_yaw+((dest_cam_yaw-cam_yaw)/5)
cam_pitch=cam_pitch+((dest_cam_pitch-cam_pitch)/5)
Rotateentity camera,cam_pitch,cam_yaw,0
Movemouse Width/2,Height/2
Endif

Select Mousezspeed()
Case -1
Moveentity camera,0,0,-100
Case 1
Moveentity camera,0,0,100
End Select
Return

anyone?? I asked terraed and they kindly replied but for some reason it still didn't work.

Can someone translate this for me. Its french.
"Permet de deplacer un objet en Y grace au mouse wheel"

Joey you are still trying to reinvent the wheel here... Lets take your question backwards...

To place code on the forums you can either use {code} {/code} (the {} is really the [] but to make the example...) this is used for SHORT bits of code.

Long bits of code are used in {codebox} {/codebox} (again {} are really []...)

second... aside from remaking your code (I had to substitute a texture for one of my own) I find no problem in making it work... Push RIGHT mouse and turn and rotate WHEEL to move forward...??? What else do you want to do???
The WHEEL to move forward works IF you have pressed the RIGHT mouse previously... That can be fixed by cut and paste.
width = 1024 
height = 768 
Graphics3D width, height, 0, 1 
SetBuffer BackBuffer() 
Global dest_cam_x#,dest_cam_z#,dest_cam_pitch#,dest_cam_yaw# 

;Load Camera 
camera = CreateCamera() 
CameraRange Camera,1,5000 
PositionEntity Camera,0,1000,0 

gridtex = LoadTexture("grid.bmp") 
ScaleTexture gridtex,100,100 

;load Grid 
groundplane = CreatePlane() 
EntityTexture groundplane, gridtex 

While Not KeyHit(1) 

If MouseDown(2) 
mxs=MouseXSpeed() 
mys=MouseYSpeed() 
dest_cam_yaw#=dest_cam_yaw#-mxs 
dest_cam_pitch#=dest_cam_pitch#+mys 
cam_yaw=cam_yaw+((dest_cam_yaw-cam_yaw)/5) 
cam_pitch=cam_pitch+((dest_cam_pitch-cam_pitch)/5) 
RotateEntity camera,cam_pitch,cam_yaw,0 
MoveMouse Width/2,Height/2 
EndIf 

Select MouseZSpeed() 
Case -1 
MoveEntity camera,0,0,-100 
Case 1 
MoveEntity camera,0,0,100 
End Select 

RenderWorld 
Flip 
Wend 
End 

All I get with your program is MEMORY ACCESS VIOLATION which means you made calls to folders or images that aren't there in my directory. Lets face it this is your first project isn't it! : )

Oh yeah... the French... Keeping in mind my French was HS French about 20+ years ago... "permission to move the object in Y using the mouse wheel"

You can also write it this way:
Graphics3D 1024,768,16,1
SetBuffer BackBuffer()

SetBuffer BackBuffer() 
Global dest_cam_x#,dest_cam_z#,dest_cam_pitch#,dest_cam_yaw# 

;Load Camera 
camera = CreateCamera() 
CameraRange Camera,1,5000 
PositionEntity Camera,0,1000,0 

gridtex = LoadTexture("grid.bmp") 
ScaleTexture gridtex,100,100 

;load Grid 
groundplane = CreatePlane() 
EntityTexture groundplane, gridtex 

While Not KeyHit(1) 

Select MouseZSpeed() 
Case -1 
MoveEntity camera,0,0,-100 
Case 1 
MoveEntity camera,0,0,100
End Select 

If MouseDown(2) 
;mxs=MouseXSpeed() 
;mys=MouseYSpeed() 
;dest_cam_yaw#=dest_cam_yaw#-mxs 
;dest_cam_pitch#=dest_cam_pitch#+mys 
;cam_yaw=cam_yaw+((dest_cam_yaw-cam_yaw)/5) 
;cam_pitch=cam_pitch+((dest_cam_pitch-cam_pitch)/5) 
;RotateEntity camera,cam_pitch,cam_yaw,0 
;MoveMouse Width/2,Height/2 
;EndIf 
; ############################################# Cleaning up the code slightly here
  mxs#=MouseXSpeed()/4
  mys#=MouseYSpeed()/4
  xa#=(xa#-mxs#)Mod 360
  ya#=(ya#+mys#)Mod 360
  MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  RotateEntity camera,ya#,xa#,0
EndIf

RenderWorld 
Flip 
Wend 
End 
: )
Now if you want it to move forward like it is walking on its own WHEN YOU PRESS the RIGHT mouse button that would be a simple modification.
-RZ

hey man i thank all of you for your help. Yea this is my first but its going well til now but I got it sorted out. All my code worked fine before. It was an optical illusion when going at high speeds and I not having any other objects could not see this. I apologize for the inconvenience and I understand what your saying now lol. Thanks for you help! Hey also I did notice it would not work unless I press the right mouse button previously but what part of the code fixes that so I know for future refrence.

Great! Good luck... ;)
-RZ