public static Vector2f Rotate(this Vector2f vector, double radians) { var vectorAngle = vector.Angle(); var length = vector.Length(); float x = (float)(length * Math.Cos(radians + vectorAngle)); float y = (float)(length * Math.Sin(radians + vectorAngle)); return(new Vector2f(x, y)); }
// Angle formed by two 2D vectors. public static float Angle(Vector2f a, Vector2f b) { return(Vector2f.Angle(a, b)); }
public override void OnUpdate() { // Not really accurate but f**k it Vector2f mousePos = Application.instance.cursorPosition; mousePos.X -= Application.instance.ScreenSize.X / 2; mousePos.Y -= Application.instance.ScreenSize.Y / 2; Vector2f lookVector = mousePos; float lookAngle = lookVector.Angle() * Mathf.RAD2DEG; _lookRotation = lookAngle; entity.sprite.rotation = lookAngle; light.SetCookieRotation(lookAngle); light.intensity = Random.Range(0.5f, 0.55f); float gbk = 1f - Mathf.Pow3(Mathf.Abs(hp - 1)); byte gb = (byte)(gbk * 255f); light.color = new Color(255, gb, gb); DebugOverlay.instance.Line("HP", hp); Vector2f motor = Control(); float mag = Mathf.Magnitude(motor.X, motor.Y); if (!Mathf.Approximately(mag, 0f)) { motor /= mag; float speed = 5; entity.body.velocity = motor * speed; // Walk anim //entity.sprite.textureRect = new IntRect(Game.TS * (1 + (world.timeMs / 50) % 4), Game.TS * _direction, 32, 32); } else { entity.body.velocity = new Vector2f(); // Idle //entity.sprite.textureRect = new IntRect(0, Game.TS * _direction, 32, 32); } // Enemies hurting IEnumerable <Entity> enemies = world.taggedEntities["Enemy"]; enemyDistance = 9999f; foreach (Entity e in enemies) { float d = e.body.position.DistanceTo(this.entity.body.position); if (d < enemyDistance) { enemyDistance = d; } } hurtState = enemyDistance < entity.body.hitbox.Width; if (hurtState) { hp -= world.delta * HP_LOSS_SPEED; if (hp <= 0) { Kill(); } } else { if (((LDWorld)world).dimension == 0) { hp += world.delta * HP_REGEN_SPEED; if (hp >= 1) { hp = 1; } } else { hp -= world.delta * HP_BADWORLD_LOSS_SPEED; if (hp <= 0) { Kill(); } } } }
public static float AngleInDegrees(this Vector2f vector) { return((float)(vector.Angle() / Math.PI * 180)); }