Skip to content

Instantly share code, notes, and snippets.

@flushpot1125
Created June 12, 2021 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flushpot1125/819906d91712548d46089272d453b083 to your computer and use it in GitHub Desktop.
Save flushpot1125/819906d91712548d46089272d453b083 to your computer and use it in GitHub Desktop.
import { Mesh,PhysicsImpostor } from "@babylonjs/core";
import { fromScene} from "../tools";
export default class block extends Mesh {
@fromScene("ball")
_ball :Mesh;//1つ上の行の"@fromScene"の引数が、Graph View上のオブジェクト名 (この場合は"ball"というオブジェクトがGraph Viewに存在する)。そのオブジェクトを_ballに対応させる
//補足 Graph Viewは、UnityのHierarchy Viewと同等
//block生成時に1度だけ呼ばれる
public onStart(): void {
//ここでブロックに物理エンジンをつけている。mass:0にしないと、ブロックが浮いてしまう。friction(摩擦係数):0, restitution(反発係数):1により弾性衝突を実現
this.physicsImpostor = new PhysicsImpostor(this, PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0, restitution: 1.0 });
}
//毎フレームごとに呼ばれる
public onUpdate(): void {
//intersectsMeshを使うと、引数のオブジェクトと衝突したら、という処理が作れる
if ((this.intersectsMesh(this._ball)) && this.callFlag== false){
this.callFlag=true;
console.log("collisioned!");
// this.dispose();//dispose = Unityのdestroyと同じ。ここで呼べば、ボールが衝突した瞬間に消えるので、ボールが貫通してしまう
this.blockDispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment