When trying to get a sun lamp to follow the player in an open world game, the shadows cast by the sun shake. I am pretty sure that this is because the shadows are calculated from the sun position instead of the camera’s. Is there any way to reduce this shadow shaking effect?
you could try raising the resolution of the the shadow. is the sun parented via vertex?
Yes, the sun is a vertex parent. I tried doubling the resolution, but the effect is still noticeable.
setting the sun to variance shadows didn’t seem to help much either. I think perhaps a python solution, where whenever the player moves a certain number of units on the X or Y axis, the sun “jumps” to the player’s location.
This is a normal behavior. It happens on every engine and it’s because of the shadow map resolution. Don’t set the resolution beyond 1024, specially if your graphics card isn’t too good.
I don’t have shaking shadows due to lag.
to replicate this - spawn a sun, turn on shadows, set resolution to 1024, rotate on z and x so the shadows are cast at an angle and put it over a plane with some cubes. Than use G(shift z) and move the lamp around. The shadows jitter because the sun lamp is moving.
I’m guessing Shadow bias issue.
A .blend will be helpful.
You can do this for the sun:
from mathutils import Vector
player = ... # Get player object here
sun = ... #Get sun object here
p = player.worldPosition
relpos = Vector((0, 8, 32)) # Position from sun relative to player
sun.worldPosition = Vector((int(p.x), int(p.y), int(p.z))) + relpos
This should work better than vertex parent:)
I think* this happens when the sun’s position is update/move at every frame, you can curb it by some extent with slow parent or only update the sun’s position at certain threshold in python
No. It’s caused because sun texture is moved. Imagine the sun texture being a decal. However, it’s rendered real-time. This makes it jitter, cause the pixels change in motion. If you move sun by a distance equal to number that can be divided by the pixel’s size without floating, than it won’t do it any more because pixels will seem to not to move.
Thanks for the code, but can you tell me how to use it?
I used an always sensor, which I connected to a python controller which used “Test.py”. I than entered the names of of the objects as strings - “Player” and “Lamp” … and it didn’t work. Did I set it up wrong? (I’m a total noob at anything dynamically typed - Python, GDScript, ect. I have a strong programming knowledge of Java and C# though)
Oh, you may run it like this:
from mathutils import Vector
def sun(cont)L
sun = cont.owner
player = sun.scene.objects["Player"]
p = player.worldPosition
relpos = Vector((0, 8, 32)) # Position from sun relative to player
sun.worldPosition = Vector((int(p.x), int(p.y), int(p.z))) + relpos
And you run it in modulus mode like this:
Test.sun