Monogame中伪3D道路效果异常问题求助
伪3D道路效果异常修复及示例请求
我正在尝试在Monogame中实现伪3D道路效果,已完成道路直行及循环功能,但添加曲线和山坡后出现两个异常:
- 道路下方线条过渡不流畅
- 道路被大幅拉伸(此前长度显示正常)
当前异常代码
public void Draw() { carX += Basic.elapsedTime * 5; float vertical = Basic.height; float drawDistance = 1f; while (drawDistance < roadLength) { float lastVertical = vertical; while (vertical >= lastVertical && drawDistance < roadLength) { drawDistance += drawDistance / 150; float x = carX + drawDistance; float scale = 1 / drawDistance; //hills float z = (float)(200 + 80 * Math.Sin(x / 13) - 120 * Math.Sin(x / 7)); vertical = (int)(60 + Basic.height * scale + z * scale); //hills if (drawDistance < roadLength) { //curve float y = (float)(200 * Math.Sin(x / 17) + 170 * Math.Sin(x / 8)); float horizontal = (Basic.width / 2) - ((roadSize.X / 2) - y) * scale; //curve Rectangle roadSlice = new Rectangle(0, (int)(100*x % roadSize.Y), (int)roadSize.X, 1); Basic.spriteBatch.Draw(Textures.road, new Vector2(horizontal, vertical), roadSlice, Color.White, 0.0f, Vector2.Zero, new Vector2(scale, 1), SpriteEffects.None, 0); } } } }
异常效果截图

添加山坡前的正常代码
public Track() { roadSize = new Vector2(Textures.road.Width, Textures.road.Height); roadLength = roadSize.Y / 3.0f; } public void Draw() { for (int i = 0; i < roadLength; i++) { float scale = ((roadLength - i) / roadLength) * 2; float x = carX + i / scale; //curve float y = (float)(200 * Math.Sin(x / 1170) + 170 * Math.Sin(x / 580)); float horizontal = (Basic.width / 2) - ((roadSize.X / 2) - y) * scale; //curve float vertical = Basic.height - i; Rectangle roadSlice = new Rectangle(0, (int)(x % roadSize.Y), (int)roadSize.X, 1); Basic.spriteBatch.Draw(Textures.road, new Vector2(horizontal, vertical), roadSlice, Color.White, 0.0f, Vector2.Zero, new Vector2(scale, 1), SpriteEffects.None, 0); } }
问题
- 如何修复上述异常,实现道路线条的平滑过渡?
- 是否有基于纹理切片的Monogame/C#伪3D道路示例?
内容的提问来源于stack exchange,提问作者Canox
相关产品推荐
相关产品推荐

