XNA - Colize

Místo pro dotazy a rady ohledně programovacích jazyků (C++, C#, PHP, ASP, Javascript, VBS..) a tvorby webových stránek

Moderátor: Mods_senior

CallManyCZ
nováček
Příspěvky: 36
Registrován: červenec 14
Pohlaví: Muž
Stav:
Offline

XNA - Colize

Příspěvekod CallManyCZ » 04 lis 2015 21:29

Ahoj,
Vyvíjím hru v XNA .. Bude to něco jako terraria, vím moc mainstream, ale to teď nechci slyšet. Chtěl bych po vás radu, a to ohledně kolizí.
Kolize fungujou, ale když chci slézt z 1 blocku o block níže , tak panáček jakoby by létal ve vzduchu.
Zde máte obrázek jak to zatím vypadá : https://ctrlv.cz/dSt1

Zde jsou moje třídy:

TŘÍDA Player

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13.  
  14. namespace AdventureGame
  15. {
  16.     public class Player : Object
  17.     {
  18.         KeyboardState key, prevKey;
  19.  
  20.         float playerSpeed = 3.0f;
  21.  
  22.         public Vector2 velocity;
  23.         public bool hasJumped;
  24.  
  25.         public Player(Vector2 posi)
  26.             : base(posi)
  27.         {
  28.             hasJumped = true;
  29.         }
  30.  
  31.         public override void Update()
  32.         {
  33.             // nastavení pohybu hráče
  34.             key = Keyboard.GetState();
  35.  
  36.             if (key.IsKeyDown(Keys.A))
  37.             {
  38.                 position.X -= playerSpeed;
  39.             }
  40.             else if (key.IsKeyDown(Keys.D))
  41.             {
  42.                 position.X += playerSpeed;
  43.             }
  44.  
  45.             // nastavení základní kolize hráče a krajů obrazovky
  46.             CollisionWithScreen();
  47.  
  48.             // nastavení gravitace hráče
  49.             GravitationOfPlayer();
  50.             /*--------------------------------------------------------------*/
  51.  
  52.             prevKey = key;
  53.             base.Update();
  54.         }
  55.  
  56.         private void CollisionWithScreen()
  57.         {
  58.             if (position.X <= 0)
  59.                 position.X = 0;
  60.             if (position.X >= 1280 - spriteTexture.Width)
  61.                 position.X = 1280 - spriteTexture.Width;
  62.             if (position.Y <= 0)
  63.                 position.Y = 0;
  64.             if (position.Y >= 720 - spriteTexture.Height)
  65.             {
  66.                 velocity.Y = 0f;
  67.                 position.Y = 720 - spriteTexture.Height;
  68.                 hasJumped = false;
  69.             }
  70.                
  71.         }
  72.  
  73.         public void Collision(Rectangle newRectangle)
  74.         {
  75.             if(collisionRectangle.isOnTopOf(newRectangle))
  76.             {
  77.                 collisionRectangle.Y = newRectangle.Y - collisionRectangle.Height;
  78.                 velocity.Y = 0f;
  79.                 hasJumped = false;
  80.             }
  81.         }
  82.  
  83.         private void GravitationOfPlayer()
  84.         {
  85.             position += velocity;
  86.             collisionRectangle = new Rectangle((int)position.X, (int)position.Y, spriteTexture.Width, spriteTexture.Height);
  87.  
  88.  
  89.  
  90.             if (key.IsKeyDown(Keys.Space) && hasJumped == false)
  91.             {
  92.                 position.Y -= 30f;
  93.                 velocity.Y = -10f;
  94.                 hasJumped = true;
  95.             }
  96.  
  97.             if (hasJumped == true)
  98.             {
  99.                 float i = 2;
  100.                 velocity.Y += 0.30f * i;
  101.             }
  102.             else if (hasJumped == false)
  103.                 velocity.Y = 0f;
  104.  
  105.  
  106.         }
  107.  
  108.     }
  109. }


TŘÍDA Object

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace AdventureGame
  14. {
  15.     public class Object
  16.     {
  17.         public Vector2 position;
  18.         public Texture2D spriteTexture;
  19.         public Rectangle collisionRectangle;
  20.         public string spriteName;
  21.         public float rotation = 0.0f;
  22.         public float speed;
  23.         public float scale = 2.0f;
  24.  
  25.         public Object(Vector2 pos)
  26.         {
  27.             this.position = pos;
  28.         }
  29.  
  30.         public Object()
  31.         {
  32.  
  33.         }
  34.  
  35.         public virtual void LoadContent(ContentManager content, string spriteName)
  36.         {
  37.             this.spriteName = spriteName;
  38.  
  39.             spriteTexture = content.Load<Texture2D>("Sprites\\" + spriteName);
  40.            
  41.         }
  42.  
  43.         public virtual void Update()
  44.         {
  45.         }
  46.  
  47.         public virtual void Draw(SpriteBatch spriteBatch)
  48.         {
  49.             //spriteBatch.Draw(spriteTexture, , Color.White, rotation, new Vector2(0, 0), scale, SpriteEffects.None, 0);
  50.  
  51.             spriteBatch.Draw(spriteTexture, collisionRectangle, Color.White);
  52.         }
  53.  
  54.     }
  55. }


TŘÍDA RectangleHelper

  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace AdventureGame
  8. {
  9.     static class RectangleHelper
  10.     {
  11.    
  12.         public static bool isOnTopOf(this Rectangle r1, Rectangle r2)
  13.         {
  14.             return (r1.Bottom >= r2.Top &&
  15.                     r1.Bottom <= r2.Top + (r2.Height / 2) &&
  16.                     r1.Right >= r2.Left + r2.Width / 5 &&
  17.                     r1.Left <= r2.Right - r2.Width / 5);
  18.         }
  19.     }
  20. }

Reklama

Zpět na “Programování a tvorba webu”

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 5 hostů