Skip to content

Instantly share code, notes, and snippets.

@novogrammer
Last active October 16, 2021 20:53
Show Gist options
  • Save novogrammer/d8a93bb49eb492f2f0e6a191671bce4a to your computer and use it in GitHub Desktop.
Save novogrammer/d8a93bb49eb492f2f0e6a191671bce4a to your computer and use it in GitHub Desktop.
Ammoのdestroyがめんどくさいので、後でまとめて片付ける仕組みを作った。
class AmmoObjectSweeper{
constructor(ammo){
this.ammo = ammo;
this.temporaryObjects = [];
this.permanentObjects = [];
}
markTemporary(object){
this.temporaryObjects.push(object);
return object;
}
markPermanent(object){
this.permanentObjects.push(object);
return object;
}
destroyTemporaryObjects(){
for(let temporaryObject of this.temporaryObjects){
this.ammo.destroy(temporaryObject);
}
this.temporaryObjects = [];
}
destroyPermanentObjects(){
for(let permanentObject of this.permanentObjects){
this.ammo.destroy(permanentObject);
}
this.permanentObjects = [];
}
}
async function main(){
Ammo=await Ammo();
const ammoObjectSweeper=new AmmoObjectSweeper(Ammo);
// markT、markPのように短い名前にしておくと便利。
const markT = ammoObjectSweeper.markTemporary.bind(ammoObjectSweeper);
const markP = ammoObjectSweeper.markPermanent.bind(ammoObjectSweeper);
// new Ammo.btXするときは必ずmarkTかmarkPでwrapする
// どちらがいいかはBulletのC++のソースを読むことでわかる。
// 後で使う場合はmarkPを呼ぶ、後で使うオブジェクトのメンバにポインタとして保持する場合もmarkPを呼ぶ。
// その他、btVectorなど値渡しでコピーされるオブジェクトはmarkTを呼ぶ。
const boxShape = markP(new Ammo.btBoxShape(markT(new Ammo.btVector3(1, 1, 1))));
// 一時オブジェクトが原因でメモリ不足になることがあるので、定期的に呼び出す。
ammoObjectSweeper.destroyTemporaryObjects();
// 永続オブジェクトを後片付けしたい時に呼び出す。
ammoObjectSweeper.destroyPermanentObjects();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment