I am working on a drag and drop system. Right now when I use the preview and click on the object it goes to the center of the screen as it should, and then the dropped/idle calls and the object drops to the ground again. Any insights in what the issue can be?
// This is a component file. You can use this file to define a custom component for your project.
// This component will appear as a custom component in the editor.
import * as ecs from '@8thwall/ecs' // This is how you access the ecs library.
ecs.registerComponent({
name: 'Draggable',
schema: {
camera: ecs.eid,
distanceToCamera: ecs.f32,
followSpeed: ecs.f32,
},
schemaDefaults: {
distanceToCamera: 3,
followSpeed: 50,
},
data: {
touchPositionX: ecs.f32,
touchPositionY: ecs.f32,
},
add: (world, component) => {
// Init data
const {eid, dataAttribute} = component
// Set to middle screen
component.data.touchPositionX = 0.5
component.data.touchPositionY = 0.5
// Get mass data for later caching
},
stateMachine: ({world, eid, schemaAttribute}) => {
ecs.defineState('idle')
.initial()
.onEnter(() => {
ecs.Collider.set(world, eid, {mass: 1})
console.log('Idle')
})
.onEvent(ecs.input.SCREEN_TOUCH_START, 'following', {
target: eid,
})
ecs.defineState('following')
.onEnter(() => {
ecs.Collider.set(world, eid, {mass: 0})
console.log('dragging')
})
.onTick(() => {
const schema = schemaAttribute.cursor(eid)
const {camera, distanceToCamera, followSpeed} = schema
// Get the camera's position and rotation
const cameraPosition = ecs.Position.get(world, camera)
const cameraRotation = ecs.Quaternion.get(world, camera)
// Get screen size
// Calculate relative position of object
// Calculate the forward vector from the camera's rotation
const forward = {
x: 2 * (cameraRotation.x * cameraRotation.z + cameraRotation.w * cameraRotation.y),
y: 2 * (cameraRotation.y * cameraRotation.z - cameraRotation.w * cameraRotation.x),
z: 1 - 2 * (cameraRotation.x * cameraRotation.x + cameraRotation.y * cameraRotation.y),
}
// Scale the forward vector by the desired distance
const forwardOffset = {
x: forward.x * distanceToCamera,
y: forward.y * distanceToCamera,
z: forward.z * distanceToCamera,
}
// Calculate the new position in front of the camera
const newPosition = {
x: cameraPosition.x + forwardOffset.x,
y: cameraPosition.y + forwardOffset.y,
z: cameraPosition.z + forwardOffset.z,
}
// Smoothly interpolate towards the target position (if followSpeed is used)
const currentPosition = ecs.Position.get(world, eid)
const smoothedPosition = {
x: currentPosition.x + (newPosition.x - currentPosition.x) * (followSpeed / 100),
y: currentPosition.y + (newPosition.y - currentPosition.y) * (followSpeed / 100),
z: currentPosition.z + (newPosition.z - currentPosition.z) * (followSpeed / 100),
}
// Set the new position for the object
ecs.Position.set(world, eid, {x: smoothedPosition.x, y: smoothedPosition.y, z: smoothedPosition.z})
})
// .listen(eid, ecs.input.SCREEN_TOUCH_MOVE, handleFollow)
.onEvent(ecs.input.SCREEN_TOUCH_END, 'idle', {
target: eid,
})
},
})