c#相机用于六边形网格 [英] c# camera for a hexagonal grid

查看:82
本文介绍了c#相机用于六边形网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们先说谢谢你花时间阅读我的问题:)。



我是应用计算机科学的学生,所以我的经验不是''太棒了至于现在我正在尝试制作游戏。它将采用4x游戏风格。我有一个六边形网格。我想用它作为棋盘,玩地图,每个六边形都是一个位置。感谢Jeff Modzel和他的源项目(http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1)。它让我对六角形网格背后的数学很感兴趣,因为我非常厌倦数学。



六边形可以有单位,行星或什么都没有。但我遇到的问题是如何制作可以在地图上滚动的相机。我从来没有做过这样的事情,也找不到任何东西。大多数事情都指向XNA。



我希望有人可以告诉我大行的原则或者引用我的指南或解释链接。



我希望它成为2D游戏(自上而下的战略观点)所以我不认为团结对我有任何好处。但如果你知道一个好的引擎可以帮助我这个请告诉:)。



非常感谢,罗宾

Let's start with saying thank you for taking the time to read my question :).

I am a student in applied computer science and so my experience isn't too great. As for now I am trying to make a game. It will be in the 4x game style. I have an hexagonal grid. I want to use that as the board, playing map, where each hexagon is a position. I got this thanks to the thread of Jeff Modzel and his source project ( http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1 ). It helped me a LOT with the math behind a hexagonal grid, as I quite suck at math.

An hexagon can have units on it, planets or nothing. But the problem I am having is how to make a camera that can scroll over the map. I have never done such a thing and I can't find anything. Most things point me to XNA.

I am hoping someone can tell me the principle in big lines or refer me to some links with guides or explanations.

I want it to be a 2D game (topdown strategic view) so I Don't think unity will be of any good to me. But if you know of a good engine that can help me with this please do tell :).

Much thanks, Robin

推荐答案

最简单的方法是使用摄像机视图区域的所需尺寸绘制网格。然后当你移动相机时,只计算在假想位置上可见的物体。



我给你留下你用六边形库做的样本(只是添加4个按钮以移动相机):



The simplest way is drawing your grid with the desired dimensions of the "view area" of the camera. Then when you "move the camera" just calculate what objects are visible on that "imaginary position".

I leave you a sample i did with your hexagon library (just add the 4 buttons for move the camera):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using Hexagonal;
using System.Diagnostics;

namespace HexagonalTest
{
	public partial class TestForm : Form
	{

		Hexagonal.Board board;
		Hexagonal.GraphicsEngine graphicsEngine;
        List<gridobject> objetos = new List<gridobject>();
        int camX;
        int camY;

		public TestForm()
		{
			InitializeComponent();

            //draw a fixed grid
	        Board board = new Board(8, 8, 25, HexOrientation.Flat);
            board.BoardState.BackgroundColor = Color.Green;
            board.BoardState.GridPenWidth = 2;
            board.BoardState.ActiveHexBorderColor = Color.Red;
            board.BoardState.ActiveHexBorderWidth = 2;
            this.board = board;
            this.graphicsEngine = new GraphicsEngine(board, 20, 20);


            //add some objects (planets, enemies, etc)
            objetos.Add(new GridObject(2, 4, Color.Blue));
            objetos.Add(new GridObject(10, 5, Color.Red));


            this.CalcCamera();
		}

        private void CalcCamera()
        { //this calculates what objects are currently visible to the camera
            foreach (var item in board.Hexes)
            {
                item.HexState.BackgroundColor = Color.White;
            }

            int x; int y;
            foreach (var item in this.objetos)
            {
                x = item.x - this.camX;
                y = item.y - this.camY;
                if (x >= 0 && x <= 7 && y >= 0 && y <= 7)
                    board.Hexes[y, x].HexState.BackgroundColor = item.color;
            }
            
       }

        //nothing changed
	private void Form_Paint(object sender, PaintEventArgs e)
	{
		//Draw the graphics/GUI

		foreach (Control c in this.Controls)
		{
			c.Refresh();
		}

		if (graphicsEngine != null)
		{
			graphicsEngine.Draw(e.Graphics);
		}

		//Force the next Paint()
		this.Invalidate();

	}
        //nothing changed
	private void Form_Closing(object sender, FormClosingEventArgs e)
	{
		if (graphicsEngine != null)
		{
			graphicsEngine = null;
		}

		if (board != null)
		{
			board = null;
		}
	}

        public class GridObject
        { //sample object (ship, planet, etc)
            public int x;
            public int y;
            public Color color;

            public GridObject(int x, int y, Color color)
            {
                this.x = x;
                this.y = y;
                this.color = color;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        { //up
            this.camY--;
            this.CalcCamera();
            this.Invalidate();
        }

        private void button4_Click(object sender, EventArgs e)
        { //down
            this.camY++;
            this.CalcCamera();
            this.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        { //left
            this.camX--;
            this.CalcCamera();
            this.Invalidate();
        }

        private void button2_Click(object sender, EventArgs e)
        { //right
            this.camX++;
            this.CalcCamera();
            this.Invalidate();
        }
		
	}
}


游戏编程中最复杂的部分可能是图形生成。一个普通显示器只能显示所谓的平面图像或2D图像。在此上下文中的一个2D图像是使场景符合一个平面(相机视图)的三维对象的投影。根据此投影平面与场景的XYZ平面之间的关系,您有一个2D,3D,第一人称等。



从3D到2D的投影涉及一个数学和图形生成理论的地狱。仅举两个例子:看看光线跟踪 [ ^ ]和光线投射 [ ^ ]。



也许,因为数学似乎不是你的主要兴趣领域,你最好直接使用这种几乎可以使用的技术之一:直接3D(直接X) [ ^ ](不是那么容易)使用,但仍然很强大, XNA游戏工作室 [ ^ ](支持有限的跨平台,似乎目前已停止使用), Unity [ ^ ](三者中最灵活的,有很好的跨平台支持,几乎可以做你想做的任何事情),OpenTK [ ^ ](从来没有试过这个,只知道它存在)和另一个2D引擎是对偶性 [ ^ ]。



您将找到大量有关它们的示例代码和教程。您可以使用C#进行任何这些技术的编程,但DirectX的设计考虑了C / C ++编程。



在我的情况下,我只是玩游戏,使用.Net Remoting为我的儿子制作2D下沉舰队游戏(所以我们使用两台计算机玩)。我让所有的场景渲染得很艰难。但是对于下一步行动,我决定使用Unity。
Probably the most complex part of games programming is graphics generation. One ordinary display can only show what is called a flat image, or 2D image. One 2D image in this context is the projection of the tri-dimensional objects that conform the scene into one plane (the camera view). Depending on the relation between this projection plane and the X-Y-Z planes of the scene you have one 2D, 3D, first person, etc.

The projection from 3D to 2D involves a hell of mathematics and graphics generation theory. Just two examples: take a look into Ray tracing[^] and Ray Casting[^].

Perhaps, as math seems not being your main interest area, you better jump directly into using one of this almost-ready-to-use technologies: Direct 3D (Direct X)[^] (not that easy to use, but still powerfull), XNA Game Studio[^] (with limited cross-plattforms support and seems being currently discontinued), Unity[^] (the most flexible among the three, with great cross-plattform support, you can do almost whatever you want), OpenTK[^] (never tried this, just know it exists) and another 2D engine is Duality[^].

You'll find plenty of example code and tutorials about them. You can use C# for programming for any of the these technologies, but DirectX was designed with C/C++ programming in mind.

In my case I was only playing around, making a 2D "sink the fleet game" for my son using .Net Remoting (so we played using two computers). I made all the scene rendering the hard way. But for the next move I decided to use Unity.


这篇关于c#相机用于六边形网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆