Skip to content

collision 実装

tkr987 edited this page Aug 8, 2018 · 1 revision

衝突判定を実装するにはHNLIB::PositionHandleを使用する。まずはuserクラスに衝突判定を実装する。HNLIB::PositionHandleのポインタをメンバに加える。

Handle系クラスの説明:
Hanlde系クラスは1フレーム内なら何時でも書き換えることができ、書き換えた内容を反映させることが出来る。Handleの取得は、新規ハンドルならCreateHandle()、既存ハンドルならGetHandle()を使用する。

#pragma once
#include "HNLIB.h"

class TeemoUser : public HNLIB::NyaUser
{
public:
	HNLIB::GraphicPropertyX4 gpx_;
	HNLIB::PositionHandle* phandle_;
	TeemoUser();
	void Act(void);
	void Draw(void);
};

ソースファイルでPositionHandleの設定をする。前述のとおり新規にHandleを取得するためCreateHandle()を呼び出す。新規ハンドル取得後、衝突判定に必要なメンバを設定する。最低限必要なメンバは3つある。

  • PositionHandle::collision_range; 衝突範囲(半径)
  • PositionHandle::grid_x; 衝突判定する中心x座標
  • PositionHandle::grid_y; 衝突判定する中心y座標

また、衝突したとき”Hit!”文字を描画するようにした。文字列を描画するにはNyaString::Settingfont()でフォントを設定した後にNyaString::Write()を呼び出す。

NyaString::Settingfont()のプロトタイプ:
void NyaString::SettingFont(std::string font_name, int font_size, int font_thick);

  • font_name; フォント設定の名前
  • font_size; フォントのサイズ
  • font_thick; フォントの太さ

NyaString::Write()のプロトタイプ:
void NyaString::Write(string font, std::tuple<int, int, int> color, int grid_x, int grid_y, string str);

  • font; 使用するフォント設定
  • color; フォントの色
  • grid_x; 描画するx座標
  • grid_y; 描画するy座標
  • str; 描画する文字列
#include <tuple>
#include "TeemoUser.h"
using namespace std;
using namespace HNLIB;

TeemoUser::TeemoUser()
{
	gpx_.draw_grid_cx_ = 100;
	gpx_.draw_grid_cy_ = 500;
	NyaGraphic::Load("img/teemo.png", &gpx_.file_);
	// 衝突判定用
	phandle_ = NyaPosition::CreateHandle();
	phandle_->collision_range_ = 20;
	phandle_->grid_x_ = 400;
	phandle_->grid_y_ = 500;
	NyaPosition::CollisionPair(eOBJECT::USER1, eOBJECT::TARGET_ATTACK1);
	// フォント設定
	NyaString::SettingFont("teemo_font", 10, 2);
}

void TeemoUser::Act(void)
{
	const tuple<int, int, int> white = make_tuple(255, 255, 255);

	NyaPosition::Collide(phandle_, eOBJECT::USER1);
	if (phandle_->collision_hit_damage_ != 0)
		NyaString::Write("teemo_font", white, 100, 400, "hit!");
}

void TeemoUser::Draw(void)
{
	NyaGraphic::Draw(&gpx_, eOBJECT::USER1);
}

次にtargetのdeviceに衝突判定用の設定をおこなう。NyaDeviceクラスは内部で自動的に衝突判定を処理してくれるため、deviceに衝突判定させるだけならPositionHandleクラスの知識は必要ない。

#pragma once
#include "HNLIB.h"

class TeemoDevice
{
public:
	HNLIB::DevicePropertyX1 dpx_;
	HNLIB::GraphicPropertyX4 gpx_;
	TeemoDevice();
};

class TeemoTarget : public HNLIB::NyaTarget 
{
public:
	void Act(void);
	void Draw(void);
	TeemoTarget();
private:
	unsigned int count_frame_;
	TeemoDevice device_;
	HNLIB::GraphicPropertyX4 gpx_;
};

deviceに衝突判定させるために設定するDevicePropertyX1のメンバは2つある。

衝突判定に関係するDevicePropertyX1のメンバ:

  • DevicePropertyX1::collision_power_; 衝撃力(設定しなければ1)
  • DevicePropertyX1::collision_range_; 衝突範囲(設定しなければ1)

2つのメンバを設定したDevicePropertyX1をNyaDevice::Attack14関数に渡す。今回は衝突範囲を半径10に設定する(衝撃力はデフォルトの1とする)。

#include "HNLIB.h"
#include "TeemoTarget.h"
using namespace std;
using namespace HNLIB;

TeemoDevice::TeemoDevice()
{
	// デバイスに衝突範囲を設定
	dpx_.collision_range_ = 10;
	// デバイスの設定
	dpx_.move_angle_deg_ = 90;
	dpx_.move_speed_ = 3;
	dpx_.create_x_ = 400;
	dpx_.create_y_ = 200;
	NyaGraphic::Load("img/attack_red1.png", &gpx_.file_);
}

TeemoTarget::TeemoTarget()
{
	count_frame_ = 0;
	gpx_.draw_grid_cx_ = 400;
	gpx_.draw_grid_cy_ = 200;
	NyaGraphic::Load("img/teemo_target.png", &gpx_.file_);
}

void TeemoTarget::Act(void)
{	
	if (count_frame_ % FPS_MAX == 0)
		NyaDevice::Attack14(&device_.dpx_, &device_.gpx_, eOBJECT::TARGET_ATTACK1);

	count_frame_++;
}

void TeemoTarget::Draw(void)
{
	NyaGraphic::Draw(&gpx_, eOBJECT::TARGET1);
}

コンパイルして実行する。
collision

Clone this wiki locally