Setting Player view direction (c++) Created 2 years ago2021-10-26 06:35:54 UTC by Lafis Lafis

Created 2 years ago2021-10-26 06:35:54 UTC by Lafis Lafis

Posted 2 years ago2021-10-26 06:35:54 UTC Post #346003
Hey everyone,

Was messing around with the HL source code for the first time and was digging around in the weapons coding. I managed to modify the amount of view kick the weapon has, which was easy enough, but I got completely hung up on setting a permanent adjustment to the player's viewing angle, rather than just a temporary camera animation. I put in a break point on:

m_pPlayer->pev->v_angle;

and that seems to have the relevant angles, but modifying this vector doesn't seem to have any effect. Anyone got any ideas on where I'm going wrong?
Posted 2 years ago2021-10-26 07:09:12 UTC Post #346004
You gotta set m_pPlayer->pev->fixangle to 1 if you wanna force view angles with pev->v_angle.
Admer456 Admer456If it ain't broken, don't fox it!
Posted 2 years ago2021-10-26 08:11:26 UTC Post #346005
Think I might be misunderstanding this still. I've set fixangle to 1, which definitely is now allowing the view angle to be modified in some way (thanks!), but I'm getting some odd results.

In weapon firing function
m_pPlayer->pev->fixangle = 1;
m_pPlayer->pev->v_angle.x += 5;

My naïve assumption would be that I would grab the current view x direction and add (or subtract) an arbitrary offset to it. The effect of this in-game however seems to be that it alternates between positive and negative X angles (above / below the horizon line), slowly averaging out to a zero X angle each time the firing function is called.
Posted 2 years ago2021-10-26 10:01:22 UTC Post #346006
I think it's more or less the result of the client fighting the server. Client is like "Hey, I'm looking at this angle", and the server is like "No no, you're looking at THIS angle".

If you really wanna modify the view angles, you're way, waaaaay better off doing it on the clientside (hl_cdll project). What is it specifically that you want to achieve? I'm guessing some kinda custom recoil, but I could be wrong.

Edit: oh right, permanent view adjustments. So it's gonna be like punchangle, except it doesn't fall back to the original angles.
That's gonna be a bit more involved, but it's still doable on the clientside, thanks to something called weapon events. You'll find them in ev_hldm.cpp.

I'd provide some example code, but it's been a while so I don't remember how to force update view angles on the client. :|
Either way, you might also wanna take a look at view.cpp (don't be frightened by the messy code; V_CalcRefdef is the function you might wanna look at)
Admer456 Admer456If it ain't broken, don't fox it!
Posted 2 years ago2021-10-26 13:54:32 UTC Post #346007
Thanks so much! Got it working.

It's probably not the best method, since I'm extremely unfamiliar with C++, but here's how I ended up doing it:

Define new Vector in view.cpp
Vector ev_permpunchangle;

Create a new function in view.cpp
void V_PermPunchAxis(int axis, float punch) {
ev_permpunchangle[ axis ] = punch;
}

Reference it and use it in ev_hldm.cpp
void V_PermPunchAxis(int axis, float punch);
...
V_PermPunchAxis(0, -5.0);

Add the vectors to cl_viewangles within view.cpp (just stuck this in under the other view kick parts)
VectorAdd ( pparams->cl_viewangles, (float *)&ev_permpunchangle, pparams->cl_viewangles);
*ev_permpunchangle = (0.0, 0.0, 0.0);

Had to zero out the permpunchangle after using it though as otherwise it'd constantly re-add the values and vertically rotate the camera endlessly. Interestingly enough, that still happens for horizontal movement, so I'll need to look into that some more. I also initially made the mistake of trying to add the vector to viewangles, which instead of moving your actual aim point, instead just adds a permanent offset between your aim point and the camera view center.

Thanks again for the help! Definitely would've got stuck on trying to do it server-side.
Posted 2 years ago2021-10-26 16:33:51 UTC Post #346008
"unfamiliar with C++"
It's less about knowing the language, more about knowing HL SDK. I think it's an okay solution, given the messy nature of the SDK.
view.cpp itself has a lot of "C-style" code, so I wouldn't even call it C++.

But, semantics and philosophy aside, I'm glad you got the desired effect.

Edit: Though i'm curious why you're doing *ev_permpunchangle and (float*)&ev_permpunchangle. You're not using a pointer to ev_permpunchangle, are you? The code would be cleaner if you did it differently, such as using a reference in the function parameters (Vector&) or something like that.
Admer456 Admer456If it ain't broken, don't fox it!
Posted 2 years ago2021-10-26 17:48:57 UTC Post #346009
As someone who's main experience with programming comes from Javascript, I honestly mainly just looked at how stuff like this: VectorAdd ( pparams->viewangles, (float *)&ev_punchangle, pparams->viewangles); was set up and tried to mimic it just to get it to compile properly, I'm not really familiar with working with pointers. Reading up on the * operator though I think I have a better idea of why that's odd.

I figured diving into the HL SDK and attempting to change some stuff would be interesting to try though!
Posted 2 years ago2021-10-26 18:23:54 UTC Post #346010
I'm asking cuz' VectorAdd should work fine with vectors.
#define VectorAdd(a,b,c) \
{ \
   (c)[0]=(a)[0]+(b)[0]; \
   (c)[1]=(a)[1]+(b)[1]; \
   (c)[2]=(a)[2]+(b)[2]; \
}
Since the Vector class has the operator [ ], you can access its XYZ components with [0], [1] and [2] respectively, thus, VectorAdd will work perfectly fine.
Vector myVector = Vector( 20, 40, 50 );
// Alternatively: Vector myVector{ 20, 40, 50 };
VectorAdd( pparams->viewangles, myVector, pparams->viewangles );
The only reason you'd use * and (float*)& might be due to something like this:
void MyFunction( Vector* someParam )
{
   someParam->z += 20.0f;
}
...
Vector myVector{ 0, 0, 0 };
MyFunction( &myVector );
But I'd rather not do that. I'd rather pass the vector by reference instead of passing a pointer to it:
void MyFunction( Vector& someParam )
{
   someParam.z += 20.0f;
}
...
Vector myVector{ 0, 0, 0 };
MyFunction( myVector );
That way, you don't need to use pointer syntax. ^^
Admer456 Admer456If it ain't broken, don't fox it!
You must be logged in to post a response.