# Object coming from far away effect in 2D

I have a path made using segments of sine wave interpolation put together that is drawn flat on the screen. And I would like to give the path the effect of coming towards the player. I tried to draw what I want to do, left is what I have now and right is the desired effect:

so the "farther" away the end of the segment is the smaller it's scale is and the segments are closer together. I think this would give the effect I'm looking for but I'm not sure how to do it.

This is the code I use to create a segment. It keeps track of the last point of the segment so the next segment can start from there.

private void createSegment() {
int g = r.Next(i, i + 240);
for (float j = 0; j < 1f; j += 0.01f) {
startY--;
}
lastX = g;
}

• Do you want draw a path that looks like this or do you need pseudo 3D in your game? In other words; will game characters move along the path? Jan 19 '19 at 19:08
• You can animate this by frames. Or use scale to change the size.
– Omer
Jan 19 '19 at 19:40
• I did manage to scale the points accordingly like in the image I posted. The bigger problem for me is how to give the effect of the wave unraveling as it comes towards the bottom of the screen. I'm thinking maybe a shader with a bump map (I think that's what it's called)? But I barely have any experience with shaders but if you guys tell me it would work than I would work on that. Also nothing will be on the path so that's not an issue. Jan 19 '19 at 19:49
• A shader and bumpmap don't sound like the solution here. My question still remains- if you just want to draw a sine wave that becomes wider as you get closer to the bottom of the screen, simply scale it- if you want pseudo 3D, you need to approach it differently. Jan 19 '19 at 22:13
• In this case you can use "Sprite Mask" and make it show the path from far to near screen.
– Omer
Jan 20 '19 at 19:49

I will make some assumptions about your function, that you need to call it twice, once for both the left and and right hand sides of your road. For what you are doing, you are close.

private void createSegment(float desiredWidth, float direction, List<Vector2> pointsList) {
int g = r.Next(i, i + 240);
for (float j = 0; j < 1f; j += 0.01f)
{
float modifier = desiredWidth * j * direction;
pointsList.Add(new Vector2(CosineInterpolate(lastX, g, j), startY) + new Vector2(modifier, 0));
startY--;
}
lastX = g;
}


I'm going to assume that you will have 2 lists that will represent the left and right hand sides. And that as you iterate through creating your list of points. Just a heads up, you can use any sort of scaling in the modifier variable. You may want to add some small exponential calculation if you want to make it look more 3d.

as mentioned, i've made some assumptions about what you're trying to achieve. If this isn't a fit, then all ok! GL

• Your assumptions are very correct and this is definitely very helpful! Thank you. Jan 22 '19 at 22:01