PHYS_Trailer and rotations

Discussions about Coding and Scripting
Post Reply
1337GameDev
Skilled
Posts: 198
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

PHYS_Trailer and rotations

Post by 1337GameDev »

I have an actor that will handle effects or other stuff, and I want it to be attached to another actor. I use PHYS_Trailer as a basis for this.

I use this:

Code: Select all

//Given a target actor, attach ourselves to it, and align to the Z axis of the target
function AttachToActor(Actor Target) {
        local Vector X, Y, Z;
	local Rotator DesiredRotation;
	//get the x,y,z axis as vector directions
	GetAxes(Target.Rotation, X, Y, Z);
	
	
	Self.SetOwner(Target);
	Self.SetPhysics(PHYS_Trailer);
	//ensure we are offset by a fixed amount from the owner
	Self.bTrailerPrePivot = true;
	
	//ensure we dont use the owner's rotation
	Self.bTrailerSameRotation = false;
	//offset us slightly higher than the origin of the target -- 16 units up on the Z axis
	Self.PrePivot = (Z * 16);
	
	//get the rotation to align to the target Z normal
	DesiredRotation = Rotator(Z);
	
	//set our rotation to align TOWARDS (or Z will point in the same direction) the positive Z axis of the target
	Self.SetRotation(DesiredRotation);
}
Only, it doesn't seem to set the rotation (or it does nothing, despite being set) and always stays sideways....
I even tried setting a timer, and calling this and inverting the Z axis vector I get, every other tick, and no change....

Ideas? am I missing something?
User avatar
ExpEM
Adept
Posts: 298
Joined: Wed Nov 09, 2016 1:48 am

Re: PHYS_Trailer and rotations

Post by ExpEM »

PHYS_Trailer can be really funky to work with if you want it to do anything more than the most basic stuff.
Have you overlooked bTrailerSameRotation per chance?

I use this to avoid PHYS_Trailer:

Code: Select all

// Snippet from CyberDragon
MyBits[I].MyEffect.SetLocation(MyBits[I].Location + (MyBits[I].EffectOffset >> MyBits[I].Rotation));
Simplified abit in psudo code:

Code: Select all

Actor_A.SetLocation(Actor_B.Location + (AttachOffset >> Actor_B.Rotation));
Actor_A.SetRotation(DoRotationStuffHere);

Where:
Actor_A is attached to Actor_B.
AttachOffset is a vector.
Signature goes here.
1337GameDev
Skilled
Posts: 198
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Re: PHYS_Trailer and rotations

Post by 1337GameDev »

ExpEM wrote: Tue Aug 17, 2021 11:01 pm PHYS_Trailer can be really funky to work with if you want it to do anything more than the most basic stuff.
Have you overlooked bTrailerSameRotation per chance?

I use this to avoid PHYS_Trailer:

Code: Select all

// Snippet from CyberDragon
MyBits[I].MyEffect.SetLocation(MyBits[I].Location + (MyBits[I].EffectOffset >> MyBits[I].Rotation));
Simplified abit in psudo code:

Code: Select all

Actor_A.SetLocation(Actor_B.Location + (AttachOffset >> Actor_B.Rotation));
Actor_A.SetRotation(DoRotationStuffHere);

Where:
Actor_A is attached to Actor_B.
AttachOffset is a vector.
Yeah, in the above code, I set bTrailerSameRotation to false...

Hmm, do you do that code in an Tick function? Isn’t the inefficient and why PHYS_Trailer was invented?

And my rotation is set in my attached actor.... it’s just not reflected....

I log output the rotation before, the desired rotation, and the rotation after setting.... and it appears to set properly, but doesn’t affect the attached actor?
User avatar
ExpEM
Adept
Posts: 298
Joined: Wed Nov 09, 2016 1:48 am

Re: PHYS_Trailer and rotations

Post by ExpEM »

Yes it runs in tick, I know I know frowned upon and all that, but hardly an issue on any pc from the last decade.
Like I said, PHYS_Trailer can be funky. If I get the time I'll have a play around and see what I can get working.
Signature goes here.
User avatar
sektor2111
Godlike
Posts: 6410
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: PHYS_Trailer and rotations

Post by sektor2111 »

PHYS_Trailer is part of SLV mods. I don't know if it's that inefficient - maybe not perfect. Also Chaos uses it at Vortex.
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: PHYS_Trailer and rotations

Post by Feralidragon »

It's pretty much like ExpEM said.

The PHYS_Trailer is good enough for actors you want to attach which either have the same rotation as their owner (bTrailerSameRotation) or which always have a constant rotation (which is mostly used to attach rotation-irrelevant actors like sprites instead of meshes).

Furthermore, PrePivot is absolute and not relative to the rotation (as far as I remember), so if you set something like:
PrePivot.Z = 100;
it will be offset in the Z axis by 100 units regardless of the rotation.

This of course could be solved by something like:

Code: Select all

var vector RelativePrePivot;

simulated event Tick(float Delta)
{
	PrePivot = RelativePrePivot >> Owner.Rotation;
}
For reference, so you understand the above, this type of operation:
NewVector = Vector >> Rotation
rotates the Vector using the given Rotation, and this is actually equivalent to:

Code: Select all

GetAxes(Rotation, X, Y, Z);
NewVector = Vector.X*X + Vector.Y*Y + Vector.Z*Z;
so using the >> operator is often simpler and preferable.
1337GameDev
Skilled
Posts: 198
Joined: Thu Apr 16, 2020 3:23 pm
Personal rank: GameDev

Re: PHYS_Trailer and rotations

Post by 1337GameDev »

Yeah, I desired a fixed offset, and then rotate my trailing actor around ITS origin.

I just had issues with the rotation (for the trailer itself) actually showing up (it renders as (0,0,0) rotation).

Hmm.

I would prefer not to use a tick update function if possible to try and stick to native functionality for support for trailing actors....
Post Reply