自己紹介

低層をがむしゃらに走る自称プログラマ ちょっとWebとかに手を出してる

ブログ アーカイブ

2010年5月26日水曜日

当たり判定ほぼ終了


で、現在進行中なのは
Shape1と2を同じようにUserDataに持たせようと言うことであった
以下次回へすすむはず…


これで終わった前回のブログ
しかしそうはいかなかった、もっと簡単な方法があったのだった

それは

b2ContactPointをUserDataに持たせることであった!



説明しよう!!

まず、ContactListenerを継承した正式な当たり判定処理を行う
この時点で、まずは引数として渡されたb2ContactPointの”中身”を
UserDataに渡すのだ。

小言:
b2ContactPointはstruct。
UserDataにb2ContactPointを持たせたクラスを持たせている(言い方がややこしいが)


そして、UserData内で、当たり判定処理をもう一つ作っておき、

ContactListenerによる正式な当たり判定が終わった後に、
当たり判定後の”アクション処理”を書く

そのアクションの中で、UserData内にあるb2ContactPointを使うことで、
うまいこと動いてくれました

以下必要なところだけ切り出した
荒削りなので微妙かも

----------------------
CallBack.cpp(BOX2Dのb2ContactListener を継承しているクラス)
----------------------

<他の判定処理も同じような事をしてるので省略>
void CCallBack::Add(const b2ContactPoint* point)
{
//UserDataをとってきます
CInfomation* info = static_cast(point->shape1->GetBody()->GetUserData());
   //UserDataに引数を渡す(CInfomation.hで記述)
info->setCP(point);
//当たり判定の種類を渡す
info->setHit( info->e_HIT_ADD );

//当たった物体両方に同じように渡すため、もうひとつのBodyが持つUserDataに同じことをする
info = static_cast(point->shape2->GetBody()->GetUserData());

info->setCP(point);
info->setHit( info->e_HIT_ADD);

}
-----------------
CInfomation.h(UserDataに格納するクラス)
-----------------

class CInfomation
{
private:

       //当たり判定関係 uint8 _uint_hit; //当たり判定状況ビット b2ContactPoint* const _struct_hit; //当たった際必要な情報をまとめている

<偽の当たり判定処理>
<継承先で処理は定義するようになっている>
//相手に「当たった時」呼ばれる
virtual void add(const b2ContactPoint* point) = 0;
//相手に「当たっている時」呼ばれる
virtual void persist(const b2ContactPoint* point) = 0;
//相手に「当たっていた状態から離れた時」呼ばれる
virtual void remove(const b2ContactPoint* point) = 0;


public:

//当たり判定
inline void setHit(uint8 flag) {_uint_hit  = flag;}
inline void setCP(const b2ContactPoint* cp)<これが自分のとこを入れかえてる>
{
_struct_hit->friction = cp->friction;
_struct_hit->id = cp->id;
_struct_hit->normal = cp->normal;
_struct_hit->position = cp->position;
_struct_hit->restitution = cp->restitution;
_struct_hit->separation = cp->separation;
_struct_hit->shape1 = cp->shape1;
_struct_hit->shape2 = cp->shape2;
_struct_hit->velocity = cp->velocity;
}

//オブジェクトを進行させる
void Update();
}

---------------------
CInfomation.cpp
---------------------

<当たり判定以外は省略>
void CInfomation::Update()
{
//当たり判定処理
//CCallBackクラスによって設定されたフラグで、処理が変化
switch(_uint_hit)
{

case(e_HIT_ADD): add(_struct_hit);
_uint_hit = this->e_HIT_NOHIT;
break;
case(e_HIT_PERSIST):persist(_struct_hit);
_uint_hit = this->e_HIT_NOHIT;
break;
case(e_HIT_REMOVE): remove(_struct_hit);
_uint_hit = this->e_HIT_NOHIT;
break;
default:
break;
}
}

0 件のコメント:

コメントを投稿