public override bool Shoot(Player player, ref Microsoft.Xna.Framework.Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack) { Vector2 direction = new Vector2(speedX, speedY); direction.Normalize(); position += direction * item.width; return true; }
public static float AngleBetween(Vector2 v1, Vector2 v2) { v1.Normalize(); v2.Normalize(); float Angle = (float)Math.Acos(Vector2.Dot(v1, v2)); return Angle; }
public Point ClosestPointTo(Point point) { Vector2 unitSegment = new Vector2((float)(this.Point2.X - this.Point1.X), (float)(Point2.Y - Point1.Y)); unitSegment.Normalize(); Vector2 pointVector = new Vector2((float)(point.X - this.Point1.X), (float)(point.Y - Point1.Y)); // Segment pointSegment = new Segment( new Point(0,0), // new Point(point.x - this.Point1.X, point.y - Point1.Y)); float l = Vector2.Dot(pointVector, unitSegment); if (l < 0) { return(this.Point1); } else if (l * l > this.GetLengthSquared()) { return(this.Point2); } else { Point newPoint = new Point(Point1.X + l * unitSegment.X, Point1.Y + l * unitSegment.Y); return(newPoint); } }
public Bullet(Game game, Vector2 spawnpoint, Vector2 impulse,ABulletStats stats,Vector2 parentVelocity,bool friendly = false) : base(game,spawnpoint,friendly) { _parentVelocity = parentVelocity; _stats = stats; _txture = _textures.ProjectileTxture; _impulse = impulse; _impulse.Normalize(); int plusminus = Tools.PlusOrMinus(); _impulse += new Vector2(_impulse.Y*plusminus,_impulse.X*plusminus*-1)*(float)MyRandom.Random.NextDouble()*stats.Inaccuracy; TimetoLive = stats.TimetoLive; //txtrect = new Rectangle(0,0); base.LoadContent(); if (krypton.Lights.Count < 20) { light2 = CreateLights(_textures.PointLightTexture32); } /* _bullet = BodyFactory.CreateCircle(_world, ConvertUnits.ToSimUnits(_stats.Radius),30f); _bullet.Position = ConvertUnits.ToSimUnits(_position.X, _position.Y ); _bullet.BodyType = BodyType.Kinematic; //_bullet.IsSensor = true; _bullet.LinearVelocity = (ConvertUnits.ToSimUnits(_impulse*200)); */ _position -= new Vector2(spriteBounds.Width / 2, spriteBounds.Height / 2); Wake(); }
/// <summary> /// 构造函数 /// </summary> public Thorn() { PhysicsBody.Active = false; AnimTexture.TexturePath = "Items/Thorn"; Size = new Vector2(11, 34); // Origin = new Vector2(12.5f, 12.5f); AnimTexture.SourceRect = new Rectangle(0, 0, 11, 34); AnimTexture.AnimSeqList.Add(new AnimSequence("Hot", 0, 1, 1, true, true)); AnimTexture.AnimSeqList.Add(new AnimSequence("Normal", 1, 1, 1, true, true)); AnimTexture.AnimSeqList.Add(new AnimSequence("Ice", 2, 1, 1, true, true)); AnimTexture.PlaySeq("Hot"); RelaRateModifier mod = new RelaRateModifier("Shoot", "Position"); mod.OnStart += Shoot_OnStart; StartTween("Shoot"); // 计算速度 vSpeed = SceneManager.Instance.GetItemByName("Role").Position - SceneManager.Instance.GetItemByName("Askia").Position; vSpeed.Normalize(); vSpeed *= speed; // 设置角度 if (vSpeed.X == 0) vSpeed.X += 0.00001f; Rotation = -(float)Math.Acos(vSpeed.Y / vSpeed.X); }
public Goalie(World world, GoalieData spawn) { var body = new Body(world); var angle = spawn.End - spawn.Begin; body.Rotation = FMath.Atan2(angle.Y, angle.X); segment.A = spawn.Begin; segment.B = spawn.End; var normal = new Vector2(-angle.Y, angle.X); normal.Normalize(); delta = normal * .5f; segmentOut.A = spawn.Begin + delta; segmentOut.B = spawn.End + delta; segmentIn.A = spawn.Begin - delta; segmentIn.B = spawn.End - delta; body.Position = spawn.Begin; var verts = new Vertices(); verts.Add(new Vector2(left, bottom)); verts.Add(new Vector2(right, bottom)); verts.Add(new Vector2(right, top)); verts.Add(new Vector2(left, top)); var shape = new PolygonShape(verts, 1f); body.FixedRotation = true; body.BodyType = BodyType.Dynamic; Fixture = body.CreateFixture(shape); }
public void activate(Vector2 startPos, Vector2 direction) { m_Position = startPos; direction.Normalize(); m_Direction = direction; m_IsActive = true; }
public Line(Vector2 p1, Vector2 p2) { direction = p2 - p1; worldPosition = p1; normal = new Vector2(-direction.Y,direction.X); normal.Normalize(); }
public Line(int x1, int y1, int x2, int y2) { direction = new Vector2(x2-x1,y2-y1); worldPosition = new Vector2(x1,y1); normal = new Vector2(-direction.Y,direction.X); normal.Normalize(); }
private WheelJointTest() { Body ground = BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); { PolygonShape shape = new PolygonShape(1); shape.Vertices = PolygonTools.CreateRectangle(0.5f, 2.0f); Body body = new Body(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 7.0f); body.CreateFixture(shape); Vector2 axis = new Vector2(-1000.0f, -2.0f); axis.Normalize(); WheelJoint jd = new WheelJoint(ground, body, new Vector2(0, 8.5f), axis); jd.MotorSpeed = 1.0f; jd.MaxMotorTorque = 1000.0f; jd.MotorEnabled = true; jd.SpringFrequencyHz = 1.0f; jd.SpringDampingRatio = 0.2f; World.AddJoint(jd); PolygonShape shape2 = new PolygonShape(1); shape2.Vertices = PolygonTools.CreateRectangle(0.5f, 2.0f); Body body2 = BodyFactory.CreatePolygon(World, shape2.Vertices, 0.5f); body2.BodyType = BodyType.Dynamic; body2.Position = new Vector2(10.0f, 7.0f); } }
public void Draw(SpriteBatch spriteBatch, TrackPoint tp) { if (tp == this) { if (!drawn) { drawn = true; } else { drawn = false; return; } } next.Draw(spriteBatch, tp); foreach (Particle p in occupying) { Vector2 shift = new Vector2(0, 1); shift.Normalize(); shift *= -10 * occupying.IndexOf(p); Color colour = maxThroughput < 3 ? Color.Red : Color.IndianRed; p.Draw(spriteBatch, rotate(loc + shift, 1), colour); } }
public static Vector2 GetInletDirection(Vector2 Position) { Vector2 inletDirection = new Vector2(); inletDirection = inletPosition - Position; inletDirection.Normalize(); return inletDirection; }
public override void Update(GameTime gameTime) { Vector2 direction = new Vector2(0, 0); mFixture.Body.ResetDynamics(); mFixture.Body.LinearVelocity = new Vector2(0, 0); mFixture.Body.Rotation = 0; UpdateInput(); //base.Update(gameTime); if (mControllable) { direction = DetermineDesiredDirection(); if (direction.Length() > .065f) { mFixture.Body.LinearVelocity = (direction * mMaxSpeed); } if (direction.Length() > 0) { direction.Normalize(); mDirection = direction; } } mPosition = new Vector2(mFixture.Body.Position.X, mFixture.Body.Position.Y); // converts Body.Position (meters) into pixels UpdateAnimation(gameTime, direction); }
/// <summary> /// A streak of lightning is drawn simply by piecing together a long string of small line segments, /// each of which is slightly rotated. /// </summary> private void DrawLightningStreak(LineBatch lineBatch, Color color, Vector2 start, Vector2 end, int numSegments, float deviation) { Random randomGen = new Random(); Vector2 disp = end - start; Vector2 unitDisp = Vector2.Normalize(disp); Vector2 stepVector = unitDisp * disp.Length() / (float)numSegments; Vector2 perpVec = new Vector2(-stepVector.Y, stepVector.X); perpVec.Normalize(); Vector2 startPoint = start; Vector2 endPoint = new Vector2(); for (int i = 0; i < numSegments; ++i) { endPoint = start + stepVector * (i + 1); // If this is an intermediate segment, apply an offset perpendicular to the line connecting start to end. if (i < numSegments - 1) { float random = (float)randomGen.NextDouble(); float offset = (random >= 0.5f ? 1 : -1) * random * deviation; endPoint += perpVec * offset; } lineBatch.DrawLine(startPoint, endPoint, color); startPoint = endPoint; } }
public Vector2 GetNormalizedVector() { Vector2 v = new Vector2(this.V2.X - this.V1.X, this.V2.Y - this.V2.Y); v.Normalize(); return v; }
public void DrawLaser(SpriteBatch batch, Vector2 from, Vector2 to, float amount, float width) { amount = MathHelper.Clamp(amount, 0, 1); //if (amount == 0) { //DrawLine(batch, brush, 1, color, from, to); } //else { float number = (int)((from - to).Length() / width); float modWidth = width * amount / 2; //Vector2 fromTop = from Vector2 center = from - (from - to) * (1 / number);; Vector2 nextCenter = from; Vector2 perp = to - from; perp = new Vector2(-perp.Y, perp.X); perp.Normalize(); DrawLine(batch, brush, 1, color, from, center + perp * modWidth); DrawLine(batch, brush, 1, color, from, center - perp * modWidth); DrawLine(batch, brush, 1, color, center + perp * modWidth, to + perp * modWidth); DrawLine(batch, brush, 1, color, center - perp * modWidth, to - perp * modWidth); for (float i = 0f; i < number + 2; ++i) { perp *= -1; center = nextCenter; nextCenter = from - (from - to) * (i / number); DrawLine(batch, brush, 1, color, center - perp * modWidth, nextCenter + perp * modWidth); DrawLine(batch, brush, 1, color, center + perp * modWidth, nextCenter - perp * modWidth); } } }
public void update(GameTime gametime, Casper casper) { if (aggro.Intersects(casper.Hitbox)) { if (!casperIsIntersecting) { SoundManager.alert.Play(); casperIsIntersecting = true; } if (!hitbox.Intersects(casper.Hitbox)) { Direction = new Vector2(casper.Position.X - hitbox.X, casper.Position.Y - hitbox.Y); Direction.Normalize(); Position.X = hitbox.X; Position.Y = hitbox.Y; Position += Direction * Velocity; hitbox.X = (int)Position.X; hitbox.Y = (int)Position.Y; } } aggro.X = hitbox.X - Res.gI().ScaleX(400); aggro.Y = hitbox.Y - Res.gI().ScaleY(400); if (!aggro.Intersects(casper.Hitbox)) { casperIsIntersecting = false; } }
public static Vector2 RandomUnitVector() { Vector2 vect = new Vector2(); int rand = NexVal(0, 7); switch (rand) { case 0: vect = new Vector2(0, 1); break; case 1: vect = new Vector2(1, 1); break; case 2: vect = new Vector2(1, 0); break; case 3: vect = new Vector2(1, -1); break; case 4: vect = new Vector2(0, -1); break; case 5: vect = new Vector2(-1, -1); break; case 6: vect = new Vector2(-1, 0); break; case 7: vect = new Vector2(-1, 1); break; } vect.Normalize(); return vect; }
public override void Update(State s, Room room) { base.Update(s, room); Vector2 direction = new Vector2((float)((TargetX - x) * (DiverGame.Random.NextDouble() * 0.2f + 0.8f)), (float)((TargetY - y) * (DiverGame.Random.NextDouble() * 0.2f + 0.8f))); if(direction.LengthSquared() > 0) direction.Normalize(); speedX += direction.X * 0.007f; speedY += direction.Y * 0.007f; speedX *= 0.999f; speedY *= 0.999f; float speed = (float)Math.Sqrt(speedX * speedX + speedY * speedY); animationGridFrame += speed * 0.25f + 0.03f; x += speedX; y += speedY; X = (int)x; Y = (int)y; float desiredRot = (float)Math.Atan2(speedX, -speedY) - (float)Math.PI / 2f; float rotDiff = desiredRot - rotation; while (rotDiff > MathHelper.Pi) rotDiff -= MathHelper.TwoPi; while (rotDiff < -MathHelper.Pi) rotDiff += MathHelper.TwoPi; rotation += rotDiff * 0.1f; }
public static float AngleBetween(Vector2 v1, Vector2 v2) { if(v1!=Vector2.Zero) v1.Normalize(); if(v2!=Vector2.Zero) v2.Normalize(); float Angle = (float)Math.Acos(Vector2.Dot(v1, v2)); return Angle; }
public override void Calculate() { if (EnemyList.Count > 0) { Target = FindClosestEnemy();// EnemyList[0]; //TODO beregn den fysisk nermeste fiendem Vector2 t = new Vector2(Target.X + Target.Width/2, Target.Y + Target.Height/2); dir = t - Position; dir.Normalize(); Angle = (float)Math.Atan2( (double)dir.Y, (double)dir.X); Position += dir * Speed; X = (int)Position.X; Y = (int)Position.Y; } else { X = (int)Position.X; Y = (int)Position.Y; Position += dir * Speed; } }
public void MoveRight(float delta) { dir = new Vector2(1, 0); pos = pos + dir * speed * delta; dir.Normalize(); }
public override void Act(GameTime gameTime) { base.Act(gameTime); hitTime -= 1.0f; if (hitTime <= 0) { Vector2 temp = new Vector2(Owner.FaceVector.X, Owner.FaceVector.Y); temp.Normalize(); ArrayList targetList = new ArrayList(); Owner.EntitiesInRadius(50, Owner.CenterPosition + Owner.Bounds, targetList); for (int i = 0; i < targetList.Count; i++) { if (targetList[i] is Player) { if (Owner is BigZombie) { ((Being)targetList[i]).Health -= 8; } ((Being)targetList[i]).Health -= 2; } } hitTime = 5; //Zombie.CurrentState = new ZombieWalkState(); } }
override public void update(float dt) { ownerTilePos = new Point(owner.hitBox.Center.X / world.tileSize, owner.hitBox.Center.Y / world.tileSize); pathTilePos = new Point(nextPathPoint.X / world.tileSize, nextPathPoint.Y / world.tileSize); if (ownerTilePos.X == pathTilePos.X && ownerTilePos.Y == pathTilePos.Y) { if (path.Count > 0) { nextPathPoint = path.ElementAt(0); path.RemoveAt(0); } else { onEnd(); } } else { Vector2 movement = new Vector2(nextPathPoint.X - owner.hitBox.Center.X, nextPathPoint.Y - owner.hitBox.Center.Y); movement.Normalize(); movement *= 2; owner.velocity.X = movement.X; owner.velocity.Y = movement.Y; owner.isWalking = true; owner.setGaze(nextPathPoint); } }
public void MoveDown(float delta) { dir = new Vector2(0,1); pos = pos + dir * speed * delta; dir.Normalize(); }
public Player() { position = new Vector2(500/2, 500/2); velocity = new Vector2(0, 0); hitbox = new Rectangle(); speed = 5; health = 100; //manaPoints = 0; #region Vapen-relaterat gunCooldown = 750; machinegunCooldown = 125; shotgunCooldown = 250; gunAmmo = 6; shotgunAmmo = 2; machinegunAmmo = 40; gunAmmoMax = 6; shotgunAmmoMax = 2; machinegunAmmoMax = 40; rsGun = 500; rsShotgun = 2000; rsMachinegun = 1000; gunDamage = 100; shotgunDamage = 100; machinegunDamage = 35; #endregion experiencePoints = 0; level = 1; velocity.Normalize(); velocity *= speed; // profession = "Blank"; sourceRectangle = new Rectangle(0, 0, 48, 60); }
public override Vector2[] FindTargets(ZazumoActor zazumoActor, IEnumerable<EnemyActor> enemies) { var moveDiretion = new Vector2(zazumoActor.Location.X, zazumoActor.Location.Y) - _lastPosition; moveDiretion.Normalize(); _lastPosition = new Vector2(zazumoActor.Location.X, zazumoActor.Location.Y); var requestedTarget = new Vector2(moveDiretion.X * -10f, moveDiretion.Y * -6f); if (Single.IsNaN(requestedTarget.X) || Single.IsNaN(requestedTarget.Y)) return new Vector2[]{}; if (_lastTarget == Vector2.Zero) { _lastTarget = requestedTarget; return new Vector2[] { requestedTarget }; } else { _lastTarget.Normalize(); var lastAngle = (Single)(Math.Acos(_lastTarget.X) * (_lastTarget.Y > 0 ? -1.0 : 1.0)); requestedTarget.Normalize(); var requestedAngle = (Single)(Math.Acos(requestedTarget.X) * (requestedTarget.Y > 0 ? -1.0 : 1.0)); Single angle = (0.7f * lastAngle) + (0.3f * requestedAngle); var direction = new Vector2((Single)Math.Cos(angle), (Single)Math.Sin(angle) * -1.0f); direction.Normalize(); var newtarget = new Vector2(direction.X * 10f, direction.Y * 6f); _lastTarget = newtarget; return new Vector2[] { newtarget }; } }
override public void update(float dt) { //Console.WriteLine(path.Count); double distance = Math.Sqrt(Math.Pow(target.hitBox.Center.X - owner.hitBox.Center.X, 2) + Math.Pow(target.hitBox.Center.Y - owner.hitBox.Center.Y, 2)); if (distance > 50) { Vector2 attackVector = new Vector2(owner.hitBox.Center.X - target.hitBox.Center.X, owner.hitBox.Center.Y - target.hitBox.Center.Y); attackVector.Normalize(); attackVector *= 50; attackVector.X += target.hitBox.Center.X; attackVector.Y += target.hitBox.Center.Y; Point attackPos = new Point((int)attackVector.X, (int)attackVector.Y); Vector2 movement = new Vector2(attackPos.X - owner.hitBox.Center.X, attackPos.Y - owner.hitBox.Center.Y); movement.Normalize(); movement *= 5; //owner.movementIntent /= 3f; owner.velocity.X = movement.X; owner.velocity.Y = movement.Y; owner.isWalking = true; owner.setGaze(attackPos); } else { owner.setGaze(target.hitBox.Center); Punch punch = new Punch(owner.animationList, owner); if (!owner.animationList.has(punch)) { owner.animationList.pushFront(punch); } } }
void IUpdatable.update() { if( Math.Abs( _shakeIntensity ) > 0f ) { _shakeOffset = _shakeDirection; if( _shakeOffset.X != 0f || _shakeOffset.Y != 0f ) { _shakeOffset.Normalize(); } else { _shakeOffset.X = _shakeOffset.X + Random.nextFloat() - 0.5f; _shakeOffset.Y = _shakeOffset.Y + Random.nextFloat() - 0.5f; } // TODO: this needs to be multiplied by camera zoom so that less shake gets applied when zoomed in _shakeOffset *= _shakeIntensity; _shakeIntensity *= -_shakeDegredation; if( Math.Abs( _shakeIntensity ) <= 0.01f ) { _shakeIntensity = 0f; enabled = false; } } entity.scene.camera.position += _shakeOffset; }
/// <summary> /// Initialize the link with a beginning and ending /// point. /// </summary> /// <param name="first">First point in the link.</param> /// <param name="last">Last point in the link.</param> public Link(Vector2 first, Vector2 last) { this.first = first; this.last = last; normalized = last - first; normalized.Normalize(); }
public static Vector2 DirectionFrom(Vector2 point1, Vector2 point2, out float angle) { angle = AngleFrom(point1, point2); Vector2 direction = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)); direction.Normalize(); return direction; }
public override void NPCLoot() //this makes stuff happen at the death { for (int i = 0; i < Main.rand.Next(3, 8); i++) //this makes three to eight slimes spawn, I think { Microsoft.Xna.Framework.Vector2 pos = new Microsoft.Xna.Framework.Vector2(npc.Center.X + Main.rand.NextFloat(-5, 5), npc.Center.Y + Main.rand.NextFloat(5)); Microsoft.Xna.Framework.Vector2 direc = pos; direc.Normalize(); //normalizing a vector will result in the vector becoming a unit direction. multiply this by a speed Dust.NewDust(pos, npc.width, npc.height, 0, direc.X, direc.Y, 0, new Color(18, 204, 52)); } }
public Coin(Vector2 pos, int type) { var rand = new Random(); Velocity.X = rand.NextFloat(-5, 5) / 4; Velocity.Y = rand.NextFloat(1, 4) / -8; var len = Velocity.Length(); Velocity.Normalize(); Velocity *= len; Dimension.LoadGameObject(this); position = pos; coinType = type; }
/// <summary> /// Determines whether the closest point on the segment lies on one of the endpoints. /// </summary> /// <param name="point">The point to test to.</param> /// <returns>Whether the closest point on this segment to the argument point lies on the endpoints.</returns> #endregion public bool IsClosestPointOnEndpoint(ref Point point) { sUnitSegmentForIsClosestPointOnEndpoint.X = (float)(this.Point2.X - this.Point1.X); sUnitSegmentForIsClosestPointOnEndpoint.Y = (float)(Point2.Y - Point1.Y); sUnitSegmentForIsClosestPointOnEndpoint.Normalize(); sPointVectorForIsClosestPointOnEndpoint.X = (float)(point.X - this.Point1.X); sPointVectorForIsClosestPointOnEndpoint.Y = (float)(point.Y - Point1.Y); #if FRB_MDX float l = Vector2.Dot(sPointVectorForIsClosestPointOnEndpoint, sUnitSegmentForIsClosestPointOnEndpoint); #else float l; Vector2.Dot(ref sPointVectorForIsClosestPointOnEndpoint, ref sUnitSegmentForIsClosestPointOnEndpoint, out l); #endif return(l < 0 || l * l > this.GetLengthSquared()); }
private void DrawVector(SpriteBatch spriteBatch, IGameObject sprite, int borderSize, Color borderColor) { // Calculate line size and angle var sr = sprite.GetComponent <SpriteRenderer>(); var rb = sprite.GetComponent <Rigidbody>(); Vector2 vv = new Vector2(rb.velocity.X, rb.velocity.Y); Vector2 begin = new Vector2(sprite.Transform.Position.X, sprite.Transform.Position.Y); Vector2 end = begin + vv; Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length() + borderSize, borderSize); Vector2 v = Vector2.Normalize(begin - end); float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX)); if (begin.Y > end.Y) { angle = MathHelper.TwoPi - angle; } // Calculate text and background sizes Vector2 XtextSize = _spriteFont.MeasureString("X:" + (int)vv.X); Vector2 YtextSize = _spriteFont.MeasureString("Y:" + (int)vv.Y); int maxWidth = (int)XtextSize.X; if (YtextSize.X > maxWidth) { maxWidth = (int)YtextSize.X; } Rectangle textSize = new Rectangle((int)end.X - maxWidth / 2, (int)end.Y - (int)XtextSize.Y + 4, maxWidth + 4, (int)XtextSize.Y + 9); // Draw vector line and component values spriteBatch.Draw(_pixel, r, null, borderColor, angle, Vector2.Zero, SpriteEffects.None, 0.0002f); if (ShowVectorPositionLabels) { spriteBatch.DrawString(_spriteFont, "X:" + (int)vv.X, new Vector2(textSize.X + 2, end.Y - 12), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0); spriteBatch.DrawString(_spriteFont, "Y:" + (int)vv.Y, new Vector2(textSize.X + 2, end.Y - 2), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0); // Draw text background spriteBatch.Draw(_consoleWindow, textSize, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.0001f); } }