Skip to content

Commit

Permalink
Improve initRigidBody and initCollider API (parameters trully optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Aug 1, 2024
1 parent 854b295 commit 80b6fdf
Show file tree
Hide file tree
Showing 31 changed files with 780 additions and 580 deletions.
13 changes: 11 additions & 2 deletions apps/playground-2d/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import './style.css'
import { FCircle, FScene2d, FSprite, FSquare } from '@fibbojs/2d'
import { F2dShapes, FCircle, FScene2d, FSprite, FSquare } from '@fibbojs/2d'
import MySquare from './classes/MySquare'

(async () => {
const scene = new FScene2d({ debug: true })
await scene.init()
await scene.initPhysics()

// Create the ground
const ground = new FSquare(scene)
ground.setPosition(0, 0)
ground.setScale(10, 0.1)
ground.initCollider()
scene.addComponent(ground)

const square = new MySquare(scene)
scene.addComponent(square)

Expand All @@ -19,7 +26,9 @@ import MySquare from './classes/MySquare'

const square3 = new FSquare(scene)
square3.setPosition(4, 1)
square3.initCollider()
square3.initCollider({
shape: F2dShapes.CIRCLE,
})
scene.addComponent(square3)

const square4 = new FSquare(scene)
Expand Down
16 changes: 0 additions & 16 deletions apps/playground-3d/src/classes/MyCube.ts

This file was deleted.

14 changes: 0 additions & 14 deletions apps/playground-3d/src/classes/MySphere.ts

This file was deleted.

59 changes: 56 additions & 3 deletions apps/playground-3d/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { F3dShapes, FCube, FGameCamera, FScene3d, FSphere } from '@fibbojs/3d'
import Duck from './classes/Duck'
import GltfCube from './classes/GltfCube'
import './style.css'
import { FCube, FGameCamera, FScene3d } from '@fibbojs/3d'

(async () => {
// Initialize the scene
const scene = new FScene3d()
const scene = new FScene3d({ debug: true })
await scene.init()
await scene.initPhysics()

Expand All @@ -17,9 +19,60 @@ import { FCube, FGameCamera, FScene3d } from '@fibbojs/3d'

// Create a cube
const cube = new FCube(scene)
cube.initRigidBody()
cube.setColor(0xA0FFA0)
cube.setPosition(-5, 5, 5)
cube.initRigidBody({
shape: F3dShapes.SPHERE,
})
scene.addComponent(cube)

const sphere = new FSphere(scene)
sphere.setPosition(2, 4, -2)
sphere.initRigidBody()
scene.addComponent(sphere)

const duck = new Duck(scene)
scene.addComponent(duck)

const gltfCube = new GltfCube(scene)
scene.addComponent(gltfCube)

// Create 8 cubes dynamically in circle from 0 to 2PI
for (let i = 0; i < 8; i++) {
const angle = i * Math.PI / 4
const x = Math.cos(angle) * 4
const z = Math.sin(angle) * 4
const cube = new FCube(scene)
cube.setPosition(x, 1, z)
cube.initRigidBody()
scene.addComponent(cube)
}

scene.camera = new FGameCamera(gltfCube, scene)

// Detect inputs to move the cube
document.addEventListener('keydown', (event) => {
const impulse = { x: 0, y: 0, z: 0 }
switch (event.key) {
case 'ArrowUp':
impulse.z = -1
break
case 'ArrowDown':
impulse.z = 1
break
case 'ArrowLeft':
impulse.x = -1
break
case 'ArrowRight':
impulse.x = 1
break
case ' ':
cube.rigidBody?.applyImpulse({ x: 0, y: 5, z: 0 }, true)
break
}
cube.rigidBody?.applyImpulse(impulse, true)
})

// Attach a camera to the cube
scene.camera = new FGameCamera(cube, scene)
})()
90 changes: 49 additions & 41 deletions docs/api/2d/classes/FCircle.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,33 @@ scene.addComponent(circle)

#### Defined in

[packages/2d/src/polygons/FCircle.ts:20](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/polygons/FCircle.ts#L20)
[packages/2d/src/polygons/FCircle.ts:20](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/polygons/FCircle.ts#L20)

## Methods

### initCollider()

> **initCollider**(`position`?, `scale`?, `rotation`?, `shape`?): `void`
> **initCollider**(`options`?): `void`
#### Parameters

**position?**: `PointData`
**options?**

The position of the collider.
The options for the collider.

**scale?**: `PointData`
**options.position?**: `PointData`

The scale of the collider.
The position of the collider.

**rotation?**: `number`
**options.rotation?**: `number`

The rotation of the collider.

**shape?**: [`F2dShapes`](../enumerations/F2dShapes.md) = `F2dShapes.CIRCLE`
**options.scale?**: `PointData`

The scale of the collider.

**options.shape?**: [`F2dShapes`](../enumerations/F2dShapes.md)

The shape of the collider.

Expand All @@ -79,12 +83,12 @@ This is useful for static objects.
#### Example

```ts
component.initCollider(
new PIXI.Point(0, 0),
new PIXI.Point(1, 1),
0,
F2dShapes.SQUARE
)
component.initCollider({
position: new PIXI.Point(0, 0),
scale: new PIXI.Point(1, 1),
rotation: 0,
shape: F2dShapes.SQUARE
})
```

#### Overrides
Expand All @@ -93,29 +97,33 @@ component.initCollider(

#### Defined in

[packages/2d/src/polygons/FCircle.ts:41](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/polygons/FCircle.ts#L41)
[packages/2d/src/polygons/FCircle.ts:44](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/polygons/FCircle.ts#L44)

***

### initRigidBody()

> **initRigidBody**(`position`?, `scale`?, `rotation`?, `shape`?): `void`
> **initRigidBody**(`options`?): `void`
#### Parameters

**position?**: `PointData`
**options?**

The position of the rigid body.
The options for the rigid body.

**scale?**: `PointData`
**options.position?**: `PointData`

The scale of the rigid body.
The position of the rigid body.

**rotation?**: `number`
**options.rotation?**: `number`

The rotation of the rigid body.

**shape?**: [`F2dShapes`](../enumerations/F2dShapes.md) = `F2dShapes.CIRCLE`
**options.scale?**: `PointData`

The scale of the rigid body.

**options.shape?**: [`F2dShapes`](../enumerations/F2dShapes.md)

The shape of the rigid body.

Expand All @@ -130,12 +138,12 @@ Init a rigid body for the model.
#### Example

```ts
component.initRigidBody(
new PIXI.Point(0, 0),
new PIXI.Point(1, 1),
0,
F2dShapes.SQUARE
)
component.initRigidBody({
position: new PIXI.Point(0, 0),
scale: new PIXI.Point(1, 1),
rotation: 0,
shape: F2dShapes.SQUARE
})
```

#### Overrides
Expand All @@ -144,7 +152,7 @@ component.initRigidBody(

#### Defined in

[packages/2d/src/polygons/FCircle.ts:32](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/polygons/FCircle.ts#L32)
[packages/2d/src/polygons/FCircle.ts:32](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/polygons/FCircle.ts#L32)

***

Expand All @@ -171,7 +179,7 @@ Should be called every frame.

#### Defined in

[packages/2d/src/polygons/FCircle.ts:28](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/polygons/FCircle.ts#L28)
[packages/2d/src/polygons/FCircle.ts:28](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/polygons/FCircle.ts#L28)

***

Expand Down Expand Up @@ -209,7 +217,7 @@ component.setPosition(0, 0)

#### Defined in

[packages/2d/src/FComponent2d.ts:100](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L100)
[packages/2d/src/FComponent2d.ts:100](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L100)

***

Expand Down Expand Up @@ -243,7 +251,7 @@ component.setRotation(Math.PI / 2)

#### Defined in

[packages/2d/src/FComponent2d.ts:131](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L131)
[packages/2d/src/FComponent2d.ts:131](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L131)

***

Expand Down Expand Up @@ -277,7 +285,7 @@ component.setRotationDegree(90)

#### Defined in

[packages/2d/src/FComponent2d.ts:144](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L144)
[packages/2d/src/FComponent2d.ts:144](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L144)

***

Expand Down Expand Up @@ -315,7 +323,7 @@ component.setScale(1, 1)

#### Defined in

[packages/2d/src/FComponent2d.ts:114](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L114)
[packages/2d/src/FComponent2d.ts:114](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L114)

## Properties

Expand All @@ -331,7 +339,7 @@ RAPIER Collider

#### Defined in

[packages/2d/src/FComponent2d.ts:46](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L46)
[packages/2d/src/FComponent2d.ts:46](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L46)

***

Expand All @@ -347,7 +355,7 @@ PIXI container

#### Defined in

[packages/2d/src/FComponent2d.ts:22](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L22)
[packages/2d/src/FComponent2d.ts:22](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L22)

***

Expand All @@ -363,7 +371,7 @@ Position of the component.

#### Defined in

[packages/2d/src/FComponent2d.ts:28](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L28)
[packages/2d/src/FComponent2d.ts:28](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L28)

***

Expand All @@ -379,7 +387,7 @@ RAPIER RigidBody

#### Defined in

[packages/2d/src/FComponent2d.ts:42](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L42)
[packages/2d/src/FComponent2d.ts:42](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L42)

***

Expand All @@ -395,7 +403,7 @@ Rotation of the component.

#### Defined in

[packages/2d/src/FComponent2d.ts:36](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L36)
[packages/2d/src/FComponent2d.ts:36](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L36)

***

Expand All @@ -411,7 +419,7 @@ Scale of the component.

#### Defined in

[packages/2d/src/FComponent2d.ts:32](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L32)
[packages/2d/src/FComponent2d.ts:32](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L32)

***

Expand All @@ -427,4 +435,4 @@ The scene which the component is in.

#### Defined in

[packages/2d/src/FComponent2d.ts:17](https://github.com/fibbojs/fibbo/blob/2fc7696bf6e72ce4d25b27bb8d1ec5dce7632448/packages/2d/src/FComponent2d.ts#L17)
[packages/2d/src/FComponent2d.ts:17](https://github.com/fibbojs/fibbo/blob/854b295adc4e4ce7db9ffee939bdf1bce33bc12e/packages/2d/src/FComponent2d.ts#L17)
Loading

0 comments on commit 80b6fdf

Please sign in to comment.