Math to have a bullet penetrate stuff in 2D?

Before I ask my question here is an example of a 2D grid of pixels, showing a bullets travel and the pixels it hits, like I want to happen:

35719-answersexample.png

So I have a 2D game where I can blow up parts of the world, spray pixels onto the world, and I want to implement bullet penetration (for like rifles) where the player shoots at some angle, the bullet collides with the terrain (checking against a texture2d’s alpha values) and then looks like it digs into the terrain by removing about ten pixels in the same direction the bullet was moving. So yeah problem is the way I faked it was having 16 possible angles to dig a line in the terrain based on the velocity (x and y) of the bullet when it collides - but that doesn’t look good enough, and I would need many many cases (if velocity x > velocity y amount n then dig n some direction) to cover more angles and be more realistic.

How could I code it to work like my image suggests, measuring all the pixels that would be hit, within ten pixels, from a given point, with the velocity on x and y, and collision x and y point given?

PS the velocity ends up being high on a bullet, it reports usually more than 1k in velocity on x and y values, showing how fast it was moving at collision time after effects of gravity and such… so trying to use that number to my advantage requires normalizing or something I guess… I am pretty lost what to try guys! Any suggestions for what to try to do instead of hardcoding a pile of generic angles, and instead like, mathematically deciding which pixels to remove?

Sorry for all the vague questions lately guys haha.

I think for this you could use the [Bresenham’s line algorithm][1]. (This method is used to draw a line between to points and can be used for this)

In this example, I used [this][2] code as a start, and then changed it to better suit our needs.

    private static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; }

    /// <summary>
    /// Returns the pixels that this ray will intersect.
    /// </summary>
    /// <param name="endPoint">The direction we are going</param>
    /// <param name="amount">This is the maximum amount of pixels we will find</param>
    /// <returns></returns>
    public List<Vector2> GetNextPoints(Vector2 direction, int amount)
    {
        List<Vector2> returnValue = new List<Vector2>(amount);


        int x0 = 0; // Start x
        int y0 = 0; // Start y

        int x1 = Mathf.RoundToInt(direction.x * amount); // End X
        int y1 = Mathf.RoundToInt(direction.y * amount); // End y

        bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0);
        if (steep) { Swap<int>(ref x0, ref y0); Swap<int>(ref x1, ref y1); }
        if (x0 > x1) { Swap<int>(ref x0, ref x1); Swap<int>(ref y0, ref y1); }
        int dX = (x1 - x0), dY = Mathf.Abs(y1 - y0), err = (dX / 2), ystep = (y0 < y1 ? 1 : -1), y = y0;

        for (int x = x0; x <= x1; ++x)
        {
            if (steep)
                returnValue.Add(new Vector2(y, x));
            else
                returnValue.Add(new Vector2(x, y));

            err = err - dY;
            if (err < 0)
            {
                y += ystep;
                err += dX;
            }
        }
        return returnValue;
    }

Calling it would be something like this:

    public void Start()
    {
        List<Vector2> result = GetNextPoints(new Vector2(3, 1).normalized , 10);

        for (int i = 0; i < result.Count; i++)
        {
            GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = result*;*

}
}
Note that this code doesn’t always return the next ‘amount’ of pixels, but all crossed pixels between 0,0 and ‘direction * amount’ and the amount of crossed pixels could be less then ‘amount’, depending on the angle.
[1]: Bresenham's line algorithm - Wikipedia
[2]: Bresenham's Line Algorithm - RogueBasin