System Third Person Camera

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
Ever needed an easy to use Third Person Camera view system?
Yeah, me neither.
Hence the immediate need to write one...

No idea what it is?
Try the map... :p


Parental advisory: NewGen required!


Here it is:
JASS:
library TPC initializer Init

globals
    // How often the camera is adjusted. Every 0.15 seconds is plenty fast enough
    private constant real Period = 0.15

    // General camera options
    private constant real CameraHeight = 300.0
    private constant real CameraAngle = 345
    private constant real CameraDistance = 600

    // Arrow keys can be used to rotate and zoom
    private constant boolean AllowArrowKeys = true
    // Escape resets the camera to "general camera options"
    private constant boolean AllowEscape = true

    // Wan't to be able to turn it on or off?
    private constant string Command = "-camera"
    private constant boolean AllowCommand = true

    // Optionally stops the camera if the current camera unit dies
    private constant boolean RemoveOnDeath = true

    // Rotation is either free...
    private constant boolean AllowFreeRotation = false
    // ... or limited to +/- this:
    private constant real CameraMaxRotate = 90

    // You can change this... but should just leave them
    private constant real CameraMaxDistance = 1200
    private constant real CameraMinDistance = 150

    // DON'T touch anything here
    private group CameraUnits = CreateGroup()
    private group CameraUnits_start = CreateGroup()
    private real array Rotate
    private real array Distance
    private real array Angle
    private boolean array DoLeft
    private boolean array DoRight
    private boolean array DoDown
    private boolean array DoUp
endglobals

private function CameraRemovePlayer_function takes nothing returns nothing
    if GetOwningPlayer(GetEnumUnit()) == bj_forceRandomCurrentPick then
        call GroupRemoveUnit(CameraUnits_start, GetEnumUnit())
        call GroupRemoveUnit(CameraUnits, GetEnumUnit())
    endif
endfunction

function TPCRemovePlayer takes player p returns nothing
    set bj_forceRandomCurrentPick = p
    call ForGroup(CameraUnits, function CameraRemovePlayer_function)
    call ForGroup(CameraUnits_start, function CameraRemovePlayer_function)
    if GetLocalPlayer() == p then
        call ResetToGameCamera(1.5)
    endif
endfunction

function TPCAdd takes unit u returns nothing
    local player p = GetOwningPlayer(u)
    local integer i = GetPlayerId(p)
    call TPCRemovePlayer(p)
    set Rotate<i> = 0
    set Angle<i> = CameraAngle
    set Distance<i> = CameraDistance
    call GroupAddUnit(CameraUnits_start, u)
    call GroupAddUnit(CameraUnits, u)
    set p = null
endfunction

function TPCRemove takes unit u returns nothing
    call GroupRemoveUnit(CameraUnits_start, u)
    call GroupRemoveUnit(CameraUnits, u)
    if GetLocalPlayer() == GetOwningPlayer(u) then
        call ResetToGameCamera(1.5)
    endif
endfunction

function TPCRemoveAll takes nothing returns nothing
    call GroupClear(CameraUnits)
    call GroupClear(CameraUnits_start)
endfunction

private function Death takes nothing returns nothing
    if IsUnitInGroup(GetTriggerUnit(), CameraUnits_start) or IsUnitInGroup(GetTriggerUnit(), CameraUnits) then
        call TPCRemove(GetTriggerUnit())
    endif
endfunction

private function Movement takes nothing returns nothing
    local unit u = GetEnumUnit()
    local location l = GetUnitLoc(u)
    if GetLocalPlayer() == GetOwningPlayer(u) then
        call PanCameraToTimed(GetUnitX(u), GetUnitY(u), Period)
        call SetCameraField(CAMERA_FIELD_ROTATION, GetUnitFacing(u) + Rotate[GetPlayerId(GetOwningPlayer(u))], Period)
        call SetCameraField(CAMERA_FIELD_ZOFFSET, GetCameraField(CAMERA_FIELD_ZOFFSET) + GetLocationZ(l) + CameraHeight - GetCameraEyePositionZ(), Period)
        call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, Angle[GetPlayerId(GetOwningPlayer(u))], Period)
        call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, Distance[GetPlayerId(GetOwningPlayer(u))], Period)
    endif
    call RemoveLocation(l)
    set u = null
    set l = null
endfunction

private function Actions takes nothing returns nothing
    call ForGroup(CameraUnits, function Movement)
endfunction

private function usesCamera takes nothing returns nothing
    if GetOwningPlayer(GetEnumUnit()) == GetTriggerPlayer() then
        set bj_isUnitGroupDeadResult = false
    endif
endfunction

private function CameraOn takes nothing returns nothing
    local integer i = GetPlayerId(GetTriggerPlayer())
    if GetOwningPlayer(GetEnumUnit()) == GetTriggerPlayer() then
        set Rotate<i> = 0
        set Angle<i> = CameraAngle
        set Distance<i> = CameraDistance
        call GroupAddUnit(CameraUnits,  GetEnumUnit())
    endif
endfunction

private function CameraOff takes nothing returns nothing
    if GetOwningPlayer(GetEnumUnit()) == GetTriggerPlayer() then
        call GroupRemoveUnit(CameraUnits, GetEnumUnit())
    endif
endfunction

private function ChatActions takes nothing returns nothing
    set bj_isUnitGroupDeadResult = true
    call ForGroup(CameraUnits, function usesCamera)
    if bj_isUnitGroupDeadResult then
        call ForGroup(CameraUnits_start, function CameraOn)
    else
        call ForGroup(CameraUnits, function CameraOff)
        if GetLocalPlayer() == GetTriggerPlayer() then
            call ResetToGameCamera(1.5)
        endif
    endif
endfunction

private function KeyLeft takes nothing returns nothing
    set DoLeft[GetPlayerId(GetTriggerPlayer())] = not DoLeft[GetPlayerId(GetTriggerPlayer())]
endfunction
private function KeyRight takes nothing returns nothing
    set DoRight[GetPlayerId(GetTriggerPlayer())] = not DoRight[GetPlayerId(GetTriggerPlayer())]
endfunction
private function KeyDown takes nothing returns nothing
    set DoDown[GetPlayerId(GetTriggerPlayer())] = not DoDown[GetPlayerId(GetTriggerPlayer())]
endfunction
private function KeyUp takes nothing returns nothing
    set DoUp[GetPlayerId(GetTriggerPlayer())] = not DoUp[GetPlayerId(GetTriggerPlayer())]
endfunction

private function DoKeys takes nothing returns nothing
    local integer i = 0
    loop
        if DoLeft<i> then
            if AllowFreeRotation or Rotate<i> &lt; CameraMaxRotate then
                set Rotate<i> = Rotate<i> + 8
                if Rotate<i> &gt; 360 then
                    set Rotate<i> = Rotate<i> - 360
                endif
            endif
        endif
        if DoRight<i> then
            if AllowFreeRotation or Rotate<i> &gt; -CameraMaxRotate then
                set Rotate<i> = Rotate<i> - 8
                if Rotate<i> &lt; -360 then
                    set Rotate<i> = Rotate<i> + 360
                endif
            endif
        endif
        if DoDown<i> then
            if Distance<i> &lt; CameraMaxDistance then
                set Distance<i> = Distance<i> + 25
                set Angle<i> = Angle<i> + 0.35
            endif
        endif
        if DoUp<i> then
            if Distance<i> &gt; CameraMinDistance then
                set Distance<i> = Distance<i> - 25
                set Angle<i> = Angle<i> - 0.35
            endif
        endif
        set i = i + 1
        exitwhen i &gt;= 12
    endloop
endfunction

private function Escape takes nothing returns nothing
    local integer i = GetPlayerId(GetTriggerPlayer())
    set Rotate<i> = 0
    set Angle<i> = CameraAngle
    set Distance<i> = CameraDistance
endfunction

private function Init takes nothing returns nothing
    local integer i
    local trigger t = CreateTrigger()
    local trigger t1 = CreateTrigger()
    local trigger t2 = CreateTrigger()
    local trigger t3 = CreateTrigger()
    local trigger t4 = CreateTrigger()
    local timer m
    local player p
    call TriggerRegisterTimerEventPeriodic(t, Period)
    call TriggerAddAction(t, function Actions)
    if AllowCommand then
        set t = CreateTrigger()
        call TriggerAddAction(t, function ChatActions)
        set i = 0
        loop
            call TriggerRegisterPlayerChatEvent(t, Player(i), Command, true)
            set i = i + 1
            exitwhen i &gt;= 12
        endloop
    endif
    if RemoveOnDeath then
        set t = CreateTrigger()
        call TriggerAddAction(t, function Death)
        set i = 0
        loop
            call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_DEATH, null)
            set i = i + 1
            exitwhen i &gt;= 12
        endloop
    endif
    call TriggerAddAction(t1, function KeyLeft)
    call TriggerAddAction(t2, function KeyRight)
    call TriggerAddAction(t3, function KeyDown)
    call TriggerAddAction(t4, function KeyUp)
    set i = 0
    loop
        set Rotate<i> = 0
        set Distance<i> = CameraDistance
        set Angle<i> = CameraAngle
        set DoLeft<i> = false
        set DoRight<i> = false
        set DoDown<i> = false
        set DoUp<i> = false
        set p = Player(i)
        if AllowArrowKeys then
            call TriggerRegisterPlayerEvent(t1, p, EVENT_PLAYER_ARROW_LEFT_DOWN)
            call TriggerRegisterPlayerEvent(t1, p, EVENT_PLAYER_ARROW_LEFT_UP)

            call TriggerRegisterPlayerEvent(t2, p, EVENT_PLAYER_ARROW_RIGHT_DOWN)
            call TriggerRegisterPlayerEvent(t2, p, EVENT_PLAYER_ARROW_RIGHT_UP)

            call TriggerRegisterPlayerEvent(t3, p, EVENT_PLAYER_ARROW_DOWN_DOWN)
            call TriggerRegisterPlayerEvent(t3, p, EVENT_PLAYER_ARROW_DOWN_UP)

            call TriggerRegisterPlayerEvent(t4, p, EVENT_PLAYER_ARROW_UP_DOWN)
            call TriggerRegisterPlayerEvent(t4, p, EVENT_PLAYER_ARROW_UP_UP)
        endif
        set i = i + 1
        exitwhen i &gt;= 12
    endloop
    set m = CreateTimer()
    call TimerStart(m, 0.05, true, function DoKeys)
    set m = null

    if AllowEscape then
        set t = CreateTrigger()
        call TriggerAddAction(t, function Escape)
        set i = 0
        loop
            call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_END_CINEMATIC)
            set i = i + 1
            exitwhen i &gt;= 12
        endloop
    endif


    set t = null
    set t1 = null
    set t2 = null
    set t3 = null
    set t4 = null
    set p = null
endfunction

endlibrary
</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>



How to use it:

Put the library somewhere in your map.
JASS user: you know what to do...
GUI user: Map header for example(*).

Give some unit to your players, and "call TPCAdd(<unit>)".
That's it.


Function reference:

- call TPCAdd(<unit>)

Sets the "camera" unit for its owner.
It will automatically replace the current unit, if there is one.
I.e. this is pretty much the only call you're going to need.

- call TPCRemove(<unit>)

Removes that unit, and the player that owns it, from the system.

- call TPCRemovePlayer(<player>)

Don't remember what unit it was? How did that happen? Call this to remove the player regardless.

- call TPCRemoveAll()

Removes all players from the system.


Configuration options:

- Period = 0.15

Determines how often the camera will readjust its settings.
Some people seem to believe that nothing less than every 0.01, i.e. 100 times a second, will do.
Others go with 0.3 or so for "lag" reasons...
Well, whatever.
The default, 0.15, works just fine.

- CameraHeight = 300.0
- CameraAngle = 345
- CameraDistance = 600

Some general options.
I hope it is clear from the name what they are doing(**).

- Command = "-camera"
- AllowCommand = true

If "true", there will be a chat command available to turn the system on or off as needed.
Assumes your unit has been added by a previous call to TPCAdd.

- RemoveOnDeath = true

Stops the camera system for a player as soon as his current camera unit dies.
That may or may not make any sense in your map.

- AllowArrowKeys = true

If true, you may use the arrow keys, up, down, left and right, to rotate or zoom the camera.

- AllowEscape = true

If true, you may press "escape" to reset the camera to the default values.

- AllowFreeRotation = false

Rotation, assuming arrows keys are allowed, can be either unlimited...

- CameraMaxRotate = 90

... or at most +/- this much.

- CameraMaxDistance = 1200
- CameraMinDistance = 150

Same here, if arrow keys are on, this limits the zooming to something that still makes sense.


Q: Does it work in multi-player?
A: Yes.

Q: Does it desync in multi-player?
A: No.

Q: Have you tested that?
A: No.

Q: Does it work with flying units?
A: Yes. But you need some slightly different height and other settings... experimentation is the key here.

Q: Can I set "distance" to 100?
A: Yes.

Q: What will that do?
A: Change to First Person Camera view system.

Q: Amazing!
A: Not really... If you don't see your unit and somehow manage to lose the selection, you have a problem...

Q: Why 100? What about 0, or less?
A: Doesn't work with less than 100...

Q: I use GUI, can I still use this?
A:
Yes, read the part "How to use it".
Additionally, to call this from GUI... well, the simplest way is probably this one:
Create some variable, of type unit (called "TempUnit" here) and
Custom script: call TPCAdd( udg_TempUnit )
Simple enough I take it(***)

Q: Well, I create the units for my players...
A:
Don't care...
Set TempUnit = (Last created unit)
Custom script: call TPCAdd( udg_TempUnit )

Q: OK! Got it! So, this doesn't work when buying the Hero from a Tavern, right?
A:
...

Event:
- A unit sells a unit
Conditions:
- ((Sold unit) is a Hero) equal to true
Actions:
- Set TempUnit = (Sold unit)
- Custom script: call TPCAdd( udg_TempUnit )

Q: 0.15 lags!
A: No, it doesn't.

Q: 0.15 is not fast enough!
A: No, it doesn't.

Q: That's the same answer as on the previous question.
A: Damn...



Yours,
AceHart


(*) No idea where that is?
Trigger editor, notice your map's name at the very top of the trigger list, click there.
See that empty space where you usually see a trigger?
That would be it.

(**) Yes Ace, sure... keep dreaming.

(***) see (**)

(****) Recursion: n. see (****) until you get it.

(*****)
Q: Isn't it simply "Recursion: n. see Recursion"?
A: No. That's not recursion, that's an endless loop...
 

Attachments

  • TPC_Demo_AceHart.w3x
    48.9 KB · Views: 1,148

UndeadDragon

Super Moderator
Reaction score
447
I just tried it out and I think it's one of the best systems I have tried.

Very smooth camera movement there. Good job +rep

EDIT: Can't yet :p
 

Kazuga

Let the game begin...
Reaction score
110
Just what I needed, second best I have ever seen this far. Thanks:thup:
 

Kazuga

Let the game begin...
Reaction score
110
>Who's first?
I saw a camera on youtube which was as controllable as the wow camera meaning that you could drag the camera by holding down left click and moving the mouse, and zooming in and out with scroll. Altough this also needed some special add thing to use.

Edit: Found a link.
 

UndeadDragon

Super Moderator
Reaction score
447
I think they are using a third party program, like Reinventing the Craft, so they can detect mouse movement and also detect other key pushes.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
Well, if that's the competition, I have no problems with second place... :p
And, yes, you need a special "plugin" of sorts, the RtC mentioned...
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
Update:
Added arrow keys to rotate and zoom. Escape resets the view.


Now, if anyone would know how to prevent the initial arrow-key-moves-normal-camera "feature", I'm all ears.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Wow, that's a great looking camera!

I liked just about everything about it, except for the arrow key zooming/rotating are a little weird. When you zoom in/out it's not just a simple distance-to-target change (or so it seems), it seems like it moves up higher (z offset) when you zoom out, and lower when you zoom in.

The rotate wasn't too bad, though It was a little annoying to have to rotate back to go to front view. I know you have esc to reset the camera, but maybe you could add a feature that automatically resets the rotation back to the middle if the unit starts moving, unless the arrow keys are held down.

Also, for an idea, you might be able to detect when the delete/ins keys are held down, as well as the scroll button, by checking to see if the AoA is nothing like it should be when the camera resets itself, and if the rotation is not what it should be at. It could make it so you don't have to use the arrow keys at all, and remove the user lag, by not using them at all.

Useful system there. I'm actually adjusting a few values in my own FP camera to make it similar to yours, since mine was a bit hard to see with. :shades:

Maybe you could try locking the camera to the unit, then unlocking right away, when your system is first initialized?
 

PureOwnage

Minecraft Server OP, Inactive.
Reaction score
72
AceHart, I have one question.
Where are you getting these Q and A questions.
They are hilarious. :D
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top