HSPポータル
サイトマップ お問い合わせ


HSPTV!掲示板


未解決 解決 停止 削除要請

2008
1216
裏口ひし形同士の当たり判定7解決


裏口

リンク

2008/12/16(Tue) 08:34:33|NO.21620

ひし形同士の当たり判定は可能でしょうか?



この記事に返信する


llllllllll

リンク

2008/12/16(Tue) 17:00:24|NO.21629

はい



Saike

リンク

2008/12/17(Wed) 21:41:48|NO.21674

抽象的なアドバイスになりますが・・・、

「◇」のような形同士の当たり判定なら
「中心座標から、x座標とy座標の和が等しく離れた図形が菱形」
と考えれば計算できると思います。

↓イメージはこんなです。等幅文字で見てください。
*****5*****
****545****
***54345***
**5432345**
*543212345*
54321012345
*543212345*
**5432345**
***54345***
****545****
*****5*****

縦横比が均一でなければ、x、yそれぞれに何か掛け算するという
方向で。。

・・・関数化してくれる方に期待・・・w。



ANTARES

リンク

2008/12/18(Thu) 01:20:52|NO.21678

 対角線の長さが同じなので、それは特殊な菱形すなわち正方形です。



hspail

リンク

2008/12/18(Thu) 07:57:17|NO.21679

作ってみました。円と菱形の当たり判定です。

#uselib "user32.dll" #func InvalidateRect "InvalidateRect" int, int, int #uselib "gdi32.dll" #cfunc CreatePolygonRgn "CreatePolygonRgn" int, int, int #cfunc CreateEllipticRgn "CreateEllipticRgn" int, int, int, int #func FillRgn "FillRgn" int, int, int #cfunc GetStockObject "GetStockObject" int #func DeleteObject "DeleteObject" int #cfunc CombineRgn "CombineRgn" int, int, int, int #define BLACK_BRUSH 4 // 黒 repeat redraw 0 color 255, 255, 255 : boxf // 菱形のリージョンを作成 points = 100, 0, 0, 200, 100, 400, 200, 200 hRgn = CreatePolygonRgn(varptr(points), 4, 1) // マウス位置に円形のリージョンを作成 hRgn2 = CreateEllipticRgn(mousex-50, mousey-50, mousex+50, mousey+50) // リージョンを塗りつぶし FillRgn hdc, hRgn, GetStockObject(BLACK_BRUSH) FillRgn hdc, hRgn2, GetStockObject(BLACK_BRUSH) // 当たり判定の結果(0と1以外は当たってる) title ""+CombineRgn(hRgn, hRgn, hRgn2, 1) DeleteObject hRgn DeleteObject hRgn2 redraw 1 wait 1 loop



hspail

リンク

2008/12/18(Thu) 08:11:57|NO.21680

すみません、菱形同士でしたね。
CreateEllipticRgnの所をCreatePolygonRgnを使うようにすればできると思います。



flory

リンク

2008/12/18(Thu) 08:23:34|NO.21681

リージョンを使わない場合の、任意の多角形の当たり判定を行うモジュールはこんな感じ。
即興で作ったので結構いい加減です。

#module #defcfunc CollisionPolygon array pol1, array pol2 num1 = length(pol1)/2 num2 = length(pol2)/2 repeat num2 ret = 1 x = pol2(cnt*2) y = pol2(cnt*2+1) repeat num1 if (x-pol1(cnt*2))*(pol1((cnt+1)\num1*2+1)-pol1(cnt*2+1)) > (pol1((cnt+1)\num1*2)-pol1(cnt*2))*(y-pol1(cnt*2+1)) { ret = 0 break } loop if ret = 1 : break loop if ret = 0 { repeat num1 ret = 1 x = pol1(cnt*2) y = pol1(cnt*2+1) repeat num2 if (x-pol2(cnt*2))*(pol2((cnt+1)\num2*2+1)-pol2(cnt*2+1)) > (pol2((cnt+1)\num2*2)-pol2(cnt*2))*(y-pol2(cnt*2+1)) { ret = 0 break } loop if ret = 1 : break loop } return ret #deffunc DrawPolygon array pol num = length(pol)/2 repeat num line pol((cnt+1)\num*2), pol((cnt+1)\num*2+1), pol(cnt*2), pol(cnt*2+1) loop return #global ; 以下サンプル。 ; カーソルキーで多角形を動かし、当たり判定を行う。 spd = 4 ; 任意の多角形の頂点座標を右回りで配列に代入する。 ; ただし、へこんでいる多角形(いわゆる凹多角形)は不具合が出る。 pol1 = 64,0, 128,48, 64,96, 0,48 pol2 = 320,48, 448,240, 320,432, 192,240 repeat x = 0 y = 0 stick key, 15 if key & 1 : x -= spd if key & 2 : y -= spd if key & 4 : x += spd if key & 8 : y += spd repeat length(pol1)/2 pol1(cnt*2) += x pol1(cnt*2+1) += y loop redraw 0 color 255, 255, 255 boxf color 0, 0, 0 DrawPolygon pol1 DrawPolygon pol2 if CollisionPolygon(pol1, pol2) { pos 0, 0 mes "Hit" } redraw 1 wait 2 loop

参考
http://marupeke296.com/COL_2D_No4_SquareToSquare.html

リンク先にもある通り、これでは効率が悪くて使い物にならないでしょうが。


Saikeさんの案も、改良すればなんとかなるんじゃないですかね?
正方形ではないひし形の場合でも、短軸と長軸の長さをうまく使えば……。
自分にはちょっと思いつかないですが。



裏口

リンク

2008/12/19(Fri) 17:22:45|NO.21724

返事遅くなってごめんなさい。
ありがとうございます。
解決しました。



ONION software Copyright 1997-2023(c) All rights reserved.