Third Person Camera system error

TheLegend

New Member
Reaction score
10
Hello users of TH.net. I have a huuuge problem with my third person camera system in multiplayer mode. The thing is that the camera system works only for the host and all other players get "kicked" from the game... Im open for all sorts of suggestions. oh and here is the system :)
JASS:

//The Camera System Library

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 = 1600
    private real array CameraMaxNow
    private constant real CameraMinDistance = 100
    //Booleans to control the options of the camera
    private constant boolean CameraAllowArrows = true
    private constant boolean CameraAllowEscape = true
    private constant boolean CameraNotDead = true
    public boolean CameraEnable = true
    //If you want to disable the camera just set CameraEnable = false
    //Dont tuch anything here
    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
    public boolean array CameraDisable
    private boolean array CameraPaused
    private boolean array PreviousSett
endglobals

private function ATPCRemove takes unit u returns nothing
    call GroupRemoveUnit(CameraTargetUnits, u)
    call ResetToGameCamera(1.5)
endfunction

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

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

private 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(CameraTargetUnits, function CameraOn)
        set CameraDisable[GetPlayerId(GetTriggerPlayer())] = false
    else
        call ForGroup(CameraTargetUnits, function CameraOff)
        set CameraDisable[GetPlayerId(GetTriggerPlayer())] = true
        call ResetToGameCamera(1.5)
    endif
endfunction

public function CinematicPause takes player p returns nothing
    call ResetToGameCamera(1.5)
    if CameraPaused[GetPlayerId(p)]==true then
        set CameraDisable[GetPlayerId(p)] = PreviousSett[GetPlayerId(p)]
        set CameraPaused[GetPlayerId(p)] = false
        call ATPCAddUnit(udg_camera_main_unit[GetPlayerId(p)])
    else
        set PreviousSett[GetPlayerId(p)] = CameraDisable[GetPlayerId(p)]
        set CameraDisable[GetPlayerId(p)] = true
        call ATPCRemove(udg_camera_main_unit[GetPlayerId(p)])
        call ATPCRemovePlayer(p)
        set CameraPaused[GetPlayerId(p)] = true
    endif
endfunction

private function CameraAdjust takes nothing returns nothing
    local unit u = GetEnumUnit()
    local location l = Location(0,0)
    local real v = 25
    local real angleadj = 0
    local real x
    local real y
    local real z
    local real x1
    local real y1
    local real z1
    local real z2
    local real backupz = 0
    if GetLocalPlayer() == GetOwningPlayer(u) then
        set CameraDistance[GetPlayerId(GetOwningPlayer(u))] = CameraMinDistance
        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))] + 5
                else
                    exitwhen true
        endif 
        endloop
        set angleadj = CameraAngle + (z1 - z) * 0.5
        loop
            exitwhen v &gt; (CameraDistance[GetPlayerId(GetOwningPlayer(u))] + 150) == true
            set x1 = x - v * Cos((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
            set y1 = y - v * Sin((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
            call MoveLocation(l,x1,y1) 
            set z2 = GetLocationZ(l)
            if angleadj &gt;= 360 then
                set angleadj = angleadj - 360
            endif
            if angleadj &lt; 0 then
                set angleadj = angleadj + 360
            endif
            if (v * Sin(angleadj)) &lt; (z2 + CameraHeight + GetUnitFlyHeight(u)) then
                set v = v + 5
            else
                set angleadj = angleadj - 10
            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) + backupz + z + CameraHeight + GetCameraField(CAMERA_FIELD_ZOFFSET) - GetCameraTargetPositionZ(), -2*Period)
        call SetCameraField(CAMERA_FIELD_ZOFFSET, GetUnitFlyHeight(u) + backupz + z + CameraHeight + GetCameraField(CAMERA_FIELD_ZOFFSET) - GetCameraTargetPositionZ(), 2*Period)
        call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, angleadj, 0.32) 
        call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, CameraDistance[GetPlayerId(GetOwningPlayer(u))], 2*Period)
        call SetCameraTargetController(u, 0, 0, false )
    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 Selector takes nothing returns nothing
    local group Selected
    if (CameraDisable[GetPlayerId(GetTriggerPlayer())] == false) and (IsUnitType(GetTriggerUnit(), UNIT_TYPE_STRUCTURE) == false) then
        set Selected = GetUnitsSelectedAll(GetTriggerPlayer())  
        if IsUnitInGroup(GetTriggerUnit(), CameraTargetUnits) == false then
            call ATPCAddUnit (GetTriggerUnit())
        endif
        call GroupClear(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
    if CameraEnable == true then
        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 CameraPaused<i> = false
            set CameraMaxNow<i> = CameraMaxDistance
            set PreviousSett<i> = false
            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)
    endif
    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></i>



oh and not to forget if you want to test the map there is another library for the pathing
JASS:

//The Terrain Pathing Library

library TerrainPathability initializer Initialization
globals
    private constant real    MAX_RANGE     = 10.
    private constant integer DUMMY_ITEM_ID = &#039;wolg&#039;
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 &lt;= 0
        set HidMax = HidMax - 1
        call SetItemVisible(Hid[HidMax], true)
        set Hid[HidMax] = null
    endloop
    return (X-x)*(X-x)+(Y-y)*(Y-y) &lt;= 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
 

UndeadDragon

Super Moderator
Reaction score
447
Moved to JASS Help. Much more likely to get looked at there.
 

TheLegend

New Member
Reaction score
10
hmmm ill look at the tuts thanx for the advice... ill check and repport back if it works or not
 

TheLegend

New Member
Reaction score
10
ok will my system desync the players if i move
if GetLocalPlayer() == GetOwningPlayer(u) then
to the very mottom of the camera adjust so that it affects onyl the camera setting and not the calculating
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Why don't you do the calculations and then cache them in to a [ljass]camerasetup[/ljass] and after that apply it in a if GetLocalPlayer() ==.. block?

I think that by changing handles (call MoveLocation(l,x,y)) inside a if GetLocalPlayer() == ... block is the cause of desyncs.
 

TheLegend

New Member
Reaction score
10
all the calculations are instant, at that moment. The section
JASS:
        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))] + 5
                else
                    exitwhen true
        endif 
        endloop

checks the pathing map behind the player unit which is important for avoiding objects blocking your sight and the section
JASS:
        set angleadj = CameraAngle + (z1 - z) * 0.5
        loop
            exitwhen v &gt; (CameraDistance[GetPlayerId(GetOwningPlayer(u))] + 150) == true
            set x1 = x - v * Cos((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
            set y1 = y - v * Sin((GetUnitFacing(u) + CameraRotate[GetPlayerId(GetOwningPlayer(u))]) * bj_DEGTORAD)
            call MoveLocation(l,x1,y1) 
            set z2 = GetLocationZ(l)
            if angleadj &gt;= 360 then
                set angleadj = angleadj - 360
            endif
            if angleadj &lt; 0 then
                set angleadj = angleadj + 360
            endif
            if (v * Sin(angleadj)) &lt; (z2 + CameraHeight + GetUnitFlyHeight(u)) then
                set v = v + 5
            else
                set angleadj = angleadj - 10
            endif
        endloop

will adjust the angle of your cam in order to avoid the camera getting stuck in the ground of even you looking at the ground so both actions have to be completed at the same time and corresponding to the units location in the world
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
My point was that you only need those inside the if GetLocaPlayer() == GetOwningPlayer(u) blcok:

 

TheLegend

New Member
Reaction score
10
i did that already read the 3 posts above your suggestion... the players dont get kicked anymore but the camera lags so hard
 
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