Is there anyway of obtaining a player's view coordinates at the moment or am I required to do some trigonometry and use trace rays?
Thanks in advance.

Syntax: Select all
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python
import math
# Source.Python
from engines.trace import engine_trace
from engines.trace import Ray
from engines.trace import GameTrace
from engines.trace import TraceType
from engines.trace import TraceFilterSimple
from mathlib import Vector
from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid
# =============================================================================
# >> CONSTANTS
# =============================================================================
# TODO: Expose this from C++
MAX_COORD_RANGE = 16384
# =============================================================================
# >> FUNCTIONS
# =============================================================================
# TODO: Add this to PlayerEntity
def get_view_coordinates(player, ignore=(), trace_type=TraceType.EVERYTHING):
"""Returns the coordinates the player is currently looking at.
@param <ignore>:
An iterable of entity indexes to ignore. The trace will not hit these
entities.
@param <trace_type>:
Defines the trace type."""
# Get the eye location of the player
start_vec = get_eye_location(player)
# Calculate the greatest possible distance
end_vec = start_vec + get_view_vector(player) * MAX_COORD_RANGE
# Create a new trace object
trace = GameTrace()
# Start the trace
engine_trace.trace_ray(
Ray(start_vec, end_vec),
0x1, # This value doesn't matter. Our trace filter ignores it
TraceFilterSimple(set((player.index,) + tuple(ignore)), trace_type),
trace
)
# Return None if we haven't git anything. Else return the position where
# the trace stopped
return trace.end_position if trace.did_hit() else None
# TODO: Add this to PlayerEntity
def get_eye_location(player):
'''
Returns the eye location of the player.
'''
return Vector(*tuple(player.get_prop_float(
'm_vecViewOffset[{0}]'.format(x)) + y for x, y in \
enumerate(player.location)))
# TODO: Add this to PlayerEntity
def get_view_vector(player):
'''
Returns the view vector of the player
'''
eye_angle0 = player.get_prop_float('m_angEyeAngles[0]')
eye_angle1 = player.get_prop_float('m_angEyeAngles[1]')
return Vector(
math.cos(math.radians(eye_angle1)),
math.sin(math.radians(eye_angle1)),
-1 * math.sin(math.radians(eye_angle0))
)
# =============================================================================
# >> TEST
# =============================================================================
@Event
def player_say(event):
userid = event.get_int('userid')
index = index_from_userid(userid)
player = PlayerEntity(index)
view_coord = get_view_coordinates(player)
print(tuple(view_coord))
Return to “Plugin Development Support”
Users browsing this forum: Bing [Bot] and 132 guests