游戏开发之二维向量角度计算

最近做android游戏开发,遇到一些问题,这里先说向量角度,其用处很大也很广泛。比如飞机当前飞向的方向、怪兽出击时候方向等都需要及时计算出其方向和位置。
这里介绍一种常见游戏:切水果所用到的方向确认。
一个香蕉飞出来,你滑动手指去击中它时候,香蕉就被切成两半。由于,香蕉不像西瓜是圆的,而是长条行的,所以基本有两种把香蕉切断的情况 。横着切,竖着切。那怎么去计算到底是横着切还是竖着切呢!
这里 就要计算出 表示香蕉的向量和你出击的效果的方向的向量 这两个向量的夹角。用这个夹角来判断是否是横着切 还是竖着切。

下面是代码:
 public static float TO_DEGREES = ((180/(float)Math.PI));

//计算向量的旋转的角度0-360
public static int get_vector_Radian (float vector_x, float vector_y){
float lenth = (float) lineDis(0, 0, vector_x, vector_y);
int degree = 0;
        
        if(DEBUG)Log.v("yuyongjun", "get_vector_Radian 9999999999999  vector_x="+vector_x +"vector_y="+vector_y);
if(vector_x > 0 && vector_y >= 0)
{
degree =  (int) (Math.asin(vector_y/lenth) * (TO_DEGREES));
}
else if(vector_x <= 0 && vector_y > 0)
{
degree = (int) (Math.asin(vector_y/lenth) * (TO_DEGREES));
degree = 180 - degree;

else if(vector_x < 0 && vector_y <= 0)
{
degree = (int) (Math.asin((-vector_y)/lenth) * (TO_DEGREES));
degree += 180;
}
else
{
degree = (int) (Math.asin((-vector_y)/lenth) * (TO_DEGREES));
degree = 360 - degree;
}
if(DEBUG)Log.v("yuyongjun", "get_vector_Radian 9999999999999  degree="+degree);
return degree;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章