Skip to content

Commit 5be3a84

Browse files
committed
drive in passanger seat
1 parent b689ec7 commit 5be3a84

File tree

10 files changed

+149
-73
lines changed

10 files changed

+149
-73
lines changed

build/assets/airplane.glb

0 Bytes
Binary file not shown.

build/assets/boxman.glb

156 KB
Binary file not shown.

build/assets/car.glb

4 Bytes
Binary file not shown.

build/assets/heli.glb

4 Bytes
Binary file not shown.

src/ts/characters/Character.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ export class Character extends THREE.Object3D implements IWorldEntity
555555

556556
public setCameraRelativeOrientationTarget(): void
557557
{
558-
// if (this.vehicleEntryInstance === null)
559-
// {
558+
if (this.vehicleEntryInstance === null)
559+
{
560560
let moveVector = this.getCameraRelativeMovementVector();
561561

562562
if (moveVector.x === 0 && moveVector.y === 0 && moveVector.z === 0)
@@ -567,7 +567,7 @@ export class Character extends THREE.Object3D implements IWorldEntity
567567
{
568568
this.setOrientation(moveVector);
569569
}
570-
// }
570+
}
571571
}
572572

573573
public rotateModel(): void
@@ -595,8 +595,18 @@ export class Character extends THREE.Object3D implements IWorldEntity
595595

596596
if (vehicleFinder.closestObject !== undefined)
597597
{
598-
let instance = new VehicleEntryInstance(this);
599-
instance.wantsToTransitionToDriverSeat = wantsToDrive;
598+
let vehicle = vehicleFinder.closestObject;
599+
this.vehicleEntryInstance = new VehicleEntryInstance(this);
600+
this.vehicleEntryInstance.wantsToTransitionToDriverSeat = wantsToDrive;
601+
602+
// Get seats' world positions
603+
let seatsWorldPositions = {};
604+
for (const seat of vehicle.seats)
605+
{
606+
let worldPos = new THREE.Vector3();
607+
seat.seatPointObject.getWorldPosition(worldPos);
608+
seatsWorldPositions[seat.seatPointObject.name] = worldPos;
609+
}
600610

601611
// Find best seat
602612
let seatFinder = new ClosestObjectFinder<SeatPoint>(this.position);
@@ -607,7 +617,7 @@ export class Character extends THREE.Object3D implements IWorldEntity
607617
// Consider driver seats
608618
if (seat.type === SeatType.Driver)
609619
{
610-
seatFinder.consider(seat, seat.seatPoint.position);
620+
seatFinder.consider(seat, seatsWorldPositions[seat.seatPointObject.name]);
611621
}
612622
// Consider passenger seats connected to driver seats
613623
else if (seat.type === SeatType.Passenger)
@@ -616,7 +626,7 @@ export class Character extends THREE.Object3D implements IWorldEntity
616626
{
617627
if (connSeat.type === SeatType.Driver)
618628
{
619-
seatFinder.consider(seat, seat.seatPoint.position);
629+
seatFinder.consider(seat, seatsWorldPositions[seat.seatPointObject.name]);
620630
break;
621631
}
622632
}
@@ -630,14 +640,14 @@ export class Character extends THREE.Object3D implements IWorldEntity
630640
// Consider passenger seats
631641
if (seat.type === SeatType.Passenger)
632642
{
633-
seatFinder.consider(seat, seat.seatPoint.position);
643+
seatFinder.consider(seat, seatsWorldPositions[seat.seatPointObject.name]);
634644
}
635645
}
636646
}
637647

638648
if (seatFinder.closestObject !== undefined)
639649
{
640-
instance.targetSeat = seatFinder.closestObject;
650+
this.vehicleEntryInstance.targetSeat = seatFinder.closestObject;
641651
this.triggerAction('up', true);
642652
}
643653
}

src/ts/characters/character_states/vehicles/EnteringVehicle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export class EnteringVehicle extends CharacterStateBase
4242
(this.seat.vehicle as unknown as THREE.Object3D).attach(this.character);
4343

4444
this.startPosition.copy(this.character.position);
45-
this.endPosition.copy(seat.seatPoint.position);
45+
this.endPosition.copy(seat.seatPointObject.position);
4646
this.endPosition.y += 0.6;
4747

4848
this.startRotation.copy(this.character.quaternion);
49-
this.endRotation.copy(this.seat.seatPoint.quaternion);
49+
this.endRotation.copy(this.seat.seatPointObject.quaternion);
5050
}
5151

5252
public update(timeStep: number): void

src/ts/data/SeatPoint.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,75 @@ import { Side } from '../enums/Side';
33
import { IControllable } from '../interfaces/IControllable';
44
import THREE = require('three');
55
import { VehicleDoor } from '../vehicles/VehicleDoor';
6+
import { Vehicle } from '../vehicles/Vehicle';
67

78
export class SeatPoint
89
{
9-
// TODO move init of these values in this class' constructor
1010
public vehicle: IControllable;
11-
public seatPoint: THREE.Object3D;
11+
public seatPointObject: THREE.Object3D;
12+
13+
// String of names of connected seats
14+
public connectedSeatsString: string;
15+
// Actual seatPoint objects, need to be identified
16+
// by parsing connectedSeatsString after all seats are imported
1217
public connectedSeats: SeatPoint[] = [];
18+
1319
public type: SeatType;
1420
public entryPoint: THREE.Object3D;
1521
public door: VehicleDoor;
1622
public doorSide: Side;
1723

18-
constructor(object: THREE.Object3D)
24+
constructor(vehicle: IControllable, object: THREE.Object3D, gltf: any)
1925
{
20-
this.seatPoint = object;
26+
this.vehicle = vehicle;
27+
this.seatPointObject = object;
28+
29+
if (object.hasOwnProperty('userData') && object.userData.hasOwnProperty('data'))
30+
{
31+
if (object.userData.hasOwnProperty('door_object'))
32+
{
33+
this.door = new VehicleDoor(this, gltf.scene.getObjectByName(object.userData.door_object));
34+
}
35+
36+
if (object.userData.hasOwnProperty('door_side'))
37+
{
38+
this.doorSide = object.userData.door_side;
39+
}
40+
else
41+
{
42+
console.error('Seat object ' + object + ' has no doorSide property.');
43+
}
44+
45+
if (object.userData.hasOwnProperty('entry_points'))
46+
{
47+
let entry_points = (object.userData.entry_points as string).split(';');
48+
for (const entry_point of entry_points)
49+
{
50+
if (entry_point.length > 0)
51+
{
52+
this.entryPoint = gltf.scene.getObjectByName(entry_point);
53+
}
54+
}
55+
}
56+
else
57+
{
58+
console.error('Seat object ' + object + ' has no entry point reference property.');
59+
}
60+
61+
if (object.userData.hasOwnProperty('seat_type'))
62+
{
63+
this.type = object.userData.seat_type;
64+
}
65+
else
66+
{
67+
console.error('Seat object ' + object + ' has no seat type property.');
68+
}
69+
70+
if (object.userData.hasOwnProperty('connected_seats'))
71+
{
72+
this.connectedSeatsString = object.userData.connected_seats;
73+
}
74+
}
2175
}
2276

2377
public update(timeStep: number): void

src/ts/vehicles/Vehicle.ts

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -380,64 +380,15 @@ export abstract class Vehicle extends THREE.Object3D
380380
{
381381
if (child.userData.data === 'seat')
382382
{
383-
let seat = new SeatPoint(child);
384-
seat.vehicle = this;
385-
386-
if (child.userData.hasOwnProperty('door_object'))
387-
{
388-
seat.door = new VehicleDoor(this, gltf.scene.getObjectByName(child.userData.door_object));
389-
}
390-
391-
if (child.userData.hasOwnProperty('door_side'))
392-
{
393-
seat.doorSide = child.userData.door_side;
394-
}
395-
else
396-
{
397-
console.error('Seat object ' + child + ' has no doorSide property.');
398-
}
399-
400-
if (child.userData.hasOwnProperty('entry_point'))
401-
{
402-
seat.entryPoint = gltf.scene.getObjectByName(child.userData.entry_point);
403-
}
404-
else
405-
{
406-
console.error('Seat object ' + child + ' has no entry point reference property.');
407-
}
408-
409-
if (child.userData.hasOwnProperty('seat_type'))
410-
{
411-
seat.type = child.userData.seat_type;
412-
}
413-
else
414-
{
415-
console.error('Seat object ' + child + ' has no seat type property.');
416-
}
417-
418-
this.seats.push(seat);
383+
this.seats.push(new SeatPoint(this, child, gltf));
419384
}
420385
if (child.userData.data === 'camera')
421386
{
422387
this.camera = child;
423388
}
424389
if (child.userData.data === 'wheel')
425390
{
426-
let wheel = new Wheel(child);
427-
428-
wheel.position = child.position;
429-
430-
if (child.userData.hasOwnProperty('steering'))
431-
{
432-
wheel.steering = (child.userData.steering === 'true');
433-
}
434-
435-
if (child.userData.hasOwnProperty('drive'))
436-
{
437-
wheel.drive = child.userData.drive;
438-
}
439-
440-
this.wheels.push(wheel);
391+
this.wheels.push(new Wheel(child));
441392
}
442393
if (child.userData.data === 'collision')
443394
{
@@ -474,5 +425,37 @@ export abstract class Vehicle extends THREE.Object3D
474425
{
475426
console.warn('Vehicle ' + typeof(this) + ' has no seats.');
476427
}
428+
else
429+
{
430+
this.connectSeats();
431+
}
432+
}
433+
434+
private connectSeats(): void
435+
{
436+
for (const firstSeat of this.seats)
437+
{
438+
if (firstSeat.connectedSeatsString !== undefined)
439+
{
440+
// Get list of connected seat names
441+
let conn_seat_names = firstSeat.connectedSeatsString.split(';');
442+
for (const conn_seat_name of conn_seat_names)
443+
{
444+
// If name not empty
445+
if (conn_seat_name.length > 0)
446+
{
447+
// Run through seat list and connect seats to this seat,
448+
// based on this seat's connected seats list
449+
for (const secondSeat of this.seats)
450+
{
451+
if (secondSeat.seatPointObject.name === conn_seat_name)
452+
{
453+
firstSeat.connectedSeats.push(secondSeat);
454+
}
455+
}
456+
}
457+
}
458+
}
459+
}
477460
}
478461
}

src/ts/vehicles/VehicleDoor.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import * as THREE from 'three';
22
import * as CANNON from 'cannon';
33
import { Vehicle } from './Vehicle';
44
import * as Utils from '../core/HelperFunctions';
5+
import { SeatPoint } from '../data/SeatPoint';
6+
import { Side } from '../enums/Side';
57

68
export class VehicleDoor
79
{
810
public vehicle: Vehicle;
11+
public seat: SeatPoint;
912
public doorObject: THREE.Object3D;
1013
public doorVelocity: number = 0;
1114
public doorWorldPos: THREE.Vector3 = new THREE.Vector3();
@@ -21,9 +24,20 @@ export class VehicleDoor
2124
public lastVehicleVel: THREE.Vector3 = new THREE.Vector3();
2225
public lastVehiclePos: THREE.Vector3 = new THREE.Vector3();
2326

24-
constructor(vehicle: Vehicle, object: THREE.Object3D)
27+
get sideMultiplier(): number
2528
{
26-
this.vehicle = vehicle;
29+
switch (this.seat.doorSide)
30+
{
31+
case Side.Right: return 1;
32+
case Side.Left: return -1;
33+
default: return 0;
34+
}
35+
}
36+
37+
constructor(seat: SeatPoint, object: THREE.Object3D)
38+
{
39+
this.seat = seat;
40+
this.vehicle = seat.vehicle as Vehicle;
2741
this.doorObject = object;
2842
}
2943

@@ -57,7 +71,7 @@ export class VehicleDoor
5771
}
5872
}
5973

60-
this.doorObject.setRotationFromEuler(new THREE.Euler(0, -this.rotation, 0));
74+
this.doorObject.setRotationFromEuler(new THREE.Euler(0, this.sideMultiplier * this.rotation, 0));
6175
}
6276

6377
public preStepCallback(): void
@@ -77,7 +91,7 @@ export class VehicleDoor
7791
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(quat);
7892

7993
// Get imaginary positions
80-
let trailerPos = back.clone().applyAxisAngle(up, -this.rotation).add(this.doorWorldPos);
94+
let trailerPos = back.clone().applyAxisAngle(up, this.sideMultiplier * this.rotation).add(this.doorWorldPos);
8195
let trailerPushedPos = trailerPos.clone().sub(vehicleVelDiff);
8296

8397
// Update last values
@@ -90,19 +104,19 @@ export class VehicleDoor
90104
let angle = Utils.getSignedAngleBetweenVectors(v1, v2, up);
91105

92106
// Apply door velocity
93-
this.doorVelocity -= angle * 0.05;
107+
this.doorVelocity += this.sideMultiplier * angle * 0.05;
94108
this.rotation += this.doorVelocity;
95109

96110
// Bounce door when it reaches rotation limit
97111
if (this.rotation < 0)
98112
{
99113
this.rotation = 0;
100-
this.doorVelocity = -this.doorVelocity;
114+
this.doorVelocity = -this.doorVelocity / 2;
101115
}
102116
if (this.rotation > 1)
103117
{
104118
this.rotation = 1;
105-
this.doorVelocity = -this.doorVelocity;
119+
this.doorVelocity = -this.doorVelocity / 2;
106120
}
107121

108122
// Damping

src/ts/vehicles/Wheel.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,20 @@ export class Wheel
1111
constructor(wheelObject: THREE.Object3D)
1212
{
1313
this.wheelObject = wheelObject;
14+
15+
this.position = wheelObject.position;
16+
17+
if (wheelObject.hasOwnProperty('userData') && wheelObject.userData.hasOwnProperty('data'))
18+
{
19+
if (wheelObject.userData.hasOwnProperty('steering'))
20+
{
21+
this.steering = (wheelObject.userData.steering === 'true');
22+
}
23+
24+
if (wheelObject.userData.hasOwnProperty('drive'))
25+
{
26+
this.drive = wheelObject.userData.drive;
27+
}
28+
}
1429
}
1530
}

0 commit comments

Comments
 (0)