忍者ブログ

GAM-22のメモ

[HSP3メモ]サンプル

2024.04 ← 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 →

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

矩形のアスペクト比を保ちウィンドウに合わせて変化

アスペクト比とは縦横の割合のことです。矩形のアスペクト比を保ち、ウィンドウの大きさに連動してサイズを可変します。

動画のプレイヤーなどに使える気がします。

if 0 {

#deffunc boxf2 int x, int y, int w, int h

;-----------------------------------------------------------
;    boxfの改造
;-----------------------------------------------------------

; p1,p2 : 中心座標
; p3,p4    : サイズ
;-----------------------------------------------------------

    boxf x-w/2,y-h/2, x+w/2,y+h/2
    return

}

    gsel 0, -1
    screen 1, 640,480, 0

    ; オリジナルのサイズ、実数がポインント
    ow = 500.0 : oh = 300.0

*main
    redraw 0
    color 255,255,255 : boxf

    sw = ow : sh = oh
    v1 = ow/oh

    if (oh > ginfo_winy) | (ow > ginfo_winx) {
        if (ow/ginfo_winx) > (oh/ginfo_winy) {
            sw = 0.0+ginfo_winx
            sh = sw/v1
        } else {
            sh = 0.0+ginfo_winy
            sw = sh*v1
        }
    }

    color 0,0,0 : boxf2 ginfo_winx/2,ginfo_winy/2, sw,sh
    redraw

    await 10
    goto *main

FancyZoomのソースを、HSPに移植すれば良いと思ったけど、上手くいかなかったから、結局自力で考える羽目になりました。次はその失敗例です。

PR

矩形の衝突判定

回転しない矩形同士の衝突判定サンプルです。
世界でも最もポピュラーじゃないんでしょうか?
最近はあんまり使ってないですね。
;===========================================================
;												2007/10/25
;	矩形の衝突判定モジュール ver.0.1.0
;												GAM-22
;===========================================================

if 0 {

#defcfunc chk_box int ax, int ay, int axs, int ays, int bx, int by, int bxs, int bys

;-----------------------------------------------------------
;	矩形同士の交差判定
;-----------------------------------------------------------
; 
; p1,p2 : 矩形Aの座標
; p3,p4	: 矩形Aのサイズ
; p5,p6 : 矩形Bの座標
; p7,p8	: 矩形Bのサイズ
;-----------------------------------------------------------

	return (ax+axs>=bx) & (bx+bxs>=ax) & (ay+bys>=by) & (by+bys>=ay)

}

	cxm = 1
	cym = 1

repeat
	redraw 0

	; 背景
	color 255,255,255 : boxf

	; 矩形A
	color ,,255
	boxf mousex,mousey, mousex+32,mousey+32

	; 円B
	color 255
	boxf cx,cy, cx+64,cy+64
	cx += cxm
	cy += cym
	if cx<=0 | cx>=576 : cxm *= -1
	if cy<=0 | cy>=416 : cym *= -1

	; 判定
	if chk_box(mousex,mousey,32,32, cx,cy,64,64) {
		pos 0,0 : mes "当たってる"
	}

	redraw
	await 5
	loop

戦車とかで、回転する矩形を判定したいなら線分判定を組み合わせれば良いと思います。
実装した例 : Battle Armor
どうせなら、多角形に対応するとベスト(?)。

円の衝突判定

円同士の衝突判定のサンプルです。
高速で簡単なので、お気に入りです。
;===========================================================
;												2007/10/24
;	円の衝突判定モジュール ver.0.1.0
;												GAM-22
;===========================================================

if 0 {

#defcfunc chk_circle int ax, int ay, int ar, int bx, int by, int br

;-----------------------------------------------------------
;	円同士の交差判定
;-----------------------------------------------------------
; 
; p1,p2 : 円Aの座標
; p3	: 円Aの半径
; p4,p5 : 円Bの座標
; p6	: 円Bの半径
;-----------------------------------------------------------

	; 絶対値に変換するのは、巨大な足し算するとマイナスになって困るから
	return abs((ax-bx)*(ax-bx) + (ay-by)*(ay-by)) <= (ar+br)*(ar+br)

#deffunc circle2 int x, int y, int r

;-----------------------------------------------------------
;	円を描画
;-----------------------------------------------------------
;
; p1,p2	: 中心座標
; p3	: 半径
;-----------------------------------------------------------

	circle x-r,y-r, x+r,y+r
	return
}

	cxm = 1
	cym = 1
	cx = 320
	cy = 240

repeat
	redraw 0

	; 背景
	color 255,255,255 : boxf

	; 円A
	color ,,255
	circle2 mousex,mousey, 32

	; 円B
	color 255
	circle2 cx,cy, 64
	cx += cxm
	cy += cym
	if cx<=64 | cx>=576 : cxm *= -1
	if cy<=64 | cy>=416 : cym *= -1

	; 判定
	if chk_circle(mousex,mousey,32, cx,cy,64) {
		pos 0,0 : mes "当たってる"
	}

	redraw
	await 5
	loop

線分の交差判定

線分の交差判定のサンプルです。
;===========================================================
;												2007/8/14
;	線分交差判定モジュール ver.0.1.0
;												GAM-22
;===========================================================

if 0 {

#defcfunc chk_line double ax1, double ay1, double ax2, double ay2, double bx1, double by1, double bx2, double by2

;-------------------------------------------------------
; 線分同士の交差判定
;-------------------------------------------------------
; 
; p1,p2  : x1,y1(A)
; p3,p4  : x2,y2(A)
; p5,p6  : x1,y1(B)
; p7,p8  : x2,y2(B)
;-------------------------------------------------------

	;	ax2-ax1, ay2-ay1 の法線
	m1 = -(ay2-ay1) : m2 = ax2-ax1
	
	;	正規化(長さを1.0にする)
	m3 = sqrt(m1*m1 + m2*m2)
	if m3>0 : m3 = 1.0/m3
	m1 *= m3
	m2 *= m3

	;	var d = -(ax * nx + ay * ny);
	;	var t = -(nx * x + ny * y + d) / (nx * dx + ny * dy);
	if (m1*(bx2-bx1) + m2*(by2-by1))!0{
		m4 = -(m1*bx1 + m2*by1 - (ax1*m1 + ay1*m2)) / (m1*(bx2-bx1) + m2*(by2-by1))
	}else{
		return 0
	}

	;	cx = x + dx * t;
	;	cy = y + dy * t;
	if m4>0 & m4<=1{
		stat1@ = bx1+(bx2-bx1)*m4
		stat2@ = by1+(by2-by1)*m4
		return (ax1-stat1@)*(ax2-stat1@)+(ay1-stat2@)*(ay2-stat2@) < 0;inner(ax1-stat1@, ay1-stat2@, ax2-stat1@, ay2-stat2@) < 0
	}else{
		return 0
	}
}


repeat
	redraw 0

	; 背景
	color 255,255,255 : boxf

	; 線分A
	color ,,255
	line mousex, mousey, 100, 100

	; 線分B
	v1 = 0.05*cnt
	line 340+cos(v1)*200,240+sin(v1)*200, 340-cos(v1)*200,240-sin(v1)*200

	; 判定
	if chk_line(mousex,mousey, 100,100, 340+cos(v1)*200,240+sin(v1)*200, 340-cos(v1)*200,240-sin(v1)*200) {
		color 255
		circle stat1-5,stat2-5, stat1+5,stat2+5
	}

	redraw
	await 50
	loop
仕組みとかは、ここのを参考にしました。

子ウィンドウを作る

子ウィンドウを作るサンプルです。
#uselib "user32"
#func SetParent "SetParent" int, int

ParentHwnd = hwnd
screen 1, 300,200, 8, 10,10
SetParent hwnd, ParentHwnd
CreateWindowExで、新規に作るとHSPの命令が使えなくなるので、
HSPのscreenでウィンドウを作ってから、SetParentを使って子ウィンドウに変更したほうが良いです。
カテゴリー
カウンター
最新記事
最新コメント
Twitter
人気記事
Amazon お買い得情報
Amazon お買い得情報
プロフィール
自画像
HN :
GAM-22
性別:
男性
職業:
大学1年生
HSP暦:
6年
好きなもの :
ゲーム, アニメ, マヨネーズ
嫌いなもの :
運動, 注射, ホラー映画, 英語
好きなバンド :
Muse, Radiohead
その他 :
文章能力が欠如している
バナー
Copyright © GAM-22のメモ All Rights Reserved
Powered by ニンジャブログ Designed by ピンキー・ローン・ピッグ 忍者ブログ / [PR]