Page 1 of 1

How to rotate a vector?

Posted: Wed Mar 08, 2017 1:26 am
by Barbie
While modding a map for MH I run into the task to create a pinball ball that is just flying (linear) around in a walled area, being reflected at walls and damaging players if that ball hits him. To give the trajectory a bit randomness I'd like to change the direction (but not the length) of the resulting velocity vector after a wall hit. For now I've been using this, but I'm sure there is an easier way:

Code: Select all

var(Movement) int BounceVariance;

simulated function float VaryValue(float Value, int Vary) {
	return (1 - 2 * fRand()) * Vary + Value;
}

simulated function HitWall(vector HitNormal, actor Wall) {
local rotator r;
[... code of Mover(Wall).bDamageTriggered left out ...]
	if (BounceVariance != 0)
	{
		r = rotator(HitNormal);
		r.yaw = VaryValue(r.yaw, BounceVariance);
		r.pitch = VaryValue(r.pitch, BounceVariance);
		r.roll = VaryValue(r.roll, BounceVariance);
		HitNormal = Normal(vector(r));
	}
	Velocity = MirrorVectorByNormal(Velocity, HitNormal);
}