碰撞检测? [英] Collision Detection?

查看:98
本文介绍了碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试制作一个简单的游戏,我有一个代表用户角色的图片框和一个背景图片框.但是我需要找出一种简单的方法来避免发生碰撞,以便用户无法穿过树木和建筑物等.做这样的最简单的方法是什么?


http://aldez.freeforums.org/ http://sites.google.com/site/aldezrpg/

Hi everyone,

I am trying to make a simple game, and i have a picturebox that represents the users character and a picturebox for the background. But i need to figure out an easy way for there to be some kind of collision so the user cant run through trees and buildings etc. What would be the easiest way of doing something like this?


http://aldez.freeforums.org/ http://sites.google.com/site/aldezrpg/

推荐答案

您的任务相当广泛.例如2D或3D图像.顶视图或透视图.我猜是2D.我认为Nobugz在这里回答了您的问题. > http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/406b2555-d615-4b9d-924f-409d3854ab63

该建议目前似乎不可行.我认为您了解最好使用辅助方法将任务和职责委托给辅助类.

我曾经建议您创建一个为您的2D环境建模的类.模型视图控制器.这个模型就发挥了作用.您的表格不是您的模型.它是您模型的视图,就像您的眼睛注视着您周围的环境一样.您的表单显示了模型的视图.

好的,您使用图像表示环境,然后使用滚动图像.该模型可以让您定义角色有效或无效动作的区域.想象一下棋盘.棋盘的模型将是一个定义每个图块的图块的类.

在此示例中,每个图块都可以包含一个或多个游戏单元或棋子.模型应包含在给定图块上作为参数传递的给定坐标XY处创建,添加,移除,处理等单位的方法. Point对象可以轻松定义一对XY坐标.

板模型应该仅跟踪环境中的单位位置.对模型的更改将触发一个事件,该事件将更新表单/视图或眼睛在模型中看到的内容.国际象棋类可以从该模型继承,并且可以在其中占据一个给定单元以上的多个单元.

但是,如何跟踪模型中的哪些地方有效以及哪些地方无效.查找值的真值表非常简单.但是,模型中所有可能的XY位置的表可能会很大.同样,这是模型的来源.如果单元试图移动到新的图块,请检查图块是否被禁止占领的单元所占据.

图块本身甚至可以是微型2D包含自己的子图块的模型,允许在图块中移动.至于模型/拼贴"方案中的碰撞检测,只需在与图像对应的正确位置处使用单位初始化模型即可.

希望这会有所帮助.永远不用担心重写工作"的任务.代码.它通常可以使您免于头痛之苦...像这样.

Rudedoǵ= 8 ^ D
Your queston is fairly broad.  2D or 3D images for example.  Overhead view, or perspective view.  I guess 2D.  I think nobugz answered your question here.

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/406b2555-d615-4b9d-924f-409d3854ab63

That advice might seem unworkable right now.  I think you understand that it is best to delegate tasks and responsiblities to helper classes with helper methods. 

I once suggested that you create a class that models your 2D enviroment.  A Model-View-Controller.  This where that model comes into play.  Your form is not your model.  It is a view of your model, just like your eye looks at the enviroment around you.  Your form presents a view of the model.

Okay, you use an image to represent the enviroment, and a scrolling image at that.  The model can allow you to define areas that are valid or invalid moves for your characters.  Imagine a chessboard.  A model of a chessboard would be a class that defined each of the Tiles one the board. 

Each tile could contain one or more game units, or chess pieces in this example.  The model should contain methods to Create, Add, Remove, Dispose, etc, of units on a given Tile at given set of coordinates XY passed as parameters.  A Point object could easily define a pair of XY coordinates.

The board model should merely keep track of unit locations within the enviroment.  Changes to the model should fire an event that that updates the form/view, or what the eye sees in the model.   A chess class could inherit from the model, and this where more than one unit from occupying a given could be implemented. 

But, how to keep track of where in the model is valid, and what is invalid.  A truth table of lookup values is quite simple.  But, a table of all possible XY positions within the model could be rather large.  Again, this is where the model comes in.  If a unit tries to move to a new tile, check to see if the tile is occupied by a unit that prohibits occupation.

The tiles themselves could even be miniature 2D models containing their own sub-tiles, permitting movement within a tile.  As for collision detection in the Model/Tile scenario, simply initialize your model with units at the proper locations that correspond to your image.

Hope this helps.  Never fear the task of re-writing "working" code.  It usually saves you from headaches down the road...like this one.

Rudedog  =8^D


这篇关于碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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