WIP SHMUP : Learn basics

A place for people with an interest in developing new shmups.
User avatar
9uile
Posts: 50
Joined: Mon Jan 13, 2020 4:57 pm

Re: WIP SHMUP : Learn basics

Post by 9uile »

I have some jitters in my project.
Can you help me ?

https://forum.unity.com/threads/scrolli ... st-6283940
pieslice
Posts: 200
Joined: Sat Mar 01, 2008 12:15 pm
Location: Finland

Re: WIP SHMUP : Learn basics

Post by pieslice »

The "jitters" in the video you posted in the Unity thread are scrolling artefacts caused by the low resolution scrolling.
For ultra smooth low res scrolling you need your scroll speed per pixels to be multiple of your screen refresh rate, otherwise it will always look a bit jittery.

Common trick for parallax layers is to scroll at half speed to the previous layer.

Achieving this in Unity needs just some basic math.
* Extract the pixel distance from your tile size

-> scroll speed is pixel distance * speed * frame rate

Speed should be 1/x or interger value for faster than 1 pixel per frame scrolling.
Other values will work but the outcome will always look a bit jittery
User avatar
9uile
Posts: 50
Joined: Mon Jan 13, 2020 4:57 pm

Re: WIP SHMUP : Learn basics

Post by 9uile »

Thanks a lot for your answer.
I modified my scrolling script attached to my camera like this :

Code: Select all

public class Scrolling : MonoBehaviour
{
    public float SpeedX;
    public float SpeedY;
    public float pixelsPerUnit;
    //public bool alignHalfPixelsOff_XAxis;
    //private bool alignHalfPixelsOff_YAxis;

    public bool FixJitters; //launch a built to see the result (may still have jitters in editor)


    void Update()
    {
        transform.Translate(SpeedX * Time.deltaTime, SpeedY * Time.deltaTime, 0);

        if (FixJitters)
        {
            int i_x = Mathf.FloorToInt(transform.localPosition.x * (float)pixelsPerUnit);
            int i_y = Mathf.FloorToInt(transform.localPosition.y * (float)pixelsPerUnit);
            int i_z = Mathf.FloorToInt(transform.localPosition.z * (float)pixelsPerUnit);
            //Vector3 oldPos = transform.localPosition;
            Vector3 p = new Vector3((float)i_x / (float)pixelsPerUnit, (float)i_y / (float)pixelsPerUnit, (float)i_z / (float)pixelsPerUnit);
            //float halfSize = (1.0F / (float)pixelsPerUnit) * 0.5F;
            //if (alignHalfPixelsOff_XAxis){p.x += halfSize;}
            //if (alignHalfPixelsOff_YAxis){p.y += halfSize;}
            transform.localPosition = p;
        }
    }
}


There are no more jitters now, like you can see here :
https://twitter.com/9uileTweet/status/1 ... 58304?s=20

I don't understand why but when SpeedX is below 4, there was a lot of jitters and below 3.5 the scrolling don't move ! weird isn't it ?:)

Now, i'll have to test it in my game project to see the result.

Thank you !
pieslice
Posts: 200
Joined: Sat Mar 01, 2008 12:15 pm
Location: Finland

Re: WIP SHMUP : Learn basics

Post by pieslice »

That's pretty simple.
You apply the movement as a lossy format. At low-enough speeds the sub pixel correction is stronger than the movement so the translation gets overridden by the pixel correction

You should have the scroll offset stored to a field which is independent on the pixel grid.

Then you apply the pixel-correct scroll offset as a translation.
User avatar
9uile
Posts: 50
Joined: Mon Jan 13, 2020 4:57 pm

Re: WIP SHMUP : Learn basics

Post by 9uile »

I modify my code with this :

I store the X offset like this :
scrolloffsetX = oldPos.x - p.x;

and then add this offset in this line :
transform.position = transform.position + new Vector3((SpeedX * Time.deltaTime) + scrolloffsetX, SpeedY * Time.deltaTime, 0);

Now, the scrolling occurs with SpeedX values below '4' but it wasn't fluid even with higher values than '4'.

I put a link to the demo project if you want to see (you need to built the project because results can be weird in the editor):
https://www.dropbox.com/s/fc7ma3ew5q626 ... 9.zip?dl=0

Code: Select all

 void Update()
    {
        //transform.Translate(SpeedX * Time.deltaTime, SpeedY * Time.deltaTime, 0);
        transform.position = transform.position + new Vector3((SpeedX * Time.deltaTime) + scrolloffsetX, SpeedY * Time.deltaTime, 0);
        
        if (FixJitters)
        {
            Vector3 oldPos = transform.localPosition;

            int i_x = Mathf.FloorToInt(transform.localPosition.x * (float)pixelsPerUnit);
            int i_y = Mathf.FloorToInt(transform.localPosition.y * (float)pixelsPerUnit);
            int i_z = Mathf.FloorToInt(transform.localPosition.z * (float)pixelsPerUnit);

            Vector3 p = new Vector3((float)i_x / (float)pixelsPerUnit, (float)i_y / (float)pixelsPerUnit, (float)i_z / (float)pixelsPerUnit);
            
            //float halfSize = (1.0F / (float)pixelsPerUnit) * 0.5F;
            // if (alignHalfPixelsOff_XAxis){p.x += halfSize;}
            //if (alignHalfPixelsOff_YAxis){p.y += halfSize;}
            
            scrolloffsetX = oldPos.x - p.x;
            
            transform.localPosition = p;
        }
    }
User avatar
waiwainl
Posts: 464
Joined: Tue Jun 24, 2014 9:23 am
Location: Netherlands

Re: WIP SHMUP : Learn basics

Post by waiwainl »

Did you consider to also rotate the ship/fire direction when you move x/y axis?
Visit: https://shmu.ps
Experience the National Video Game Museum in the Netherlands
User avatar
9uile
Posts: 50
Joined: Mon Jan 13, 2020 4:57 pm

Re: WIP SHMUP : Learn basics

Post by 9uile »

Yes, but right now my issue is just the scrolling jitters. :)
Post Reply