每像素碰撞 - 可与一些通用的技巧做 [英] Per Pixel Collision - Could do with some general tips

查看:113
本文介绍了每像素碰撞 - 可与一些通用的技巧做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解其背后的每像素碰撞在XNA理论,你从两个纹理纹理数据,然后对它们进行比较,检查,例如,你的人物纹理alpha值。

I understand the theory behind Per Pixel Collision in Xna, you get the texture data from two textures, and then compare them, checking the alpha values of, for example, your character texture.

什么我想要做的是使用每像素碰撞检查,如果玩家已经雪碧通过检查一种特定的颜色(黑色)碰撞感动地图。我已经试过之前实现这一点,但人物总是通过与不扔编程错误地图下降。
请点我朝着文章/教程/视频,或者如果在这里任何人都可以给我一些建议,并指出这将是巨大的。

What I'm wanting to do is use Per Pixel Collision to check if the Player Sprite has touched the map by checking for collisions with a certain colour (Black). I've tried implementing this before, but the characters always falls through the map with no programming errors thrown. Please point me towards articles/tutorials/videos, or if anybody here could give me some tips and points that would be great too.

推荐答案

我也不会检查像素到像素两个纹理之间,如果我是你。检查只是一个点VS地图是不是太糟糕,虽然;

I would not check pixel to pixel between two textures, if I were you. Checking just one point vs a map is not too bad, though;

//In Class-scope:
Color[] CollisionMapData;
Texture2D CollisionMap;

public void LoadContent()  
{  
    CollisionMap = Content.Load<Texture2D>("map");  
    CollisionMapData = new Color[CollisionMap.Width * CollisionMap.Height];  
    CollisionMap.GetData<Color>(CollisionMapData);  
}  

public Boolean Collision(Vector2 position)  
{  
    int index = (int)position.Y * CollisionMap.Width + (int)position.X;

    if (index < 0 || index >= CollisionMapData.Length) //Out of bounds  
        return true;

    if (CollisionMapData[index] == Color.Black)
        return true;

    return false;
}

要核对地图整个玩家精灵,你就必须调用碰撞法在玩家精灵的每个像素,创造一个vector2得到正确的点。
这是一个更容易或许检查几个点(例如,左上,topmiddle,topright,左,右,BOTTOMLEFT,bottommiddle,bottomright不需要检查中间,因为双方都已经被测试。)

To check the entire player-sprite against a map, you would have to call the Collision-method for each pixel in the players sprite, creating a vector2 to get the right point. It is a lot easier to check maybe a few points (for instance; topleft, topmiddle, topright, left, right, bottomleft, bottommiddle, bottomright. No need to check middle because sides are already being tested.)

这篇关于每像素碰撞 - 可与一些通用的技巧做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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