System TheLegend's Advanced Third Person Camera

TheLegend

New Member
Reaction score
10
Advanced Third Person Camera
by TheLegend​
After i saw the system that AceHart created I decided to write my own camera system.
The system is easy to use, it doesn't leak or lag, and its fast
FEATURES AND INFO
JASS:

// Advanced Third Person Camera by TheLegend
// Features:
// Features:
//          - The camera can be rotated
//          - The distance of the camera can be reduced or increased
//          - The camera avoids objects that can block your view
//          - It can be switched from one unit to another
//          - It can be switched on or off
//          - It can adjust the height and the angle according to the terrain
// Controls:
//          - Escape        -    Turn off or on
//          - Arrow keys    -    Rotation and distance change
// What you need:
//          - vJASS
//          - No variables needed
// It doesnt leak or lag
// Just copy everything from the map header to your map

SYSTEM
Before we start with the system you need a Terrain Pathability Checker. Use this one since its the best. PS: this library isn't mine but the Camera library is
JASS:

library TerrainPathability initializer Initialization
globals
    private constant real    MAX_RANGE     = 10.
    private constant integer DUMMY_ITEM_ID = 'wolg'
endglobals
globals    
    private item       Item   = null
    private rect       Find   = null
    private item array Hid
    private integer    HidMax = 0
    public  real       X      = 0.
    public  real       Y      = 0.
endglobals
function IsTerrainDeepWater takes real x, real y returns boolean
    return not IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY) and IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY)
endfunction
function IsTerrainShallowWater takes real x, real y returns boolean
    return not IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY) and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY) and IsTerrainPathable(x, y, PATHING_TYPE_BUILDABILITY)
endfunction
function IsTerrainLand takes real x, real y returns boolean
    return IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY)
endfunction
function IsTerrainPlatform takes real x, real y returns boolean
    return not IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY) and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY) and not IsTerrainPathable(x, y, PATHING_TYPE_BUILDABILITY)
endfunction
private function HideItem takes nothing returns nothing
    if IsItemVisible(GetEnumItem()) then
        set Hid[HidMax] = GetEnumItem()
        call SetItemVisible(Hid[HidMax], false)
        set HidMax = HidMax + 1
    endif
endfunction
function IsTerrainWalkable takes real x, real y returns boolean
    call MoveRectTo(Find, x, y)
    call EnumItemsInRect(Find ,null, function HideItem)
    call SetItemPosition(Item, x, y)
    set X = GetItemX(Item)
    set Y = GetItemY(Item)
    call SetItemVisible(Item, false)
    loop
        exitwhen HidMax <= 0
        set HidMax = HidMax - 1
        call SetItemVisible(Hid[HidMax], true)
        set Hid[HidMax] = null
    endloop
    return (X-x)*(X-x)+(Y-y)*(Y-y) <= MAX_RANGE*MAX_RANGE and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY)
endfunction
private function Initialization takes nothing returns nothing
    set Find = Rect(0., 0., 128., 128.)
    set Item = CreateItem(DUMMY_ITEM_ID, 0, 0)
    call SetItemVisible(Item, false)
endfunction
endlibrary

Now we cone to the TPC System. It doesn't leak or lag. It took me hours to complete it. Just copy it to the map header like the Pathing library
JASS:

library ThirdPersonCamera initializer Init uses TerrainPathability

globals
    //How often the camera should be adjusted, 0.15 is ok
    private constant real Period = 0.15
    //The angle of the camera
    private constant real CameraAngle = 345
    //The Height of the camera
    private constant real CameraHeight = 150
    //The maximum and minimum distance
    private constant real CameraMaxDistance = 800
    private real array CameraMaxNow
    private constant real CameraMinDistance = 50
    //Booleans to control the options of the camera
    private constant boolean CameraAllowArrows = true
    private constant boolean CameraAllowEscape = true
    private constant boolean CameraNotDead = true
    //Dont tuch anything here
    private group CameraMainUnits = CreateGroup()
    private group CameraTargetUnits = CreateGroup()
    private real array CameraRotate
    private real array CameraDistance
    private boolean array CameraLeft
    private boolean array CameraRight
    private boolean array CameraFront
    private boolean array CameraBack
    private boolean array CameraDisable
endglobals

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

private function CameraRemovePlayerMain_function takes nothing returns nothing
    if GetOwningPlayer(GetEnumUnit()) == bj_forceRandomCurrentPick then
        call GroupRemoveUnit(CameraMainUnits, GetEnumUnit())
        call GroupRemoveUnit(CameraTargetUnits, GetEnumUnit())
    endif
endfunction

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

function ATPCRemovePlayerMain takes player p returns nothing
    set bj_forceRandomCurrentPick = p
    call ForGroup(CameraMainUnits, function CameraRemovePlayerMain_function)
    call ForGroup(CameraTargetUnits, function CameraRemovePlayer_function)
    if GetLocalPlayer() == p then
        call ResetToGameCamera(1.5)
    endif
endfunction

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

function ATPCAddMainUnit takes unit u returns nothing
    local player p = GetOwningPlayer(u)
    call ATPCRemovePlayerMain(p)
    call GroupAddUnit(CameraMainUnits, u)
    set p = null
endfunction

function ATPCAddUnit takes unit u returns nothing
    local player p = GetOwningPlayer(u)
	local integer i = GetPlayerId(p)
    call ATPCRemovePlayer(p)
	set CameraRotate<i> = 0
	set CameraDistance<i> = CameraMaxDistance
    call GroupAddUnit(CameraTargetUnits, u)
    set p = null
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 CameraRotate<i> = 0
        set CameraDistance<i> = CameraMaxDistance
        call GroupAddUnit(CameraTargetUnits,  GetEnumUnit())
    endif
endfunction

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

private function StopActions takes nothing returns nothing
    set bj_isUnitGroupDeadResult = true
    call ForGroup(CameraTargetUnits, function usesCamera)
    if bj_isUnitGroupDeadResult then
        call ForGroup(CameraMainUnits, function CameraOn)
        set CameraDisable[GetPlayerId(GetTriggerPlayer())] = false
    else
        call ForGroup(CameraTargetUnits, function CameraOff)
        if GetLocalPlayer() == GetTriggerPlayer() then
            set CameraDisable[GetPlayerId(GetTriggerPlayer())] = true
            call ResetToGameCamera(1.5)
        endif
    endif
endfunction

private function CameraAdjust takes nothing returns nothing
    local unit u = GetEnumUnit()
    local location l = Location(0,0) 
    local real x
    local real y
    local real z
    local real x1
    local real y1
    local real z1
    if GetLocalPlayer() == GetOwningPlayer(u) then
        set CameraDistance[GetPlayerId(GetOwningPlayer(u))] = 25 
        set x = GetUnitX(u)
        set y = GetUnitY(u)
        call MoveLocation(l,x,y)
        set z = GetLocationZ(l)
        set x1 = x + 64 * Cos((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
        set y1 = y + 64 * Sin((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
        call MoveLocation(l,x1,y1) 
        set z1 = GetLocationZ(l)
        loop
        set x1 = x - CameraDistance[GetPlayerId(GetOwningPlayer(u))] * Cos((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
        set y1 = y - CameraDistance[GetPlayerId(GetOwningPlayer(u))] * Sin((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
        if ( (IsTerrainWalkable(x1, y1) or IsTerrainDeepWater(x1, y1) or GetUnitFlyHeight(u) &gt; 0) and (CameraDistance[GetPlayerId(GetOwningPlayer(u))] &lt; CameraMaxNow[GetPlayerId(GetOwningPlayer(u))]) ) then
                    set CameraDistance[GetPlayerId(GetOwningPlayer(u))] = CameraDistance[GetPlayerId(GetOwningPlayer(u))] + 25
                else
                    exitwhen true
        endif 
        endloop
        call PanCameraToTimed(GetUnitX(u), GetUnitY(u), Period)
        call SetCameraField(CAMERA_FIELD_ROTATION, GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))], Period)
        call SetCameraField(CAMERA_FIELD_ZOFFSET, GetUnitFlyHeight(u) + z + CameraHeight + GetCameraField(CAMERA_FIELD_ZOFFSET) - GetCameraTargetPositionZ(), -Period)
        call SetCameraField(CAMERA_FIELD_ZOFFSET, GetUnitFlyHeight(u) + z + CameraHeight + GetCameraField(CAMERA_FIELD_ZOFFSET) - GetCameraTargetPositionZ(), Period)
        call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, CameraAngle + ((z1 - z) * 0.5), 0.32) 
        call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, CameraDistance[GetPlayerId(GetOwningPlayer(u))], Period)
    endif
    call RemoveLocation(l)
    set u = null
    set l = null
endfunction

private function Actions takes nothing returns nothing
    call ForGroup(CameraTargetUnits, function CameraAdjust)
endfunction

private function Death takes nothing returns nothing
    if IsUnitInGroup(GetTriggerUnit(), CameraTargetUnits) then
        call ATPCRemove(GetTriggerUnit())
    endif
endfunction

private function KeyLeft takes nothing returns nothing
    set CameraLeft[GetPlayerId(GetTriggerPlayer())] = not CameraLeft[GetPlayerId(GetTriggerPlayer())]
endfunction
private function KeyRight takes nothing returns nothing
    set CameraRight[GetPlayerId(GetTriggerPlayer())] = not CameraRight[GetPlayerId(GetTriggerPlayer())]
endfunction
private function KeyUp takes nothing returns nothing
    set CameraFront[GetPlayerId(GetTriggerPlayer())] = not CameraFront[GetPlayerId(GetTriggerPlayer())]
endfunction
private function KeyDown takes nothing returns nothing
    set CameraBack[GetPlayerId(GetTriggerPlayer())] = not CameraBack[GetPlayerId(GetTriggerPlayer())]
endfunction

private function DoKeys takes nothing returns nothing
    local integer i = 0
    loop
    	if CameraLeft<i> then
            set CameraRotate<i> = CameraRotate<i> + 8
            if CameraRotate<i> &gt; 360 then
                set CameraRotate<i> = CameraRotate<i> - 360
            endif
    	endif
    	if CameraRight<i> then
            set CameraRotate<i> = CameraRotate<i> - 8
            if CameraRotate<i> &lt; -360 then
                set CameraRotate<i> = CameraRotate<i> + 360
            endif
    	endif
        if CameraFront<i> then
            if CameraMaxNow<i> &gt; CameraMinDistance + 25 then
                set CameraMaxNow<i> = CameraMaxNow<i> - 25
            endif
        endif
        if CameraBack<i> then
            if (CameraMaxNow<i> &lt; CameraMaxDistance - 25) then
                set CameraMaxNow<i> = CameraMaxNow<i> + 25
            endif
        endif
    	set i = i + 1
    	exitwhen i &gt;= 12
    endloop
endfunction

private function UnitCheck takes nothing returns nothing
    local unit c = GetEnumUnit()
    local boolean d = false
    if IsUnitInGroup(c, CameraMainUnits) and (d == false) then
        set d = true
        call ATPCAddUnit (c)
    endif
endfunction

private function Selector takes nothing returns nothing
    local group Selected
    if CameraDisable[GetPlayerId(GetTriggerPlayer())] == false then
        set Selected = GetUnitsSelectedAll(GetTriggerPlayer())  
        if (IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true) and CountUnitsInGroup(Selected) == 1 then
            call ATPCAddMainUnit (GetTriggerUnit())
        endif
        call ForGroup(Selected, function UnitCheck)
        if IsUnitInGroup(GetTriggerUnit(), CameraTargetUnits) == false then
            call ATPCAddUnit (GetTriggerUnit())
        endif
        call DestroyGroup (Selected)
        endif
endfunction
    
private function Init takes nothing returns nothing
    local integer i //Player index
    local trigger t = CreateTrigger()
    local trigger t1 = CreateTrigger()
    local trigger t2 = CreateTrigger()
    local trigger t3 = CreateTrigger()
    local trigger t4 = CreateTrigger()
    local timer ct //For the rotation
    local player p
    call TriggerRegisterTimerEventPeriodic(t, Period)
    call TriggerAddAction(t, function Actions)
    if CameraAllowEscape then
        set t = CreateTrigger()
        call TriggerAddAction(t, function StopActions)
        set i = 0
        loop
            call TriggerRegisterPlayerEventEndCinematic( t, Player(i) )
            set i = i + 1
            exitwhen i &gt;= 12
        endloop
    endif
    if CameraNotDead 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
    set t = CreateTrigger()
    call TriggerAddAction(t, function Selector)
    set i = 0
    loop
        call TriggerRegisterPlayerSelectionEventBJ( t, Player(i), true )
        set i = i + 1
        exitwhen i &gt;= 12
    endloop
    call TriggerAddAction(t1, function KeyLeft)
	call TriggerAddAction(t2, function KeyRight)
    call TriggerAddAction(t3, function KeyUp)
    call TriggerAddAction(t4, function KeyDown)
	set i = 0
    loop
		set CameraRotate<i> = 0
		set CameraDistance<i> = CameraMaxDistance
		set CameraLeft<i> = false
		set CameraRight<i> = false
        set CameraFront<i> = false
        set CameraBack<i> = false
        set CameraDisable<i> = false
        set CameraMaxNow <i> = CameraMaxDistance
		set p = Player(i)
		if CameraAllowArrows 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_UP_DOWN)
			call TriggerRegisterPlayerEvent(t3, p, EVENT_PLAYER_ARROW_UP_UP)
            call TriggerRegisterPlayerEvent(t4, p, EVENT_PLAYER_ARROW_DOWN_DOWN)
			call TriggerRegisterPlayerEvent(t4, p, EVENT_PLAYER_ARROW_DOWN_UP)
		endif
		set i = i + 1
		exitwhen i &gt;= 12
	endloop
    set ct = CreateTimer()
	call TimerStart(ct, 0.05, true, function DoKeys)
	set ct = null
    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 once again repeat that you need vJASS to use it
FAQ
Q - Does it work in multiplayer?
A - Yes it does
Q - Does it leak?
A - Nope
Q - Did you test it?
A - Yes i did test it more than once
Q - How does it avoid other objects?
A - If you are standing in front of a tree the camera will try to avoid the tree so that you are able to see normaly
Q - What do you mean by "changes the angle"?
A - If you are climbing a hill you will be able to see its top no matter the angle of the side
Here is the test map:
View attachment TheLegend's TPC.w3x
 

TheLegend

New Member
Reaction score
10
an important update to the system.
The players camera will no longer be adjusted if it is disabeled
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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