In my HL2 single player mod, I have to determine if the entity the player's looking is the entity I expect.
Example:
I have 2 entities: A and B.
If the player is looking at entity A, do something, otherwise (this is for example looking at entity B) do something else with entity A.
How can I do that? :smile:
I hope I've created the thread in the correct section. If not, I apologize for my dumbness... :rolleyes:
EDIT: solved, but now I have another problem: how can I convert CBasePlayer to CBaseEntity?
Not sure if you can use raycasts in source because I have never used source before. But that is what I would do in other engines.
Thank you anyway, but I've just solved this problem. :smile:
Now I have another... :rolleyes:
[QUOTE=FrontBack;40561777]EDIT: solved, but now I have another problem: how can I convert CBasePlayer to CBaseEntity?[/QUOTE]
You just use it as one, CBasePlayer [I]is a[/I] CBaseEntity.
[QUOTE=FrontBack;40562038]Thank you anyway, but I've just solved this problem. :smile:
Now I have another... :rolleyes:[/QUOTE]
You should post your solution, because when someone in 5 years googles this thread with the exact same problem, he will be disappointed...
[img]http://imgs.xkcd.com/comics/wisdom_of_the_ancients.png[/img]
Not a programming answer, but Hammer does have this: [URL="https://developer.valvesoftware.com/wiki/Trigger_look"]Trigger_Look[/URL].
Maybe that will sort of help the next poor soul to find this thread?
Actually, no. I must make all this in C++... :(
[editline]10th May 2013[/editline]
[QUOTE=DrLuckyLuke;40573506]You should post your solution, because when someone in 5 years googles this thread with the exact same problem, he will be disappointed...
[img]http://imgs.xkcd.com/comics/wisdom_of_the_ancients.png[/img][/QUOTE]
It turned out that my solution isn't working. :rolleyes:
You should use UTIL_TraceLine :
[url]https://developer.valvesoftware.com/wiki/UTIL_TraceLine[/url]
[QUOTE=FrontBack;40595442]Actually, no. I must make all this in C++... :([/QUOTE]
But you can take a look at the code for this trigger.
[QUOTE=FrontBack;40561777]
EDIT: solved, but now I have another problem: how can I convert CBasePlayer to CBaseEntity?[/QUOTE]
You use a cast. Helpfully, there's a ToBasePlayer utility function that can do this for you.
there is, but for most cases, you could just
[QUOTE=Dienes;40573373]use it as one, CBasePlayer [I]is a[/I] CBaseEntity.[/QUOTE]
YEAH!
I've found the solution! :dance:
Look at this:
It's almost useless to say, but "this" is CBaseEntity.
The function returns true if the entity is watched and false if it's not.
[CODE]
CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
if(!pPlayer)
{
Warning("Could not determine calling player!\n");
return false;
}
trace_t trace;
UTIL_TraceLine(this->GetAbsOrigin(), pPlayer->GetAbsOrigin(), CONTENTS_SOLID | CONTENTS_OPAQUE | CONTENTS_MOVEABLE, this, COLLISION_GROUP_NONE, &trace);
Vector lookdir = pPlayer->EyeDirection3D();
Vector targetdir = this->GetAbsOrigin()-pPlayer->EyePosition();
float dot = DotProduct(lookdir, targetdir);
if (dot > pPlayer->GetFOV() && trace.fraction > 0.98f)
{
return true;
}
else
{
return false;
}
[/CODE]
There ya go :)
Casts are your friends.
[QUOTE=SteveUK;40596465]You use a cast.[/QUOTE]
[QUOTE=Duskling;40604975]Casts are your friends.[/QUOTE]
What casts? He wants to use derived as a base, in which case you don't need any casts. He is not even casting anything in the code he posted.
[QUOTE=Dienes;40605134]What casts? He wants to use derived as a base, in which case you don't need any casts. He is not even casting anything in the code he posted.[/QUOTE]
My answer came before. He didn't give any clues so that was the most obvious answer.
[QUOTE=FrontBack;40604672]YEAH!
I've found the solution! :dance:
Look at this:
It's almost useless to say, but "this" is CBaseEntity.
The function returns true if the entity is watched and false if it's not.
[/QUOTE]
You don't even need to use "this->" unless there's a name conflict. For example "this->GetAbsOrigin()" could just be "GetAbsOrigin()"
Sorry, you need to Log In to post a reply to this thread.