3rd Person View with mouse movement

Blitz3D Forums/Blitz3D Beginners Area/3rd Person View with mouse movement

Chad(Posted 2006) [#1]
All the 3rd person Codes I have looked at do not include the mouse in any part of the program. I want to create a 3rd person view of a mesh where you can rotate around the mesh with the mouse.. Does anyone have any suggestions to this?

If you want to look at what code I would like to use, with this new impletication, here it is:

; ID: 798
; Author: PsychicParrot
; Date: 2003-09-25 14:26:29
; Title: Supercam! Another 3rd person cam.
; Description: Alternative to smoothcam, I guess :)

; SuperCam!
;
; by PsychicParrot 2003
;
; 
; Usage : SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)
; 
;

Graphics3D 640,480,16,3
SetBuffer=BackBuffer()

Global campivot=CreateCube() ; create pivot for camera
Global camera=CreateCamera()  ; create camera (!!!)

; ---------------------- THIS IS ALL JUST TO POPULATE THE WORLD WITH SOME RUBBISH ---------------

Global light=CreateLight()
Global player=CreateCube()    ; create simple player
Global plane=CreatePlane()   ; create simple floor
MoveEntity player,0,1,0
; Create texture of size 256x256
tex=CreateTexture(256,256)

; Set buffer - texture buffer
SetBuffer TextureBuffer(tex)

; Clear texture buffer with background white color
For i=1 To 10
Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
Rect Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256)
Next

; Texture cube with texture
EntityTexture plane,tex
EntityTexture player,tex

; Set buffer - backbuffer
SetBuffer BackBuffer()

; ----------------------------------------------------------------------------------------------

While Not KeyHit(1)

If KeyDown(200) Then 
	MoveEntity player,0,0,.2
End If

If KeyDown(203) TurnEntity player,0,1,0
If KeyDown(205) TurnEntity player,0,-1,0

SuperCam(camera,player,.02,8,3,0,2)

RenderWorld
Flip

Wend

End

Function SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)

TFormPoint 0,hite#,-dist#,ent,0

cx#=(TFormedX()-EntityX(cam))*cspeed#
cy#=(TFormedY()-EntityY(cam))*cspeed#
cz#=(TFormedZ()-EntityZ(cam))*cspeed#

TranslateEntity cam,cx,cy,cz
PointEntity cam,ent
RotateEntity cam,xrot#,EntityYaw(cam),tilt#

End Function

Basically, other than that, this is exactly how I want it, I just want to rotate around the mesh with my mouse.

Thanks for the help,
Chad


IKG(Posted 2006) [#2]
I'm working on a game right now that was going to use the same exact camera controls, but then I realized it would be more original and just plain better to make up my own control scheme.


Chad(Posted 2006) [#3]
Any suggestions or anything you could help me with?

Thanks,
Chad


SheepOnMintSauce(Posted 2006) [#4]
Hello Chad, I asked something similar to this a while back. Well, I think it's similar anyway.. There's a bunch of answers, some people helped me with here.

Not sure if it helps or not. =)


jfk EO-11110(Posted 2006) [#5]
you may use MouseX() to define the angle of the camera.

; x,y and z is te position of the player
mx=mousex()
positionentity cam,x+sin(mx)*20.0,y+10.0,z+cos(mx)*20.0
pointentity cam,player

Now the tricky part, when there is a wall between the camera and the player, you probably want the camera to get closer, until it can see the player.

Player and Scene must be pickable. Check if the camera can see the player (entityvisible). If so, use the default distance. If it's obscured then move the camera closer until it can be seen.
You may use a binary search instead of simple steps towards the player, to reduce the number of EntityVisible calls (they're pretty slow. the more complex a scene is, the slower it is)

Using this method will dynamicly zoom to the player whenever needed when you are rotating the camera around the player.


Chad(Posted 2006) [#6]
So jfk, what your saying is something like this:

; ID: 798
; Author: PsychicParrot
; Date: 2003-09-25 14:26:29
; Title: Supercam! Another 3rd person cam.
; Description: Alternative to smoothcam, I guess :)

; SuperCam!
;
; by PsychicParrot 2003
;
; 
; Usage : SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)
; 
;

Graphics3D 1024,768,1,2
SetBuffer=BackBuffer()

Global campivot=CreateCube() ; create pivot for camera
Global camera=CreateCamera()  ; create camera (!!!)

; ---------------------- THIS IS ALL JUST TO POPULATE THE WORLD WITH SOME RUBBISH ---------------

Global light=CreateLight()
Global player=CreateCube()    ; create simple player
Global plane=CreatePlane()   ; create simple floor
MoveEntity player,0,1,0
; Create texture of size 256x256
tex=CreateTexture(256,256)

; Set buffer - texture buffer
SetBuffer TextureBuffer(tex)

; Clear texture buffer with background white color
For i=1 To 10
Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
Rect Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256)
Next

; Texture cube with texture
EntityTexture plane,tex
EntityTexture player,tex

; Set buffer - backbuffer
SetBuffer BackBuffer()

; ----------------------------------------------------------------------------------------------

While Not KeyHit(1)

If KeyDown(200) Then 
	MoveEntity player,0,0,.2
End If
If KeyDown (208) Then
	MoveEntity player,0,0,-.2
End If


If KeyDown(203) TurnEntity player,0,1,0
If KeyDown(205) TurnEntity player,0,-1,0


SuperCam(camera,player,.02,8,3,0,2)

RenderWorld
Flip

mx=MouseX()
PositionEntity cam,x+Sin(mx)*20.0y+10.0,z+Cos(mx)*20.0
PointEntity cam,player

If EntityVisible = True Then SuperCam(Camera,player,.02,8,3,0,2)
If EntityVisible = False Then SuperCam(Camera,player,.1,7,2,0,1)

Wend

End

Function SuperCam(cam,ent,cspeed#,dist#,hite#,xrot#,tilt#)

TFormPoint 0,hite#,-dist#,ent,0

cx#=(TFormedX()-EntityX(cam))*cspeed#
cy#=(TFormedY()-EntityY(cam))*cspeed#
cz#=(TFormedZ()-EntityZ(cam))*cspeed#

TranslateEntity cam,cx,cy,cz
PointEntity cam,ent
RotateEntity cam,xrot#,EntityYaw(cam),tilt#

End Function


If that's what your saying.. this code doesn't work, so could you explain a little better? (Maybe I'm just not gettin it)


mindstorms(Posted 2006) [#7]
You never set x,y, and z to anything for the positionentity cam,x+sin(mx)*20.0...For this to work you will have to update x, y, and z with the player coordinates or simply use entityx#(player),entityy#(player), and entityz#(player).

You also forgot to put a comma seperating the y from the x in the same line.


Chad(Posted 2006) [#8]
Howdo I update the players position coordinates or whatever? I fixed the x and y comma problem :-)


mindstorms(Posted 2006) [#9]
put this in the loop
x# = entityX#(player)
y# = entityY#(player)
z# = entityZ#(player)


Of course, this sets the x,y,and z values to a "random" point on the cube, so I would sugest using at startup

FitMesh  Mesh,  MeshWidth(Mesh)/-2 , MeshHeight(Mesh)/-2, MeshDepth(Mesh)/-2, MeshWidth(Mesh), MeshHeight(Mesh), MeshDepth(Mesh)


where "mesh" is the mesh that you want to make the handle point to the center. Ross C gave this code to me in another forum, it is not mine.


Chad(Posted 2006) [#10]
Ok, I think I almost got it :-D

An error pops up that says "Entity does not exist" highlighting

PositionEntity cam,x+Sin(mx)*20.0,y+10.0,z+Cos(mx)*20.0z+Cos(mx)*20


Did I mess somethin up along this line?


mindstorms(Posted 2006) [#11]
As far as I can see, you initialize the "cam" as camera. Try changing all the "cam"s to "camera"s


Chad(Posted 2006) [#12]
Ok, it loads now, but the mouse is still not moving anything..

I didn't think it would be this much of a hassle, sorry guys but just need a little more help.


mindstorms(Posted 2006) [#13]
I can't see any reason for there to be the supercam function anymore...the positionentity wipes out everything the supercam does.

I messed around with it, and I think this is what you want.




Chad(Posted 2006) [#14]
As of now this is exactly what I wanted :-D

Thanks for all the help mindstorms


mindstorms(Posted 2006) [#15]
Your welcome! I just relized that I did not put in the fitmesh line, so you might want to add that in as well.


jfk EO-11110(Posted 2006) [#16]
BTW - since in mindstorms example the camera is parented to the player, you won't need to use the variables x,y and z for the camera, instead you may say

PositionEntity camera,Sin(mx)*20.0, 10.0, Cos(mx)*20.0

because the position is relative to the player.

It may become neccessary to unparent the camera if you plan to smooth out the camera motion, using a rubberband motion delay. In this case you would calculate the cameras wanted position the same way as you did it here, but instead of positioning the camera right there you would rather move the camera towards that new coordinate. Typicly you would measure the distance from the current position to the wanted position, divide this by 3 and then move the camera by this distance towards the wanted position.


Chad(Posted 2006) [#17]
I don't know if I messed something up, but there is a large black box across the bottom of my screen.. Any suggestions?

Also, it only appears with my model, if you want my exact model to test it out download it from http://www.chadmcrae.com/human.zip

Back to my code

Graphics3D 1024,768,1,2
SetBuffer=BackBuffer()

Global camera=CreateCamera()  ; create camera (!!!)
 
; ---------------------- THIS IS ALL JUST TO POPULATE THE WORLD WITH SOME RUBBISH ---------------

Global light=CreateLight()
;Global player=CreateCube()    ; create simple player
Global player=LoadMesh("C:\Documents and Settings\Owner\Desktop\Small models\Restaurants\bar\textures\man.3ds")
ScaleEntity player, .025,.025,.025
;RotateEntity player,0,90,90

Global plane=CreatePlane()   ; create simple floor
MoveEntity player,0,0,20
EntityParent(camera,player)
; Create texture of size 256x256
tex=CreateTexture(256,256)

; Set buffer - texture buffer
SetBuffer TextureBuffer(tex)



; Clear texture buffer with background white color
For i=1 To 10
Color Rnd(0,255),Rnd(0,255),Rnd(0,255)
Rect Rnd(0,256),Rnd(0,256),Rnd(0,256),Rnd(0,256)
Next

; Texture cube with texture
EntityTexture plane,tex
;EntityTexture player,tex

; Set buffer - backbuffer
SetBuffer BackBuffer()

; ----------------------------------------------------------------------------------------------

While Not KeyHit(1)

If KeyDown(200) Then 
	MoveEntity player,0,0,.2
End If
If KeyDown (208) Then
	MoveEntity player,0,0,-.2
End If


If KeyDown(203) TurnEntity player,0,1,0
If KeyDown(205) TurnEntity player,0,-1,0



mx=MouseX()
PositionEntity camera,x+Sin(mx)*50.0,y+20.0,z+Cos(mx)*50.0
PointEntity camera,player

RenderWorld
Flip

Wend
End



boomboom(Posted 2006) [#18]
Change the line to this:

PositionEntity camera,x+Sin(mx)*200.0,y+80.0,z+Cos(mx)*200.0

Your camera was too low, which was clipping the ground


Chad(Posted 2006) [#19]
Thank you, that solved the problem :)


Mike0101(Posted 2006) [#20]
This is no solution. No look up, no look down, no collisions :(


Chad(Posted 2006) [#21]
To be able to look up and look down what would I change from this code?

my=MouseY()
PositionEntity camera,x+Sin(mx)*50.0,y+20.0,z+Cos(mx)*50.0
PointEntity camera,player


It doesn't work like the mousex command does.

One last thing, less important but what I would like to get in the future, is when you move your mouse to look around, the model turns with it, but only when a key is pressed does it flip back to the backside say, like when your running, and you move the mouse to the left then the player follows what direction you move the mouse.. But while standing still and no key is hit, then you can encircle the model.. How would I go about doing that?

PS: Mike, if you want I can give you my code of collisions, and as you can see, I'm trying to learn the rest of the code, such as the look up and looking down.


Mike0101(Posted 2006) [#22]
Chad

I'm created a topic with my code. 'TPS ... ' Look it.
If you have something useful I'm waiting

thnx


Sir Gak(Posted 2006) [#23]
One way to get your viewpoint to turn, is to check the mousex() and/or mousey() coordinates. If a user swings the mouse pointer to the far left, you would use that to make the camera entity turn to the left. Maybe have the program check for whether the mouse position is within such-and-such number of pixels of the left side(say, if mousex() < 100 then turnentity camera,etc).

Same thing for the right turn, and of course, this is applicable for looking up or down, but just using a check on the mousey() position.

You can experiment for the right number of pixels, but perhaps within 10% of the respective screen width and screen height dimensions, might be a usable choice.


Chad(Posted 2006) [#24]
Anything on getting the mousey() to work Sir Gak?


jfk EO-11110(Posted 2006) [#25]
maybe you should start using angle variables instead of absolute mousex parameters.

So you would say:

a_x#=a_x#+mouseXspeed()
if a_x<0 then a_x=a_x+360
if a_x>360 then a_x=a_x-360

a_y#=a_y+mouseYspeed()
if a_y<-45 then a_y=-45
if a_y> 45 then a_y= 45


PositionEntity camera,x+Sin(a_x)*50.0,y+20.0,z+Cos(a_x)*50.0
PointEntity camera,player
rotateentity camera,a_y, entityYaw(camera),entityRoll(camera),1

cause the first parameter of Rotateentity (after the entity handle) is the pitch angle that defines if it's looking up or down.


Chad(Posted 2006) [#26]
Thank you..

I've got it working, and it's a lot like Mike0101's code..

However, I do have a couple questions about his code in his thread..