开发者

XNA: I have a 1*1*1 Box and want to know from which side i am clicking on it. (Minecraft Clone)

I am currently creating a Minecraft clone (a game where you are in a 3d space and create or delete blocks to create structures) So I have got nearly everything work that i need. If i look at a Block it gets highligthed the way i want it to, and if i call "RemoveBlock()" with a rightclick the currently looked at block is being removed. But now i also want to add blocks by clicking leftclick.

So what i have is: AddBlock(Vector3 LookingAt, Vector3 Direction) where "LookingAt" is being correctly related to the 3dArray in where i store my Blockdata. and the Direction is either Vector3.UnitX/Z/Y and the negatives. If i manually say that Direction = Vector3.UnitY, then a block above the LookedAt block will be added. which is what i want.

But how do i figure out from which side I am looking at the Block. ie. determin Direction: what i've tried is to create 6 Spheres around around the LookedatBlock and from CameraPossition i drew a Ray to CameraDirection and then tried to find out which开发者_Python百科 Sphere it collides with first. the 6 speres have their centers always in the middle of each side of the cube, and a radius of 0.5f. This makes sense to me. But it doesn't work. What can I do to get this working.

I need the result being some kind of Vector3.UnitX/Y/Z so that i can use it with my other methods.

thank you.


I would imagine that you could determine which side you're clicking is on by checking the X, Y, and Z values and finding what they are from the origin Vector of the cube.

For example, say the top-back-left of a 100 pixel cube is a Vector3 with (0,0,0) (relative to the cube, of course). Well, if you click on the bottom-front-right of the cube, the coordinates would be (100, 100, 100), right?

Using that, you could simply try to find the bounds of a square on the cube. For example, the six sides would be between the following vectors:

Side 1: (0,0,0), (100,0,0), (0,0,100), and (100,0,100) Side 2: (0,100,0), (100,100,0), (0,100,100), and (100,100,100)

Those are the top and bottom sides of the cube, respectively, using an XYZ coordinate system and picking a point to start from. If I messed up on one of the coordinates, forgive me, but they should be about right.

So you could determine where you click on the cube, pinpoint the Vector3 of where you clicked to figure out what side it is on by simply finding (okay, if this side's value is (i, 100, j)...).

Does any of that make sense? I'm assuming that when you click the cube, some Vector is returned.

Also, it might help to add a debugging tool that shows what vector your mouse is currently pointed at, etc?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜