rapier2d示例
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
[InternetShortcut]
|
||||
URL=https://docs.cocos.com/creator/manual/en/scripting/setup.html#custom-script-template
|
||||
1
tools/flashfin-rapier2d/.creator/default-meta.json
Normal file
1
tools/flashfin-rapier2d/.creator/default-meta.json
Normal file
@@ -0,0 +1 @@
|
||||
{"trimType":"none","image":{"type":"sprite-frame"},"sprite-frame":{"trimType":"none"}}
|
||||
24
tools/flashfin-rapier2d/.gitignore
vendored
Normal file
24
tools/flashfin-rapier2d/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#///////////////////////////
|
||||
# Cocos Creator 3D Project
|
||||
#///////////////////////////
|
||||
library/
|
||||
temp/
|
||||
local/
|
||||
build/
|
||||
profiles/
|
||||
native
|
||||
#//////////////////////////
|
||||
# NPM
|
||||
#//////////////////////////
|
||||
node_modules/
|
||||
|
||||
#//////////////////////////
|
||||
# VSCode
|
||||
#//////////////////////////
|
||||
.vscode/
|
||||
|
||||
#//////////////////////////
|
||||
# WebStorm
|
||||
#//////////////////////////
|
||||
.idea/
|
||||
21
tools/flashfin-rapier2d/LICENSE
Normal file
21
tools/flashfin-rapier2d/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 soida
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
15
tools/flashfin-rapier2d/README.md
Normal file
15
tools/flashfin-rapier2d/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
### 这是什么
|
||||
> 这是一个简单的在CocosCreater 3.8.x中使用Rapier2d物理引擎的样例
|
||||
|
||||
### 兼容版本
|
||||
` 3.6.x以上上都支持 ,因为是用的wasm库` `"@dimforge/rapier2d-compat": "^0.15.0"`
|
||||
|
||||
### 使用注意
|
||||
1. 拉取仓库
|
||||
2. 进入根目录后 `npm install`
|
||||
3. 可以在 `https://soidaken.github.io/flashfin-rapier2d-runpage/` 查看运行效果
|
||||
|
||||
|
||||
### 帮到你了吗?
|
||||
如果对你有帮助,请不吝赞助我一杯卡布奇诺☕️,谢谢!
|
||||

|
||||
9
tools/flashfin-rapier2d/assets/fonts.meta
Normal file
9
tools/flashfin-rapier2d/assets/fonts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6f5a343f-6383-4387-afc0-17b54ab0e2de",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
BIN
tools/flashfin-rapier2d/assets/fonts/pixelhanzi.ttf
(Stored with Git LFS)
Normal file
BIN
tools/flashfin-rapier2d/assets/fonts/pixelhanzi.ttf
(Stored with Git LFS)
Normal file
Binary file not shown.
12
tools/flashfin-rapier2d/assets/fonts/pixelhanzi.ttf.meta
Normal file
12
tools/flashfin-rapier2d/assets/fonts/pixelhanzi.ttf.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.0.1",
|
||||
"importer": "ttf-font",
|
||||
"imported": true,
|
||||
"uuid": "1cfbb169-e553-48b7-8741-55373a87f587",
|
||||
"files": [
|
||||
".json",
|
||||
"pixelhanzi.ttf"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
tools/flashfin-rapier2d/assets/prefab.meta
Normal file
9
tools/flashfin-rapier2d/assets/prefab.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "236b5f6b-6868-436d-87da-90ff4fcd7d95",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
BIN
tools/flashfin-rapier2d/assets/prefab/ballitem.prefab
(Stored with Git LFS)
Normal file
BIN
tools/flashfin-rapier2d/assets/prefab/ballitem.prefab
(Stored with Git LFS)
Normal file
Binary file not shown.
13
tools/flashfin-rapier2d/assets/prefab/ballitem.prefab.meta
Normal file
13
tools/flashfin-rapier2d/assets/prefab/ballitem.prefab.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "prefab",
|
||||
"imported": true,
|
||||
"uuid": "f011b380-7741-4c8d-ae17-76cf8a5fc011",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"syncNodeName": "ballitem"
|
||||
}
|
||||
}
|
||||
BIN
tools/flashfin-rapier2d/assets/prefab/testitem.prefab
(Stored with Git LFS)
Normal file
BIN
tools/flashfin-rapier2d/assets/prefab/testitem.prefab
(Stored with Git LFS)
Normal file
Binary file not shown.
13
tools/flashfin-rapier2d/assets/prefab/testitem.prefab.meta
Normal file
13
tools/flashfin-rapier2d/assets/prefab/testitem.prefab.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "prefab",
|
||||
"imported": true,
|
||||
"uuid": "1f217bc3-f4c1-4c97-80cd-f92e92bebb09",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"syncNodeName": "testitem"
|
||||
}
|
||||
}
|
||||
BIN
tools/flashfin-rapier2d/assets/scene_box2d.scene
(Stored with Git LFS)
Normal file
BIN
tools/flashfin-rapier2d/assets/scene_box2d.scene
(Stored with Git LFS)
Normal file
Binary file not shown.
11
tools/flashfin-rapier2d/assets/scene_box2d.scene.meta
Normal file
11
tools/flashfin-rapier2d/assets/scene_box2d.scene.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "452ca018-6ac2-414a-966a-166c7c8f4c30",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
BIN
tools/flashfin-rapier2d/assets/scene_rapier2d.scene
(Stored with Git LFS)
Normal file
BIN
tools/flashfin-rapier2d/assets/scene_rapier2d.scene
(Stored with Git LFS)
Normal file
Binary file not shown.
1
tools/flashfin-rapier2d/assets/scene_rapier2d.scene.meta
Normal file
1
tools/flashfin-rapier2d/assets/scene_rapier2d.scene.meta
Normal file
@@ -0,0 +1 @@
|
||||
{"ver":"1.1.50","importer":"scene","imported":true,"uuid":"6773b206-616b-4cb6-9a1b-0182367abb1c","files":[".json"],"subMetas":{},"userData":{}}
|
||||
9
tools/flashfin-rapier2d/assets/scripts.meta
Normal file
9
tools/flashfin-rapier2d/assets/scripts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "fe6eee8d-b74e-4f57-be34-fb9bacca7f0f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
49
tools/flashfin-rapier2d/assets/scripts/Clogger.ts
Normal file
49
tools/flashfin-rapier2d/assets/scripts/Clogger.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
function padStart(value: string | number, length: number, padChar: string): string {
|
||||
let result = String(value);
|
||||
while (result.length < length) {
|
||||
result = padChar + result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class Clogger {
|
||||
private prefix: string;
|
||||
|
||||
constructor(prefix: string = '') {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
// 将参数转换为字符串并输出到控制台
|
||||
private formatAndLog(...args: any[]): void {
|
||||
const now = new Date();
|
||||
const hours = padStart(now.getHours(), 2, '0');
|
||||
const minutes = padStart(now.getMinutes(), 2, '0');
|
||||
const seconds = padStart(now.getSeconds(), 2, '0');
|
||||
const milliseconds = padStart(now.getMilliseconds(), 3, '0');
|
||||
|
||||
const timestamp = `${hours}:${minutes}:${seconds}.${milliseconds}`;
|
||||
|
||||
console.log(
|
||||
this.prefix +'|'+ timestamp +'|'+
|
||||
args.map(arg => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg))).join(' ')
|
||||
);
|
||||
}
|
||||
debug(...args: any[]): void {
|
||||
this.formatAndLog('DEBUG', ...args);
|
||||
}
|
||||
|
||||
log(...args: any[]): void {
|
||||
this.formatAndLog('LOG', ...args);
|
||||
}
|
||||
|
||||
warn(...args: any[]): void {
|
||||
this.formatAndLog('WARN', ...args);
|
||||
}
|
||||
|
||||
error(...args: any[]): void {
|
||||
this.formatAndLog('ERROR', ...args);
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = new Clogger('SOIDA');
|
||||
9
tools/flashfin-rapier2d/assets/scripts/Clogger.ts.meta
Normal file
9
tools/flashfin-rapier2d/assets/scripts/Clogger.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ff0e1b17-5af0-4e18-8357-b98382f3911c",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
100
tools/flashfin-rapier2d/assets/scripts/RapierDebugRenderer.ts
Normal file
100
tools/flashfin-rapier2d/assets/scripts/RapierDebugRenderer.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { _decorator, Component, Graphics, Color } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
// import RAPIER from '@dimforge/rapier2d';
|
||||
|
||||
export const RAPIER_PTM_RATIO:number = 32.0;
|
||||
|
||||
@ccclass('RapierDebugRenderer')
|
||||
export class RapierDebugRenderer extends Component {
|
||||
@property(Graphics)
|
||||
graphics: Graphics = null!;
|
||||
|
||||
// 可选:可调整的线宽
|
||||
@property
|
||||
lineWidth: number = 1.0;
|
||||
|
||||
// 可选:坐标缩放系数
|
||||
@property
|
||||
scale: number = RAPIER_PTM_RATIO;
|
||||
|
||||
@property
|
||||
onoff: boolean = false;
|
||||
|
||||
|
||||
private _world: any = null; // RAPIER.World 类型
|
||||
|
||||
/**
|
||||
* 设置物理世界引用
|
||||
* @param world RAPIER物理世界实例
|
||||
*/
|
||||
public setWorld(world: any) {
|
||||
this._world = world;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在每帧更新中调用以渲染调试图形
|
||||
*/
|
||||
update() {
|
||||
if (!this._world || !this.graphics) return;
|
||||
|
||||
this.onoff && this.render(this._world);
|
||||
}
|
||||
|
||||
clear(){
|
||||
this.graphics&&this.graphics.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染物理世界的调试信息
|
||||
* @param world RAPIER物理世界
|
||||
*/
|
||||
render(world: any) {
|
||||
// 获取调试渲染数据
|
||||
const { vertices, colors } = world.debugRender();
|
||||
|
||||
// 清除之前的绘制内容
|
||||
this.graphics.clear();
|
||||
|
||||
// const g = this.graphics;
|
||||
// g.lineWidth = 10;
|
||||
// g.fillColor.fromHEX('#ff0000ff');
|
||||
// g.moveTo(-40, 0);
|
||||
// g.lineTo(0, -80);
|
||||
// g.lineTo(40, 0);
|
||||
// g.lineTo(0, 80);
|
||||
// g.close();
|
||||
// g.stroke();
|
||||
// g.fill();
|
||||
|
||||
// 遍历所有线段进行绘制
|
||||
for (let i = 0; i < vertices.length / 4; i++) {
|
||||
// 从颜色数组中获取当前线段的颜色
|
||||
const r = Math.floor(colors[i * 8] * 255); // 转换为 0-255 范围
|
||||
const g = Math.floor(colors[i * 8 + 1] * 255);
|
||||
const b = Math.floor(colors[i * 8 + 2] * 255);
|
||||
const a = colors[i * 8 + 3] * 255; // alpha 直接使用 0-255 范围
|
||||
|
||||
// 设置线条样式
|
||||
this.graphics.lineWidth = this.lineWidth;
|
||||
this.graphics.strokeColor = new Color(r, g, b, a);
|
||||
|
||||
// 获取线段的起点和终点坐标
|
||||
// 注意:Cocos Creator 使用左下角为原点的坐标系,y轴向上为正
|
||||
// 而 RAPIER 可能使用不同的坐标系,因此这里做适当调整
|
||||
|
||||
// 获取原始坐标
|
||||
let x1 = vertices[i * 4] * this.scale;
|
||||
let y1 = vertices[i * 4 + 1] * this.scale;
|
||||
let x2 = vertices[i * 4 + 2] * this.scale;
|
||||
let y2 = vertices[i * 4 + 3] * this.scale;
|
||||
|
||||
|
||||
// 执行绘制
|
||||
this.graphics.moveTo(x1, y1);
|
||||
this.graphics.lineTo(x2, y2);
|
||||
this.graphics.close();
|
||||
this.graphics.stroke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "020358be-0b18-47a4-ad46-a6972ff0ed99",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
87
tools/flashfin-rapier2d/assets/scripts/SceneMain.ts
Normal file
87
tools/flashfin-rapier2d/assets/scripts/SceneMain.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite } from 'cc';
|
||||
import * as cc from 'cc';
|
||||
|
||||
import { logger } from './Clogger';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('SceneMain')
|
||||
export class SceneMain extends Component {
|
||||
|
||||
@property(Node)
|
||||
node_fps:Node|null = null;
|
||||
|
||||
@property(Node)
|
||||
node_counts:Node|null = null;
|
||||
|
||||
@property(Prefab)
|
||||
prefab_item:Prefab|null = null;
|
||||
|
||||
@property(Node)
|
||||
container_balls:Node|null = null;
|
||||
|
||||
@property(cc.Node)
|
||||
node_switchscene:cc.Node|null = null;
|
||||
|
||||
private _timeAccumulator: any;
|
||||
private _countAcc: any;
|
||||
|
||||
private _listBalls: Node[] = [];
|
||||
|
||||
protected onLoad(): void {
|
||||
logger.log('dballs onLoad');
|
||||
|
||||
let p = cc.PhysicsSystem2D.instance;
|
||||
p.positionIterations = 4;
|
||||
p.velocityIterations = 2;
|
||||
|
||||
}
|
||||
start() {
|
||||
logger.log('dballs start');
|
||||
|
||||
// let n= this.node.getChildByName('ballitem')
|
||||
// let r = n.getComponent(cc.CircleCollider2D).radius
|
||||
// logger.log('radius:', r);
|
||||
|
||||
this.node_switchscene.on('click', (b:cc.Button) => {
|
||||
cc.director.loadScene('scene_rapier2d');
|
||||
});
|
||||
}
|
||||
|
||||
update(dt: number) {
|
||||
if (!this._timeAccumulator) {
|
||||
this._timeAccumulator = 0;
|
||||
}
|
||||
this._timeAccumulator += dt;
|
||||
|
||||
if (this._timeAccumulator >= 0.1) {
|
||||
this.node_fps.getComponent(Label)!.string = `FPS: ${Math.floor(1 / dt)}`;
|
||||
this._timeAccumulator = 0;
|
||||
}
|
||||
|
||||
if(Math.floor(1 / dt) < 30){
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this._countAcc) {
|
||||
this._countAcc = 0;
|
||||
}
|
||||
// this._countAcc += 1;
|
||||
//每秒实例化添加5个node_item 到场景中,并且显示当前场景中的node_item数量
|
||||
// if (this._countAcc >= 60/60) {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
let node = instantiate(this.prefab_item);
|
||||
node.getComponent(Sprite).color = new Color(Math.random()*255, Math.random()*255, Math.random()*255);
|
||||
this.container_balls.addChild(node);
|
||||
let x = (Math.random()*2-1)*cc.view.getVisibleSize().width/2;;
|
||||
node.setPosition(x, 400);
|
||||
this._listBalls.push(node);
|
||||
}
|
||||
this.node_counts.getComponent(Label)!.string = `BALLS: ${this._listBalls.length}`;
|
||||
this._countAcc = 0;
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
9
tools/flashfin-rapier2d/assets/scripts/SceneMain.ts.meta
Normal file
9
tools/flashfin-rapier2d/assets/scripts/SceneMain.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "22878c0e-0345-4a10-896a-6c1520d22df5",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
180
tools/flashfin-rapier2d/assets/scripts/SceneRapier2d.ts
Normal file
180
tools/flashfin-rapier2d/assets/scripts/SceneRapier2d.ts
Normal file
@@ -0,0 +1,180 @@
|
||||
import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite } from 'cc';
|
||||
import * as cc from 'cc';
|
||||
|
||||
import RAPIER, { IntegrationParameters } from '@dimforge/rapier2d-compat';
|
||||
import { RAPIER_PTM_RATIO, RapierDebugRenderer } from './RapierDebugRenderer';
|
||||
import { logger } from './Clogger';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('SceneRapier2d')
|
||||
export class SceneRapier2d extends Component {
|
||||
|
||||
@property(Node)
|
||||
node_fps:Node|null = null;
|
||||
|
||||
@property(Node)
|
||||
node_counts:Node|null = null;
|
||||
|
||||
@property(Prefab)
|
||||
prefab_item:Prefab|null = null;
|
||||
|
||||
@property(Node)
|
||||
container_balls:Node|null = null;
|
||||
|
||||
@property(cc.Node)
|
||||
node_switch:cc.Node|null = null;
|
||||
|
||||
@property(cc.Node)
|
||||
node_switchscene:cc.Node|null = null;
|
||||
|
||||
|
||||
private _timeAccumulator: any;
|
||||
private _countAcc: any;
|
||||
|
||||
|
||||
private world: RAPIER.World;
|
||||
private gravity = new RAPIER.Vector2(0, -10);
|
||||
private _initComplete: boolean = false;
|
||||
private _rigidBody: RAPIER.RigidBody;
|
||||
private _debugRenderer: RapierDebugRenderer;
|
||||
|
||||
|
||||
//将一个node和一个RigidBody绑定
|
||||
private _mapNodeBalls: Map<Node, RAPIER.RigidBody> = new Map<Node, RAPIER.RigidBody>();
|
||||
|
||||
|
||||
protected async onLoad(): Promise<void> {
|
||||
logger.log('dballs onLoad');
|
||||
|
||||
RAPIER.init().then(() => {
|
||||
logger.log('RAPIER.init success');
|
||||
this._initComplete = true;
|
||||
// 创建物理世界
|
||||
this.world = new RAPIER.World(this.gravity);
|
||||
this.fillWorld();
|
||||
this._debugRenderer = this.getComponent(RapierDebugRenderer)!;
|
||||
this._debugRenderer.setWorld(this.world);
|
||||
|
||||
this.afterInit();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
afterInit() {
|
||||
|
||||
this.node_switch.children[0]!.getComponent(Label).string = this._debugRenderer.onoff ? 'debug:ON' : 'debug:OFF';
|
||||
this.node_switch.on('click', (b:cc.Button) => {
|
||||
this._debugRenderer.onoff = !this._debugRenderer.onoff;
|
||||
this._debugRenderer.clear();
|
||||
b!.node.children[0]!.getComponent(Label).string = this._debugRenderer.onoff ? 'debug:ON' : 'debug:OFF';
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
start() {
|
||||
logger.log('dballs start');
|
||||
|
||||
|
||||
|
||||
this.node_switchscene.on('click', (b:cc.Button) => {
|
||||
cc.director.loadScene('scene_box2d');
|
||||
});
|
||||
|
||||
// this.schedule((dt:number)=>{
|
||||
// if(!this._initComplete){
|
||||
// return;
|
||||
// }
|
||||
|
||||
// this.world.step();
|
||||
|
||||
// this._mapNodeBalls.forEach((rigidBody, node)=>{
|
||||
// let position = rigidBody.translation();
|
||||
// node.setPosition(position.x*RAPIER_PTM_RATIO, position.y*RAPIER_PTM_RATIO);
|
||||
// node.active = true;
|
||||
// });
|
||||
|
||||
|
||||
// }, 1/60, cc.macro.REPEAT_FOREVER, 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
fillWorld(){
|
||||
let world = this.world;
|
||||
let viewSize = cc.view.getVisibleSize();
|
||||
let groundRigidBody = world.createRigidBody(new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Fixed));
|
||||
world.createCollider(RAPIER.ColliderDesc.cuboid(viewSize.width/2, 0.5).setTranslation(0,-viewSize.height/(2*RAPIER_PTM_RATIO)).setRestitution(1).setFriction(0.0), groundRigidBody);
|
||||
|
||||
let groundRigidBody1 = world.createRigidBody(new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Fixed));
|
||||
world.createCollider(RAPIER.ColliderDesc.cuboid(0.5,viewSize.height/2 ).setTranslation(-viewSize.width/(2*RAPIER_PTM_RATIO),0).setRestitution(1).setFriction(0.0), groundRigidBody1);
|
||||
|
||||
let groundRigidBody2 = world.createRigidBody(new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Fixed));
|
||||
world.createCollider(RAPIER.ColliderDesc.cuboid(0.5,viewSize.height/2 ).setTranslation(viewSize.width/(2*RAPIER_PTM_RATIO),0).setRestitution(1).setFriction(0.0), groundRigidBody2);
|
||||
|
||||
// let rigidBody = world.createRigidBody(new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Dynamic).setTranslation(0.0, 5.0).setLinearDamping(0).setAngularDamping(0));
|
||||
// let collider = world.createCollider(RAPIER.ColliderDesc.ball(0.5).setDensity(1.0).setRestitution(1).setFriction(0.0), rigidBody);
|
||||
// this._rigidBody = rigidBody;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
update(dt: number) {
|
||||
if (!this._timeAccumulator) {
|
||||
this._timeAccumulator = 0;
|
||||
}
|
||||
this._timeAccumulator += dt;
|
||||
|
||||
// if (this._timeAccumulator >= 0.1) {
|
||||
this.node_fps.getComponent(Label)!.string = `FPS: ${Math.floor(1 / dt)}`;
|
||||
// this._timeAccumulator = 0;
|
||||
// }
|
||||
|
||||
if(Math.floor(1 / dt) < 30){
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this._countAcc) {
|
||||
this._countAcc = 0;
|
||||
}
|
||||
|
||||
//物理系统同步
|
||||
if(!this._initComplete){
|
||||
return;
|
||||
}
|
||||
this.world.step();
|
||||
this._mapNodeBalls.forEach((rigidBody, node)=>{
|
||||
let position = rigidBody.translation();
|
||||
node.setPosition(position.x*RAPIER_PTM_RATIO, position.y*RAPIER_PTM_RATIO);
|
||||
node.active = true;
|
||||
});
|
||||
|
||||
|
||||
// this._countAcc += 1;
|
||||
// if (this._countAcc >= 60/60) {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
let node = instantiate(this.prefab_item);
|
||||
node.getComponent(Sprite).color = new Color(Math.random()*255, Math.random()*255, Math.random()*255);
|
||||
node.active = false;
|
||||
this.container_balls.addChild(node);
|
||||
let x = (Math.random()*2-1)*cc.view.getVisibleSize().width/2;
|
||||
let y = 400;
|
||||
|
||||
let r = (node.getComponent(cc.UITransform).width-2)/2 ;
|
||||
let rigidBody = this.world.createRigidBody(new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Dynamic).setTranslation(x/RAPIER_PTM_RATIO, y/RAPIER_PTM_RATIO).setLinearDamping(0).setAngularDamping(0).setCanSleep(false));
|
||||
this.world.createCollider(RAPIER.ColliderDesc.ball(r/RAPIER_PTM_RATIO).setDensity(1.0).setRestitution(0.5).setFriction(0.0), rigidBody);
|
||||
this._mapNodeBalls.set(node, rigidBody);
|
||||
// node.setPosition(x, 400);
|
||||
// this._listBalls.push(node);
|
||||
|
||||
}
|
||||
this.node_counts.getComponent(Label)!.string = `BALLS: ${this.container_balls.children.length}`;
|
||||
// this._countAcc = 0;
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"ver":"4.0.24","importer":"typescript","imported":true,"uuid":"188ce8c7-8772-4533-8d62-ad630ea294eb","files":[],"subMetas":{},"userData":{}}
|
||||
9
tools/flashfin-rapier2d/assets/textures.meta
Normal file
9
tools/flashfin-rapier2d/assets/textures.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "01530936-dd18-4bfd-8c4b-7cb2ce4e87ed",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
BIN
tools/flashfin-rapier2d/assets/textures/ball1.png
(Stored with Git LFS)
Normal file
BIN
tools/flashfin-rapier2d/assets/textures/ball1.png
(Stored with Git LFS)
Normal file
Binary file not shown.
134
tools/flashfin-rapier2d/assets/textures/ball1.png.meta
Normal file
134
tools/flashfin-rapier2d/assets/textures/ball1.png.meta
Normal file
@@ -0,0 +1,134 @@
|
||||
{
|
||||
"ver": "1.0.27",
|
||||
"importer": "image",
|
||||
"imported": true,
|
||||
"uuid": "cd9e5dce-f1bc-4320-acc4-4acd8bb105e7",
|
||||
"files": [
|
||||
".json",
|
||||
".png"
|
||||
],
|
||||
"subMetas": {
|
||||
"6c48a": {
|
||||
"importer": "texture",
|
||||
"uuid": "cd9e5dce-f1bc-4320-acc4-4acd8bb105e7@6c48a",
|
||||
"displayName": "ball1",
|
||||
"id": "6c48a",
|
||||
"name": "texture",
|
||||
"userData": {
|
||||
"wrapModeS": "clamp-to-edge",
|
||||
"wrapModeT": "clamp-to-edge",
|
||||
"minfilter": "linear",
|
||||
"magfilter": "linear",
|
||||
"mipfilter": "none",
|
||||
"anisotropy": 0,
|
||||
"isUuid": true,
|
||||
"imageUuidOrDatabaseUri": "cd9e5dce-f1bc-4320-acc4-4acd8bb105e7",
|
||||
"visible": false
|
||||
},
|
||||
"ver": "1.0.22",
|
||||
"imported": true,
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {}
|
||||
},
|
||||
"f9941": {
|
||||
"importer": "sprite-frame",
|
||||
"uuid": "cd9e5dce-f1bc-4320-acc4-4acd8bb105e7@f9941",
|
||||
"displayName": "ball1",
|
||||
"id": "f9941",
|
||||
"name": "spriteFrame",
|
||||
"userData": {
|
||||
"trimThreshold": 1,
|
||||
"rotated": false,
|
||||
"offsetX": 0,
|
||||
"offsetY": 0,
|
||||
"trimX": 0,
|
||||
"trimY": 0,
|
||||
"width": 70,
|
||||
"height": 70,
|
||||
"rawWidth": 70,
|
||||
"rawHeight": 70,
|
||||
"borderTop": 0,
|
||||
"borderBottom": 0,
|
||||
"borderLeft": 0,
|
||||
"borderRight": 0,
|
||||
"packable": true,
|
||||
"pixelsToUnit": 100,
|
||||
"pivotX": 0.5,
|
||||
"pivotY": 0.5,
|
||||
"meshType": 0,
|
||||
"vertices": {
|
||||
"rawPosition": [
|
||||
-35,
|
||||
-35,
|
||||
0,
|
||||
35,
|
||||
-35,
|
||||
0,
|
||||
-35,
|
||||
35,
|
||||
0,
|
||||
35,
|
||||
35,
|
||||
0
|
||||
],
|
||||
"indexes": [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
1,
|
||||
3
|
||||
],
|
||||
"uv": [
|
||||
0,
|
||||
70,
|
||||
70,
|
||||
70,
|
||||
0,
|
||||
0,
|
||||
70,
|
||||
0
|
||||
],
|
||||
"nuv": [
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"minPos": [
|
||||
-35,
|
||||
-35,
|
||||
0
|
||||
],
|
||||
"maxPos": [
|
||||
35,
|
||||
35,
|
||||
0
|
||||
]
|
||||
},
|
||||
"isUuid": true,
|
||||
"imageUuidOrDatabaseUri": "cd9e5dce-f1bc-4320-acc4-4acd8bb105e7@6c48a",
|
||||
"atlasUuid": "",
|
||||
"trimType": "none"
|
||||
},
|
||||
"ver": "1.0.12",
|
||||
"imported": true,
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {}
|
||||
}
|
||||
},
|
||||
"userData": {
|
||||
"type": "sprite-frame",
|
||||
"hasAlpha": true,
|
||||
"fixAlphaTransparencyArtifacts": false,
|
||||
"redirect": "cd9e5dce-f1bc-4320-acc4-4acd8bb105e7@6c48a"
|
||||
}
|
||||
}
|
||||
132
tools/flashfin-rapier2d/engine-mangle-config.json
Normal file
132
tools/flashfin-rapier2d/engine-mangle-config.json
Normal file
@@ -0,0 +1,132 @@
|
||||
{
|
||||
"__doc_url__": "https://docs.cocos.com/creator/3.8/manual/zh/advanced-topics/mangle-properties.html",
|
||||
"COMMON": {
|
||||
"mangleProtected": false,
|
||||
"mangleList": [
|
||||
"UITransform._sortSiblings",
|
||||
"UITransform._cleanChangeMap",
|
||||
"Node._findComponents",
|
||||
"Node._findChildComponent",
|
||||
"Node._findChildComponents",
|
||||
"Node.idGenerator",
|
||||
"Node._stacks",
|
||||
"Node._stackId",
|
||||
"Node._setScene",
|
||||
"EffectAsset._layoutValid",
|
||||
"EffectAsset._effects",
|
||||
"ReflectionProbe.DEFAULT_CUBE_SIZE",
|
||||
"ReflectionProbe.DEFAULT_PLANER_SIZE",
|
||||
"WebGLDeviceManager.setInstance",
|
||||
"WebGL2DeviceManager.setInstance",
|
||||
"CanvasPool"
|
||||
],
|
||||
"dontMangleList": [
|
||||
"Component"
|
||||
]
|
||||
},
|
||||
"HTML5": {
|
||||
"extends": "COMMON",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"NATIVE": {
|
||||
"extends": "COMMON",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"MINIGAME": {
|
||||
"extends": "COMMON",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"WECHAT": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"BYTEDANCE": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"XIAOMI": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"ALIPAY": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"TAOBAO_MINIGAME": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"OPPO": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"VIVO": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"HUAWEI": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"MIGU": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"HONOR": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"COCOS_RUNTIME": {
|
||||
"extends": "MINIGAME",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"ANDROID": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"WINDOWS": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"IOS": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"MAC": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"OHOS": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"OPEN_HARMONY": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
},
|
||||
"LINUX": {
|
||||
"extends": "NATIVE",
|
||||
"mangleList": [],
|
||||
"dontMangleList": []
|
||||
}
|
||||
}
|
||||
19
tools/flashfin-rapier2d/package-lock.json
generated
Normal file
19
tools/flashfin-rapier2d/package-lock.json
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "phy2d-test",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "phy2d-test",
|
||||
"dependencies": {
|
||||
"@dimforge/rapier2d-compat": "^0.15.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dimforge/rapier2d-compat": {
|
||||
"version": "0.15.0",
|
||||
"resolved": "https://registry.npmjs.org/@dimforge/rapier2d-compat/-/rapier2d-compat-0.15.0.tgz",
|
||||
"integrity": "sha512-Jj3bSd225iKSLaZTd6Kh3nrA8X9Zj5FcfAVKYN1xizt8PPwHaGK2gY46arTQo7CmSqONfYvI+G8fPY6xP7SeiQ==",
|
||||
"license": "Apache-2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
10
tools/flashfin-rapier2d/package.json
Normal file
10
tools/flashfin-rapier2d/package.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "phy2d-test",
|
||||
"uuid": "49871c9c-fccf-4e92-8887-3966b708b800",
|
||||
"creator": {
|
||||
"version": "3.8.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"@dimforge/rapier2d-compat": "^0.15.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"__version__": "1.3.9"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"__version__": "3.0.9",
|
||||
"game": {
|
||||
"name": "未知游戏",
|
||||
"app_id": "UNKNOW",
|
||||
"c_id": "0"
|
||||
},
|
||||
"appConfigMaps": [
|
||||
{
|
||||
"app_id": "UNKNOW",
|
||||
"config_id": "6c525d"
|
||||
}
|
||||
],
|
||||
"configs": [
|
||||
{
|
||||
"app_id": "UNKNOW",
|
||||
"config_id": "6c525d",
|
||||
"config_name": "Default",
|
||||
"config_remarks": "",
|
||||
"services": []
|
||||
}
|
||||
]
|
||||
}
|
||||
3
tools/flashfin-rapier2d/settings/v2/packages/device.json
Normal file
3
tools/flashfin-rapier2d/settings/v2/packages/device.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"__version__": "1.0.1"
|
||||
}
|
||||
1108
tools/flashfin-rapier2d/settings/v2/packages/engine.json
Normal file
1108
tools/flashfin-rapier2d/settings/v2/packages/engine.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"__version__": "1.0.1",
|
||||
"information": {
|
||||
"customSplash": {
|
||||
"id": "customSplash",
|
||||
"label": "customSplash",
|
||||
"enable": true,
|
||||
"customSplash": {
|
||||
"complete": false,
|
||||
"form": "https://creator-api.cocos.com/api/form/show?sid=311ddac62fb285889cb7ed40438bbd50"
|
||||
}
|
||||
},
|
||||
"removeSplash": {
|
||||
"id": "removeSplash",
|
||||
"label": "removeSplash",
|
||||
"enable": true,
|
||||
"removeSplash": {
|
||||
"complete": false,
|
||||
"form": "https://creator-api.cocos.com/api/form/show?sid=311ddac62fb285889cb7ed40438bbd50"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"__version__": "1.0.4"
|
||||
}
|
||||
29
tools/flashfin-rapier2d/settings/v2/packages/project.json
Normal file
29
tools/flashfin-rapier2d/settings/v2/packages/project.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"__version__": "1.0.6",
|
||||
"general": {
|
||||
"designResolution": {
|
||||
"width": 1624,
|
||||
"height": 750,
|
||||
"fitHeight": true,
|
||||
"fitWidth": false
|
||||
}
|
||||
},
|
||||
"custom_joint_texture_layouts": [],
|
||||
"physics": {
|
||||
"collisionGroups": [
|
||||
{
|
||||
"index": 1,
|
||||
"name": "WALL"
|
||||
},
|
||||
{
|
||||
"index": 2,
|
||||
"name": "BALL"
|
||||
}
|
||||
],
|
||||
"collisionMatrix": {
|
||||
"0": 1,
|
||||
"1": 4,
|
||||
"2": 6
|
||||
}
|
||||
}
|
||||
}
|
||||
4
tools/flashfin-rapier2d/settings/v2/packages/scene.json
Normal file
4
tools/flashfin-rapier2d/settings/v2/packages/scene.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"__version__": "1.0.3",
|
||||
"current-scene": "452ca018-6ac2-414a-966a-166c7c8f4c30"
|
||||
}
|
||||
10
tools/flashfin-rapier2d/tsconfig.json
Normal file
10
tools/flashfin-rapier2d/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
/* Base configuration. Do not edit this field. */
|
||||
"extends": "./temp/tsconfig.cocos.json",
|
||||
|
||||
/* Add your custom configuration here. */
|
||||
"compilerOptions": {
|
||||
"strict": false,
|
||||
"allowSyntheticDefaultImports": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user