求旋轉矩形的四個頂點坐標,用來做旋轉矩形的碰撞檢測


 

package com.example.com.android.test;

import java.util.Vector;import java.lang.Math;

import android.util.Log;public class MySprite { private float mRotation = 0; private float mWidth = 10; private float mHeight = 20; private CCPoint mPosition = new CCPoint(); private final static double PI = 3.1415926; private Vector<CCPoint> mConstFourPoints = new Vector<CCPoint>(4);  private Vector<CCPoint>  mFourPoints = new Vector<CCPoint>(4);  MySprite() {  initConstPoints(); } MySprite(float rotation,float width,float height) {//  mPosition.m_fPositionX = 10;//  mPosition.m_fPositionY = 20;  initRotatedSpriteRecet(rotation,width,height); }  public void setWidth(float wth) {  mWidth = wth; }  public void setHeight(float height) {  mHeight = height; }  public float getWidth() {  return mWidth; }  public float getHeight() {  return mHeight; }  public class CCPoint {  float m_fPositionX = 0;  float m_fPositionY = 0; }  private void initConstPoints() {  for(int i=0; i!=4; ++i)  {   CCPoint tempPoint = new CCPoint();   if(i%2 == 0 )   {    tempPoint.m_fPositionX = mPosition.m_fPositionX + mWidth/2;   }   else   {    tempPoint.m_fPositionX = mPosition.m_fPositionX - mWidth/2;   }      if(i<2)   {    tempPoint.m_fPositionY = mPosition.m_fPositionY + mHeight/2;   }   else   {    tempPoint.m_fPositionY = mPosition.m_fPositionY - mHeight/2;   }   mConstFourPoints.add(tempPoint);   mFourPoints.add(tempPoint);  }  //  for(int i=0; i!=mConstFourPoints.size(); ++i)//  {//   Log.d("CCPoint","Position " + i +" = ("+mFourPoints.get(i).m_fPositionX+","+mFourPoints.get(i).m_fPositionY+")");//  } }  public void initRotatedSpriteRecet(float rotation,float width,float height) {  mRotation = rotation;  mWidth = width;  mHeight = height;  initConstPoints();  newRotationPosition(mRotation); }  public void newRotationPosition(float rotation) {  double dirRectPositionX = mPosition.m_fPositionX - mConstFourPoints.get(0).m_fPositionX;  double dirRectPositionY = mPosition.m_fPositionY - mConstFourPoints.get(0).m_fPositionY;  double dirRectSqrt = dirRectPositionX * dirRectPositionX + dirRectPositionY * dirRectPositionY;  double dirRect = Math.sqrt(dirRectSqrt);    double constRotation = Math.asin(mHeight/(2 *dirRect));  double rotation360_1 = constRotation*180/PI;  double appendRotation1 = rotation360_1 + rotation;  double appendRotationPI1 = appendRotation1 * PI/180;  double dirEndPositionY1  = dirRect * Math.sin(appendRotationPI1);  double dirEndPositionX1 = dirRect * Math.cos(appendRotationPI1);    double rotation360_2 = 180 -constRotation*180/PI;  double appendRotation2 = rotation360_2 + rotation;  double appendRotationPI2 = appendRotation2 * PI/180;  double dirEndPositionY2  = dirRect * Math.sin(appendRotationPI2);  double dirEndPositionX2 = dirRect * Math.cos(appendRotationPI2);    mFourPoints.get(0).m_fPositionX = mPosition.m_fPositionX + (float)dirEndPositionX1;   mFourPoints.get(0).m_fPositionY = mPosition.m_fPositionY + (float)dirEndPositionY1;   

  mFourPoints.get(1).m_fPositionX = mPosition.m_fPositionX + (float)dirEndPositionX2;   mFourPoints.get(1).m_fPositionY = mPosition.m_fPositionY + (float)dirEndPositionY2;     mFourPoints.get(2).m_fPositionX = mPosition.m_fPositionX - (float)dirEndPositionX2;   mFourPoints.get(2).m_fPositionY = mPosition.m_fPositionY - (float)dirEndPositionY2;     mFourPoints.get(3).m_fPositionX = mPosition.m_fPositionX - (float)dirEndPositionX1;   mFourPoints.get(3).m_fPositionY = mPosition.m_fPositionY - (float)dirEndPositionY1;   printfPointsPosition(); } public void printfPointsPosition() {  for(int i=0; i!=mFourPoints.size(); ++i)  {   Log.d("CCPoint","Position " + i +" = ("+mFourPoints.get(i).m_fPositionX+","+mFourPoints.get(i).m_fPositionY+")");  }    double dir1X = (mFourPoints.get(0).m_fPositionX - mFourPoints.get(1).m_fPositionX)*(mFourPoints.get(0).m_fPositionX - mFourPoints.get(1).m_fPositionX);  double dir1Y = (mFourPoints.get(0).m_fPositionY - mFourPoints.get(1).m_fPositionY)*(mFourPoints.get(0).m_fPositionY - mFourPoints.get(1).m_fPositionY);    double dir3X = (mFourPoints.get(0).m_fPositionX - mFourPoints.get(2).m_fPositionX)*(mFourPoints.get(0).m_fPositionX - mFourPoints.get(2).m_fPositionX);  double dir3Y = (mFourPoints.get(0).m_fPositionY - mFourPoints.get(2).m_fPositionY)*(mFourPoints.get(0).m_fPositionY - mFourPoints.get(2).m_fPositionY);    double dir13X = (mFourPoints.get(1).m_fPositionX - mFourPoints.get(2).m_fPositionX)*(mFourPoints.get(1).m_fPositionX - mFourPoints.get(2).m_fPositionX);  double dir13Y = (mFourPoints.get(1).m_fPositionY - mFourPoints.get(2).m_fPositionY)*(mFourPoints.get(1).m_fPositionY - mFourPoints.get(2).m_fPositionY);    double dir1 = dir1X + dir1Y;  double dir3 = dir3X + dir3Y + dir1;  double dir13 = dir13X + dir13Y;  Log.d("CCPoint","is equals!! " + dir3 + " dir13 " + dir13); }  static boolean insertPersonRect(MySprite sprite1,MySprite sprite2) {  float sprite1Radius = (float)Math.sqrt(sprite1.mWidth*sprite1.mWidth + sprite1.mHeight*sprite1.mHeight)/2;  float sprite2Radius = (float)Math.sqrt(sprite2.mWidth*sprite2.mWidth + sprite2.mHeight*sprite2.mHeight)/2;    float dirFormSp1ToSp2 = (float)Math.sqrt((sprite2.mPosition.m_fPositionX-sprite1.mPosition.m_fPositionX)*(sprite2.mPosition.m_fPositionY-sprite1.mPosition.m_fPositionY));    if(sprite1Radius + sprite2Radius < dirFormSp1ToSp2)  {   return false;  }  else  {   double rotationPI = MySprite.rotationToPI(sprite1.mRotation);   float cosValue = Math.abs((float)Math.cos(rotationPI));   if(Math.cos(rotationPI) < 1e-10 && Math.cos(rotationPI) > -1e-10)   {         //精靈角度為90度或者270,矩形長寬調整,直接判斷       }   else if((cosValue-1) < 1e-10 && (cosValue-1) > -1e-10)   {    //精靈角度為0度或者360度,直接判斷   }   else   {       }   int equals0 = Float.compare(sprite1.mRotation,0);//   int equals0 = Float.compare(sprite1.mRotation,0);//   int equals0 = Float.compare(sprite1.mRotation,0);//   int equals0 = Float.compare(sprite1.mRotation,0);     }  //if(Person.)  return false; }  static double rotationToPI(float rotation) {  return rotation *PI/180; }  static float PIToRotation(double PIrotation) {  return (float)(PIrotation*180/PI); }}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com