Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by fulvio

I have the following method inside my EntityProjectile class:

attractEntities: function(entityTypes) {
    for (var i = 0; i < entityTypes.length; i++) {
        // Get all entities of this type.
        var entities = ig.game.getEntitiesByType(entityTypes[i]);

        // Entities found.
        if (entities) {
            // All entities.
            for (var j = 0; j < entities.length; j++) {
                // Attract these entities.
                if (entities[j]) {
                    var distance = this.distanceTo(entities[j]);
                    var thisSizeX = 300; // this.size.x * 2;
                    if (this.distanceTo(entities[j]) <= thisSizeX) {
                        var angle = this.angleTo(entities[j]);
                        this.currentAnim.angle = angle;
                        this.vel.x = Math.cos(angle) * 20000;
                        this.vel.y = Math.sin(angle) * 20000;
                    }
                }
            }
        }
    }
}

I use it like the following:

EntityProjectile:

attractedEntities: new Array(),
init: function(x, y, settings) {
    this.parent(x, y, settings);

    // Entities to attract.            
    this.attractedEntities.push(EntityBossEnemy);
}

I then call the method within the update() method:

update: function() {
    // Array of enemy entity types which will attract projectile.
    this.attractEntities(this.attractedEntities);

    this.parent();
}

This works quite well, however I&039;m not convinced it's the best way of attracting the #EntityProjectile towards an enemy Entity. I'm not getting the desired outcome I'm after unfortunately.

The desired outcome is displayed here in the absolutely incredible game by the name of The Iconoclasts when you're taking out bad guys.

The Iconoclasts Game Play

Notice how the angle of the projectile is always facing towards an enemy? That, my friends, is what I'm after! :P

There's also quite a few problems when two enemies are close to each other. The projectile just doesn't know which enemy to attract to considering I'm looping through every single enemy entity in the game. *ouch*

1 decade ago by TigerJ

inside of here is the code you are looking for, but you have to reverse engineer my script that fires this entity towards the mouse

var mx = ig.input.mouse.x + ig.game.screen.x;
        var my = ig.input.mouse.y + ig.game.screen.y;
        var mouseAngle =  Math.atan2(
            my - (this.pos.y + this.size.y/2),
            mx - (this.pos.x + this.size.x/2)
        );
        this.vel.x = Math.cos(mouseAngle)*300;
        this.vel.y = Math.sin(mouseAngle)*300;

I'm in the middle of some stuff so I can't dissect it at the moment, I can however come back later and update this post when I have more time. Sorry.

1 decade ago by TigerJ

So to angle your object at the player the player would be the mouse
var mx = <entity you want attacted to>.pos.x;
        var my = <entity you want attracted to>.pos.y;
        var yourAngle =  Math.atan2(
            my - (<angled entity>.pos.y + this.size.y/2),
            mx - (<angled entity>.pos.x + this.size.x/2)
        );
        this.currentAnim.angle=mouseAngle;

this should work**
if not maybe put those gamescreen calls back in and see. atan2 is what you want for sure.

http://www.w3schools.com/jsref/jsref_atan2.asp

ACTUALLY I think that this would work
<attracted entity>.currentAnim.angle=Math.atan2(<attractor>.pos.x,<attractor>.pos.y);

you may need to compensate it (the attractor) size to get the true middle.

pos.x + size.x/2
pos.y + size.y/2

1 decade ago by fulvio

I ended up going with this. Works much better than my implementation that's for sure. It still needs a little tweaking, but it's getting there.

The only problem is that the angle changes even if an enemy is separated by a tile and the player. How can I detect for a tile inbetween the player and the enemy entity?

attractEntities: function(entityTypes) {
    for (var i = 0; i < entityTypes.length; i++) {
        // Get all entities of this type.
        var entities = ig.game.getEntitiesByType(entityTypes[i]);

        // Entities found.
        if (entities) {
            // All entities.
            for (var j = 0; j < entities.length; j++) {
                // Attract these entities.
                if (entities[j]) {
                    var distance = this.distanceTo(entities[j]);
                    var thisSizeX = 100;
                    if (this.distanceTo(entities[j]) <= thisSizeX) {
                        var mx = entities[j].pos.x;
                        var my = entities[j].pos.y;
                        var mouseAngle =  Math.atan2(
                            my - (this.pos.y + this.size.y / 2),
                            mx - (this.pos.x + this.size.x / 2)
                        );
                        this.currentAnim.angle = mouseAngle;
                        this.vel.x = Math.cos(mouseAngle) * 300;
                        this.vel.y = Math.sin(mouseAngle) * 300;
                    }
                    /* Old method:
                    var distance = this.distanceTo(entities[j]);
                    var thisSizeX = 100;
                    if (this.distanceTo(entities[j]) <= thisSizeX) {
                        var angle = this.angleTo(entities[j]);
                        this.currentAnim.angle = angle;
                        this.vel.x = Math.cos(angle) * 200;
                        this.vel.y = Math.sin(angle) * 200;
                    }*/
                }
            }
        }
    }
}

Thank you very much for your help so far.

1 decade ago by TigerJ

probably using some trace http://impactjs.com/documentation/class-reference/collisionmap#trace

if there is a collision in the trace then you can rotate it accordingly. I took trig twice this is hurting my brain.

(failed the first time because I was making games on my ti-83 :D where was impact ti-83+ edition )
Page 1 of 1
« first « previous next › last »