Player View Coordinates

Please post any questions about developing your plugin here. Please use the search function before posting!
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Player View Coordinates

Postby Predz » Sun Aug 24, 2014 2:30 pm

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. :)
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Aug 24, 2014 3:00 pm

Yes, you would want to use trace rays. :)
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/packages/source-python/engines/trace.py

But i think it would make sense to add this functionality to the PlayerEntity.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Aug 27, 2014 4:02 am

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.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Wed Aug 27, 2014 11:22 am

Just had some time to work on this. Here you are :smile:

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
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Wed Aug 27, 2014 1:38 pm

Nice work! :)
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Postby Predz » Thu Aug 28, 2014 11:47 pm

Thank you, I will have a read through this to try get a grasp of how trace rays are used. :D

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 131 guests