ConvexHullCollider instead of TriMesh

This commit is contained in:
azykov@mail.ru 2026-06-03 19:54:07 +03:00
parent 50a553b24d
commit b5a9772248
No known key found for this signature in database
7 changed files with 55 additions and 41 deletions

View File

@ -58,7 +58,7 @@ export const GameView = observer(function () {
return (<> return (<>
<Suspense> <Suspense>
{/* <PlayerMovement /> */} <PlayerMovement />
<Physics paused={game.isPaused}> <Physics paused={game.isPaused}>
<SceneView scene={game.scene} /> <SceneView scene={game.scene} />
</Physics> </Physics>

View File

@ -80,6 +80,7 @@ export const ObjectEditorView = observer(function ({ object }: ObjectEditorViewP
<ObjectViewInternal <ObjectViewInternal
ref={groupRef} ref={groupRef}
object={object} object={object}
isEditor
objectType={objectType} objectType={objectType}
onClick={handleClick} onClick={handleClick}
/> />

View File

@ -1,43 +1,36 @@
import { observer } from "mobx-react-lite"; import { observer } from "mobx-react-lite";
import type { ObjectType, RuntimeObjectInstance } from "../types"; import type { ObjectType, RuntimeObjectInstance } from "../types";
import { forwardRef, useMemo } from "react"; import { forwardRef } from "react";
import type { Group } from "three"; import type { Group } from "three";
import { Instance, Instances } from "@react-three/drei"; import { Instance, Instances } from "@react-three/drei";
import type { ThreeEvent } from "@react-three/fiber"; import type { ThreeEvent } from "@react-three/fiber";
import { state } from "../state"; import { ConvexHullCollider } from "@react-three/rapier";
import { TrimeshCollider } from "@react-three/rapier";
import { SyncRigidBody } from "./SyncRigidBody"; import { SyncRigidBody } from "./SyncRigidBody";
import { runInAction } from "mobx";
import { buildObjectTrimesh } from "../utils/graphics/mesh";
type ObjectViewInternalProps = { type ObjectViewInternalProps = {
object: Omit<RuntimeObjectInstance, 'typeId'>; object: Omit<RuntimeObjectInstance, 'typeId'>;
objectType: ObjectType; objectType: ObjectType;
isEditor?: boolean;
onClick?: (e: ThreeEvent<MouseEvent>) => void; onClick?: (e: ThreeEvent<MouseEvent>) => void;
} }
export const ObjectViewInternal = observer(forwardRef<Group | null, ObjectViewInternalProps>( export const ObjectViewInternal = observer(forwardRef<Group | null, ObjectViewInternalProps>(
function ({ object, objectType, onClick }, ref) { function ({ object, objectType, ...props }, ref) {
const trimeshArgs = useMemo(
() => buildObjectTrimesh(objectType, state.world.data.voxelTypes),
// eslint-disable-next-line react-hooks/exhaustive-deps
[objectType.id]
);
return ( return (
<SyncRigidBody <SyncRigidBody
colliders={false} colliders={false}
gravityScale={object.physics ? 1 : 0.1} gravityScale={object.physics ? 1 : 0.1}
onSync={(data) => { onSync={(_data) => {
runInAction(() => { // TODO rewrite so that is does not trigger re-render
const obj = state.game?.scene.objects[object.id]; // runInAction(() => {
if (obj) { // object.position = data.position;
obj.position = data.position; // object.rotation = data.rotation;
obj.rotation = data.rotation; // if (!props.isEditor) {
obj.linearVelocity = data.linearVelocity; // (object as RuntimeGameObjectInstance).linearVelocity = data.linearVelocity;
obj.radialVelocity = data.radialVelocity; // (object as RuntimeGameObjectInstance).radialVelocity = data.radialVelocity;
} // }
}); // });
}} }}
> >
<group <group
@ -46,7 +39,7 @@ export const ObjectViewInternal = observer(forwardRef<Group | null, ObjectViewIn
position={object.position} position={object.position}
rotation={object.rotation} rotation={object.rotation}
scale={object.scale} scale={object.scale}
onClick={onClick} onClick={props.onClick}
> >
{ {
object.cache.voxelGroups.map((vg) => object.cache.voxelGroups.map((vg) =>
@ -57,7 +50,7 @@ export const ObjectViewInternal = observer(forwardRef<Group | null, ObjectViewIn
</Instances> </Instances>
) )
} }
{trimeshArgs && <TrimeshCollider args={trimeshArgs} />} {object.cache.colliderMesh && <ConvexHullCollider args={[object.cache.colliderMesh[0]]} />}
</group> </group>
</SyncRigidBody> </SyncRigidBody>
); );

View File

@ -2,8 +2,6 @@ import { observer } from "mobx-react-lite";
import type { RuntimeScene } from "../types"; import type { RuntimeScene } from "../types";
import { CharacterView } from "./CharacterView"; import { CharacterView } from "./CharacterView";
import { GameObjectView } from "./GameObjectView"; import { GameObjectView } from "./GameObjectView";
import { useFrame } from "@react-three/fiber";
import { useRapier } from "@react-three/rapier";
import { ObjectEditorView } from "./ObjectEditorView"; import { ObjectEditorView } from "./ObjectEditorView";
type SceneViewProps = { type SceneViewProps = {
@ -12,9 +10,9 @@ type SceneViewProps = {
} }
export const SceneView = observer(function (props: SceneViewProps) { export const SceneView = observer(function (props: SceneViewProps) {
const rapier = useRapier(); // const rapier = useRapier();
useFrame((_, dt) => { // useFrame((_, dt) => {
// if (props.editMode) // if (props.editMode)
// return; // return;
@ -23,7 +21,7 @@ export const SceneView = observer(function (props: SceneViewProps) {
// return; // return;
// rapier.step(dt); // rapier.step(dt);
}) // })
return (<> return (<>
<ambientLight intensity={0.5} /> <ambientLight intensity={0.5} />

View File

@ -7,10 +7,16 @@ import { GameFactory } from "../model/gameFactory";
export class GameState { export class GameState {
private readonly world: WorldState; private readonly world: WorldState;
public data: Game; public isPaused: boolean = false;
public time: number = 0;
public scene: RuntimeGameScene;
private startAutoSave() { private startAutoSave() {
return reaction(() => toJS(this.data), (data) => this.saveData(data), { delay: 5000 }); return reaction(
() => (this.asGame),
(data) => this.saveData(data),
{ delay: 5000 }
);
} }
private withoutAutoSave(fn: () => void) { private withoutAutoSave(fn: () => void) {
@ -23,18 +29,31 @@ export class GameState {
constructor(world: WorldState) { constructor(world: WorldState) {
this.world = world; this.world = world;
this.data = GameFactory.create(toJS(this.world.data)); const game = GameFactory.create(toJS(this.world.data));
this.isPaused = game.paused;
this.time = game.time;
this.scene = game.scene;
this._stopAutoSave = this.startAutoSave(); this._stopAutoSave = this.startAutoSave();
makeAutoObservable(this); makeAutoObservable(this);
} }
public get isPaused(): boolean { public get asGame(): Game {
return this.data.paused; return {
paused: toJS(this.isPaused),
time: toJS(this.time),
scene: toJS(this.scene),
}
} }
public get scene(): RuntimeGameScene { public setGame(value: Game) {
return this.data.scene; this.isPaused = value.paused;
this.time = value.time;
this.scene = value.scene;
}
public setIsPaused(value: boolean) {
this.isPaused = value;
} }
public get camera(): Pos3 { public get camera(): Pos3 {
@ -63,19 +82,19 @@ export class GameState {
const stack = new Error('Saving game...').stack!.split('\n').slice(1); const stack = new Error('Saving game...').stack!.split('\n').slice(1);
const { ...debug } = toJS(data); const { ...debug } = toJS(data);
console.dir({ stack, debug }); console.dir({ stack, debug });
GameFactory.save(toJS(this.data)); GameFactory.save(data);
} }
public save(): void { public save(): void {
this.saveData(this.data); this.saveData(this.asGame);
} }
public resume(): void { public resume(): void {
this.data.paused = false; this.isPaused = false;
} }
public pause(): void { public pause(): void {
this.data.paused = true; this.isPaused = true;
} }
public setCharacterTransform( public setCharacterTransform(
@ -99,6 +118,6 @@ export class GameState {
if (this.isPaused) if (this.isPaused)
return; return;
this.data.time += deltaTime; this.time += deltaTime;
} }
} }

View File

@ -4,6 +4,7 @@ import type { ObjectInstance } from "./object";
export type ObjectInstanceRuntimeData = { export type ObjectInstanceRuntimeData = {
cache: { cache: {
voxelGroups: VoxelGroup[]; voxelGroups: VoxelGroup[];
colliderMesh: [Float32Array, Uint32Array] | null;
}; };
} }

View File

@ -1,5 +1,6 @@
import { getObjectVoxelGroups } from "../graphics/voxelGroup"; import { getObjectVoxelGroups } from "../graphics/voxelGroup";
import type { ObjectInstance, RuntimeObjectInstance, World } from "../../types"; import type { ObjectInstance, RuntimeObjectInstance, World } from "../../types";
import { buildObjectTrimesh } from "../graphics/mesh";
export function populateRuntimeObject(object: ObjectInstance, world: World): RuntimeObjectInstance { export function populateRuntimeObject(object: ObjectInstance, world: World): RuntimeObjectInstance {
const objectType = world.objectTypes[object.typeId]; const objectType = world.objectTypes[object.typeId];
@ -8,6 +9,7 @@ export function populateRuntimeObject(object: ObjectInstance, world: World): Run
...object, ...object,
cache: { cache: {
voxelGroups: getObjectVoxelGroups(objectType, world.voxelTypes), voxelGroups: getObjectVoxelGroups(objectType, world.voxelTypes),
colliderMesh: buildObjectTrimesh(objectType, world.voxelTypes),
}, },
}; };