Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
private Vector2 CheckCollision(int Index) { Rectangle rectangleA, rectangleB; rectangleA = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y); rectangleB = new Rectangle((int)Level.CollisionMasks[Index].Position.X, (int)Level.CollisionMasks[Index].Position.Y, Level.CollisionMasks[Index].Texture.Width, Level.CollisionMasks[Index].Texture.Height); Color[] dataA, dataB; dataA = colorData; dataB = Level.CollisionMasks[Index].colorInfo; // Find the bounds of the rectangle intersection int top = Math.Max(rectangleA.Top, rectangleB.Top); int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom); int left = Math.Max(rectangleA.Left, rectangleB.Left); int right = Math.Min(rectangleA.Right, rectangleB.Right); // Check every point within the intersection bounds for (int y = top; y < bottom; y++) { for (int x = left; x < right; x++) { // Get the color of both pixels at this point Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width]; Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width]; // If both pixels are not completely transparent, if (colorA.A != 0 && colorB.A != 0) { // then an intersection has been found return new Vector2(x, y); } } } // No intersection found return new Vector2(1.337f, 1.337f); } |
C#-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
override public void OnUpdate(GameTime gameTime) { Vector2 previousPosition = Position; state = Player.State.Standing; if (Keyboard.GetState().IsKeyDown(Keys.A)) { Velocity = new Vector2(-20, Velocity.Y); direction = Player.Direction.Left; state = Player.State.Walking; } else if (Keyboard.GetState().IsKeyDown(Keys.D)) { Velocity = new Vector2(20, Velocity.Y); direction = Player.Direction.Right; state = Player.State.Walking; } else Velocity = Vector2.Zero; if (Keyboard.GetState().IsKeyDown(Keys.Space)) { state = Player.State.Jumping; } Velocity += Acceleration * (float)gameTime.ElapsedGameTime.TotalSeconds; Position += Velocity; //check coli Vector2 PoC = CheckCollision(0); if (PoC != new Vector2(1.337f, 1.337f)) { Position = previousPosition; MoveToContact(); } switch (state) { case State.Walking: SetAnimation("Walk"); break; case State.Jumping: SetAnimation("Jump"); break; case State.Standing: default:SetAnimation("Stand"); break; } base.OnUpdate(gameTime); } private void MoveToContact() { Vector2 beforeCollision = Position; while (CheckCollision(0) == new Vector2(1.337f, 1.337f)) { beforeCollision = Position; Position += new Vector2(0, 1); } Position = beforeCollision; } |
Werbeanzeige