Page 1 of 1
Player View Coordinates
Posted: Sun Aug 24, 2014 2:30 pm
by Predz
Hey.
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.

Posted: Sun Aug 24, 2014 3:00 pm
by Ayuto
Posted: Wed Aug 27, 2014 4:02 am
by satoon101
I agree that we should add this to PlayerEntity. I am not sure when any of us devs will be able to look into this. So, if anyone outside of the development team wishes to work on this, please feel free and post a pull request once you have it figured out. If not, we will get to it when we can.
Posted: Wed Aug 27, 2014 11:22 am
by Ayuto
Just had some time to work on this. Here you are
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))
It will require this class.
https://github.com/Source-Python-Dev-Team/Source.Python/commit/e85a5966797c60554604493558be19bce373a668
Posted: Wed Aug 27, 2014 1:38 pm
by L'In20Cible
Nice work!

Posted: Thu Aug 28, 2014 11:47 pm
by Predz
Thank you, I will have a read through this to try get a grasp of how trace rays are used.
