#[SOLVED] ApplyImpulse clamping

1 messages · Page 1 of 1 (latest)

quasi garnet
#

Problem:
It overshoots, whenever a faster enemy is "moving", it overshoots.
How do i clamp the velocity, so it doesn't overshoots anymore. IM not good with math stuff.

EnemySpeed when testing: 0.1

private moveAlongPath(entity: Entity): void {
        const move = () => {
            if (Game.gameStatus === GameStatus.LOST || !this.enemyMap.has(entity)) return;
            const enemy = this.enemyMap.get(entity)!;
            const { pathGroupIndex, waypointIndex } = enemy;
            const enemyPath = this.enemyPathGroups[pathGroupIndex];

            if (waypointIndex >= enemyPath.length) {
                enemy.reachedExit();
                this.removeEnemy(enemy);
                return;
            }

            const target = enemyPath[waypointIndex];
            const position = entity.location;

            // get relative distance
            const direction = {
                x: target.x - position.x,
                y: target.y - position.y,
                z: target.z - position.z
            };

            // get distance
            const distance = Math.sqrt(
                direction.x ** 2 +
                direction.y ** 2 +
                direction.z ** 2
            );

            // if we're close enough, move to the next waypoint
            if (distance < 0.2 /* epsilon */) {
                enemy.waypointIndex += 1;
                this.enemyMap.set(entity, enemy);
                system.run(move);
            } else {
                const velocity = {
                    x: direction.x / distance * enemy.speed * EnemyManager.tickDelay,
                    y: direction.y / distance * enemy.speed * EnemyManager.tickDelay,
                    z: direction.z / distance * enemy.speed * EnemyManager.tickDelay
                };

                entity.applyImpulse(velocity);
                entity.lookAt(target);
                system.runTimeout(move, EnemyManager.tickDelay);
            }
        };

        move();
    }
#

Would be nice, if you got some time to help me out with this. @rustic scarab

#

ApplyImpulse clamping

quasi garnet
#

@viscid cradle I hope the @ in your name means I can ping you.
-# if not, i'm sorry for bothering you

Would you mind helping me out real quick?

arctic leaf
#

|| There is clearly not enough patience ||

quasi garnet
quasi garnet
arctic leaf
#

Tell more about what you want to do.

arctic leaf
quasi garnet
#

shit..

arctic leaf
quasi garnet
#

Alright -

#

This is a enemy movement system for my tower defense

#

but if the enemies speed is too high

#

it overshots when moving

#

and I want to

#

uhhhh

#

english skills are lacking, wait

#

Impede

#

I want to impede the overshooting

#

by "clamping" the velocity input

#

but im not good with maths, so i cant do that

arctic leaf
#

What is the structure of the path data. I mean, how is it stored? Just an array of Vector3?

quasi garnet
#

Yes, here the interface

#
export interface ILevel {
    id: string;
    displayName: string;
    towerSlots: Vector3[];
    enemyPaths: Vector3[][];
    waves: IWave[];
    structureOptions: {
        id: string;
        position: Vector3;
    };
}
#

a path is basically Vector3[]

#

dont mind the Vector[][]

#

these are the points, he tries to move to

arctic leaf
#

hmm

quasi garnet
#

Maybe it isnt even really possible with applyImpulse

arctic leaf
# quasi garnet Maybe it isnt even really possible with applyImpulse

Well, you can do this: the function step(enemy, count), which takes an Enemy object and a non-integer number of steps (can be made absolute), takes a location with the index Math.floor(step) and adds to it (step - Math.floor(step)) * 100% of the next location. That is, for step = 0.5, the location will be equal to path[0] + 50% of path[1]. and then just teleports the enemy to this point. The rotation of the head is determined using the next location (i.e. relative path[0] to path[1])

quasi garnet
#

ill try this, thanks