object touch events implemented

This commit is contained in:
azykov@mail.ru
2026-06-13 13:18:03 +03:00
parent 209ff3f710
commit 1ed3203ec5
6 changed files with 40 additions and 13 deletions
@@ -1,4 +1,4 @@
import type { ObjectType, RuntimeGameObjectInstance, RuntimeObjectInstance } from "../types";
import type { ObjectType, ObjectInstanceRigidBody, RuntimeGameObjectInstance, RuntimeObjectInstance } from "../types";
import { useEffect, useImperativeHandle, useRef, type Ref } from "react";
import { Euler, Quaternion, type Group } from "three";
import { Instance, Instances } from "@react-three/drei";
@@ -7,6 +7,7 @@ import { ConvexHullCollider, RapierRigidBody } from "@react-three/rapier";
import { SyncRigidBody } from "./SyncRigidBody";
import { reaction, runInAction } from "mobx";
import { v3toRapier } from "../utils";
import { state } from "../state";
export type ObjectViewInternalHandle = {
group: Group | null;
@@ -56,7 +57,7 @@ export const ObjectViewInternal = function ({ object, objectType, ref, ...props
},
[object, object.id, props.isPlayer]
);
const gameObj = object as RuntimeGameObjectInstance;
useEffect(
@@ -84,6 +85,19 @@ export const ObjectViewInternal = function ({ object, objectType, ref, ...props
gravityScale={object.gravityScale}
position={object.position}
rotation={object.rotation}
onCollisionEnter={props.isEditor ? undefined : (payload) => {
const game = state.game;
if (!game)
return;
const otherHandle = payload.other.rigidBody?.handle;
if (otherHandle === undefined)
return;
const otherObj = Object.values(game.scene.objects)
.find((o) => o.rb()?.handle === otherHandle);
game.emitEvent('object_touch_start', { object: gameObj, other: otherObj });
if (otherObj?.id === game.scene.playerObjectId)
game.emitEvent('player_touch_start', { object: gameObj, other: otherObj });
}}
onSync={(data) => {
runInAction(() => {
object.position = data.position;