Jump to content

Updated Basic 3rd Person Camera


Marleys Ghost
 Share

Recommended Posts

Updated Basic 3rd Person Camera

 

 

Changed : Pivot removed as it was not required and the player body creation re-written/condensed : (Thanks to Macklebee for that, and for reminding me to use curve on the zooming value)

 

Added : Player starts in 1st person, the mouse wheel now controls zooming out to 3rd person and back into 1st person view.

 

The additions to the Simple Scene Loader code are enclosed in dashed lines.

 

I hope this will be of some use to someone.

 

 

SuperStrict

Framework leadwerks.engine

Import "PATH TO YOUR 2.3 SDK FOLDER\BMX\Framework\framework.bmx"
Include "lua-gluefunctions.bmx"


AppTitle:String = "3rd Person camera test"
GCSetMode(2)

'Register the directory under which the abstract file system will search.
RegisterAbstractPath("PATH TO YOUR 2.3 SDK FOLDER")

'Create a Graphics window
Graphics(800, 600)


AFilter(4) 'Set the anisotropic filter. Range 1-16

'Using AFilter(MaxAFilter()) will use the maximum supported anisotropic filter on your PC
'Adding the line "DrawText ("MaxAFilter = " + MaxAFilter(), 0, 80)"
'after "fw.Render()" will display on screen the maximum supported anisotropic filter.

TFilter(1) 'Set trilinear filtering : 0 = off : 1 = on.


'Create a new Framework object.
Global fw:TFramework = TFramework.Create()
If Not fw RuntimeError "Failed to initialize engine."

       SetScriptObject("fw", fw)'Setup the global script objects.
       SetPostFX()'Setup the post processing FX
       SetVegetationShadowMode(0)'Setup vegetation layer shadows : 0 = off : 1 = on.

'Load the scene. 
       Global scene:TEntity = LoadScene("abstract::YOUR_SCENE.sbx")


'Setup the player controller.
       Global player:TController = CreateController(2.0)
       PositionEntity(player, StringToVec3(scene.getkey("cameraposition")))
       EntityType(player, 3) 
       SetBodyMass(player, 10.0)
       SetBodyBuoyancyMode(player,0)
       Collisions(1, 3, 3)

'Setup the player movement variables.
       Global move:Float = 0.0
       Global strafe:Float = 0.0
       Global camrotation:TVec3 = Vec3(0)
       Global mx:Float = 0.0
       Global my:Float = 0.0
       Global jump:Float = 0.0

'--------------------------------------------------------------------------------

Global camy:Float = 0.0
Global camz:Float = 0.0

'Setup the player character model
Global player_body:TEntity = CreateCube(player)
ScaleEntity(player_body,Vec3(0.5,1.8,0.5))

'--------------------------------------------------------------------------------

       HideMouse()

'Set stats level : 0 = No statistics : 1 = Show frame rate : 2 = Full statistics. 
       fw.SetStats(2)

'Main program loop.
Repeat
       If KeyHit(KEY_ESCAPE) Exit
       If AppTerminate() Exit

       UpdateAppTime()
       UpdateWorld(AppSpeed())

       PlayerController()

       fw.Update()
       fw.Render()

       Flip(0)
Forever
fw.renderer.gbuffer = Null
GCCollect()
End

'Functions.

Function SetPostFX()'0 = off : 1 = on.
       fw.renderer.SetAntialias(1)
       fw.renderer.SetBloom(1) 
       fw.renderer.SetGodRays(1)
       fw.renderer.SetHDR(1)
       fw.renderer.SetSSAO(0)
       fw.renderer.SetWireFrame(0)     
End Function

Function PlayerController()
       mx = Curve(MouseX() - GraphicsWidth() / 2, mx, 6)
       my = Curve(MouseY() - GraphicsHeight() / 2, my, 6)
       MoveMouse(GraphicsWidth() / 2, GraphicsHeight() / 2)
       camrotation.X = camrotation.X + my / 10.0
       camrotation.Y = camrotation.Y - mx / 10.0
       RotateEntity(fw.Main.camera, camrotation)

       move = KeyDown(KEY_W) - KeyDown(KEY_S)
       strafe = KeyDown(KEY_D) - KeyDown(KEY_A)
       jump:Float = 0.0
       If KeyHit(KEY_SPACE) & Not ControllerAirborne(player)
               jump = 4.0
       End If
       If KeyDown(KEY_LSHIFT) | KeyDown(KEY_RSHIFT)
               move = move * 3.0
               strafe = strafe * 3.0
       End If

'--------------------------------------------------------------------------------

PositionEntity(fw.Main.camera, Vec3(player.position.x, player.position.y + 0.8, player.position.z))
camy = 0.5
If MouseZ()>0 Then FlushMouse() 
If MouseZ()=0 Then camy = 0.0
camz = Curve(MouseZ(), camz, 10)
MoveEntity(fw.Main.camera, Vec3(0,camy,camz))

'--------------------------------------------------------------------------------

       UpdateController(player, camrotation.Y, move * 3, strafe * 3, jump, 500)
End Function

Function StringToVec2:TVec2(text:String, scale:Float = 1.0)
       Local t:TVec2 = Vec2(1)
       Local sarr:String[]
       sarr = text.split(",")
       If sarr
               If sarr.length > 0 t.x = Float(sarr[0]) * scale
               If sarr.length > 1 t.y = Float(sarr[1]) * scale
       EndIf
       Return t
EndFunction

Function StringToVec3:TVec3(text:String, scale:Float = 1.0)
       Local t:TVec3 = Vec3(1)
       Local sarr:String[]
       sarr = text.split(",")
       If sarr
               If sarr.length > 0 t.x = Float(sarr[0]) * scale
               If sarr.length > 1 t.y = Float(sarr[1]) * scale
               If sarr.length > 2 t.z = Float(sarr[2]) * scale
       EndIf
       Return t
EndFunction

Function StringToVec4:TVec4(text:String, scale:Float = 1.0)
       Local t:TVec4 = Vec4(1)
       Local sarr:String[]
       sarr = text.split(",")
       If sarr
               If sarr.length > 0 t.x = Float(sarr[0]) * scale
               If sarr.length > 1 t.y = Float(sarr[1]) * scale
               If sarr.length > 2 t.z = Float(sarr[2]) * scale
               If sarr.length > 3 t.w = Float(sarr[3]) * scale
       EndIf
       Return t
EndFunction

Function SetScriptObject(name:String, o:Object)
       Local size:Int=GetStackSize()
       lua_pushbmaxobject(luastate.L,o)
       lua_setglobal(luastate.L,name)
       SetStackSize(size)
EndFunction

Function GetStackSize:Int()
       Return lua_gettop(luastate.L)
EndFunction

Function SetStackSize(size:Int)
       Local currentsize:Int=GetStackSize()
       If size<currentsize
       lua_pop(luastate.L,currentsize-size)
       EndIf
EndFunction

 

 

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

  • 3 weeks later...

Thanks MG :)

 

I looked at your post (BasicSceneLoader) and then moved onto this one.

 

Works great and im getting great FPS (140 - 180) using the DesertHighWay.sbx + some of my models loaded in aswell :)

 

Keep it up mate , ya might make me a programmer yet ;)

 

Thanks

Gimpy73 :)

Link to comment
Share on other sites

ya might make me a programmer yet :)

 

 

 

 

Hmm, water into wine, "sure" ... cure lepers .. "doable" ... but Gimpy into a programmer?!?!?!?!? :)

 

You're welcome, I am glad they are of some use to someone.

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

Thank You, works great. Is it possible to restrict the camera that it is not going thru the ground like in my picture? Then it would be like in the Gothic games or in Risen. That would be great. ;)

post-86-12649311733528_thumb.jpg

Athlon Phenom 2 X4 965 BE / Windows 7 64bit SP1 / 8G Ram / GForce GTX 460

Link to comment
Share on other sites

Hey GoodOne.

 

Try adding this to the player controller function.

 

If camrotation.x > 90.0 Then camrotation.x = 90.0
If camrotation.x < -20.0 Then camrotation.x = -20.0

 

So your PlayerController should look like this:

 

Function PlayerController()
       mx = Curve(MouseX() - GraphicsWidth() / 2, mx, 6)
       my = Curve(MouseY() - GraphicsHeight() / 2, my, 6)
       MoveMouse(GraphicsWidth() / 2, GraphicsHeight() / 2)
       camrotation.X = camrotation.X + my / 10.0
       camrotation.Y = camrotation.Y - mx / 10.0
       RotateEntity(fw.Main.camera, camrotation)

	If camrotation.x > 90.0 Then camrotation.x = 90.0
	If camrotation.x < -20.0 Then camrotation.x = -20.0

       move = KeyDown(KEY_W) - KeyDown(KEY_S)
       strafe = KeyDown(KEY_D) - KeyDown(KEY_A)
       jump:Float = 0.0
       If KeyHit(KEY_SPACE) & Not ControllerAirborne(player)
               jump = 8.0
       End If
       If KeyDown(KEY_LSHIFT) | KeyDown(KEY_RSHIFT)
               move = move * 3.0
               strafe = strafe * 3.0
       End If

There might be a better way of doing this , but it works :)

 

Thanks

 

Gimpy73 ;)

Link to comment
Share on other sites

gimpy the programmer!

 

 

 

 

WOW!!!!!! I am more of a god than I thought ... who'd have thunk ;):)

 

 

 

 

Now ... put that code in code quotes !! ;)

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

I would if i knew how lol ;)

 

Maybe since ya some sort of God , ya could teach me :)

 

 

 

Well if I believed in reincarnation I might be under the impression that there would be enough time but ... ;)

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

Waits for MG to come and smite me down

 

 

 

Can't smite you when you are expecting it ... wheres the fun in that ;):)

AMD Bulldozer FX-4 Quad Core 4100 Black Edition

2 x 4GB DDR3 1333Mhz Memory

Gigabyte GeForce GTX 550 Ti OC 1024MB GDDR5

Windows 7 Home 64 bit

 

BlitzMax 1.50 • Lua 5.1 MaxGUI 1.41 • UU3D Pro • MessiahStudio Pro • Silo Pro

3D Coat • ShaderMap Pro • Hexagon 2 • Photoshop, Gimp & Paint.NET

 

LE 2.5/3.4 • Skyline UE4 • CE3 SDK • Unity 5 • Esenthel Engine 2.0

 

Marleys Ghost's YouTube Channel Marleys Ghost's Blog

 

"I used to be alive like you .... then I took an arrow to the head"

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...