I'm sure this is a very simple problem but I'm totally stumped!
I'm doing shadows by flattening a black-coloured copy of my player's mesh onto the ground. I use a LinePick to get the position and normal of the ground point where my player is standing, then I use PositionEntity and AlignToVector to position and rotate my shadow on the ground.
Next I set the Roll angle of the shadow to the Yaw value of the player. This makes the shadow sideways on to the ground when the player is sideways on to the light source that is casting the shadow.
However I then have to scale the shadow to be flat on the ground and that has me stumped. I use a scale of 1.2, 1.2, 0.05, but when I rotate my shadow with the player's Yaw, this must obviously be adjusted so the shadow is scaled to 0.05 in the direction of the Global Y-axis and 1.2 on the X and Z axes.
I've tried just about every combination of TFormVector I can think of and none work. The closest I have got to a solution is some simple trigonometry, but it only works at -180, -90, 0, 90, and 180 degree rotations - angles in between cause problems.
Here's the code (with my trig scaling solution):
pThis = Player type instance
pThis\EN = Player mesh
pThis\ShadowEN = Player shadow mesh
At 90 degree rotation intervals for the player mesh it works beautifully, but at anything in between (37 degrees, -52 degrees, etc.) the shadow is not flattened to the ground - it sticks up at an angle and goes through the ground, because the scale is not correct.
Heeeeeeeelp! *pulls hair out*
I'm doing shadows by flattening a black-coloured copy of my player's mesh onto the ground. I use a LinePick to get the position and normal of the ground point where my player is standing, then I use PositionEntity and AlignToVector to position and rotate my shadow on the ground.
Next I set the Roll angle of the shadow to the Yaw value of the player. This makes the shadow sideways on to the ground when the player is sideways on to the light source that is casting the shadow.
However I then have to scale the shadow to be flat on the ground and that has me stumped. I use a scale of 1.2, 1.2, 0.05, but when I rotate my shadow with the player's Yaw, this must obviously be adjusted so the shadow is scaled to 0.05 in the direction of the Global Y-axis and 1.2 on the X and Z axes.
I've tried just about every combination of TFormVector I can think of and none work. The closest I have got to a solution is some simple trigonometry, but it only works at -180, -90, 0, 90, and 180 degree rotations - angles in between cause problems.
Here's the code (with my trig scaling solution):
Result = LinePick(EntityX#(pThis\EN), EntityY#(pThis\EN), EntityZ#(pThis\EN), 0, -1000.0, 0)
If Result
PositionEntity pThis\ShadowEN, PickedX#(), PickedY#() + 3.0, PickedZ#()
AlignToVector pThis\ShadowEN, PickedNX#(), PickedNY#(), PickedNZ#(), 2
Yaw# = EntityYaw#(pThis\EN)
RotateEntity pThis\ShadowEN, EntityPitch#(pThis\ShadowEN), 0.0, Yaw#
TurnEntity pThis\ShadowEN, -90.0, 0.0, 0.0
MoveEntity pThis\ShadowEN, 0.0, MeshHeight#(pThis\EN) / 2.0, 0.0
; FIXME
XScale# = Abs(Sin#(Yaw#)) * 0.05 : XScale# = XScale# + (1.2 * ((0.05 - XScale#) * 20.0))
ZScale# = Abs(Cos#(Yaw#)) * 0.05 : ZScale# = ZScale# + (1.2 * ((0.05 - ZScale#) * 20.0))
ScaleEntity pThis\ShadowEN, XScale#, 1.2, ZScale#
EndIf
pThis = Player type instance
pThis\EN = Player mesh
pThis\ShadowEN = Player shadow mesh
At 90 degree rotation intervals for the player mesh it works beautifully, but at anything in between (37 degrees, -52 degrees, etc.) the shadow is not flattened to the ground - it sticks up at an angle and goes through the ground, because the scale is not correct.
Heeeeeeeelp! *pulls hair out*