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
Kód: Vybrat vše
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AdventureGame
{
public class Player : Object
{
KeyboardState key, prevKey;
float playerSpeed = 3.0f;
public Vector2 velocity;
public bool hasJumped;
public Player(Vector2 posi)
: base(posi)
{
hasJumped = true;
}
public override void Update()
{
// nastavení pohybu hráče
key = Keyboard.GetState();
if (key.IsKeyDown(Keys.A))
{
position.X -= playerSpeed;
}
else if (key.IsKeyDown(Keys.D))
{
position.X += playerSpeed;
}
// nastavení základní kolize hráče a krajů obrazovky
CollisionWithScreen();
// nastavení gravitace hráče
GravitationOfPlayer();
/*--------------------------------------------------------------*/
prevKey = key;
base.Update();
}
private void CollisionWithScreen()
{
if (position.X <= 0)
position.X = 0;
if (position.X >= 1280 - spriteTexture.Width)
position.X = 1280 - spriteTexture.Width;
if (position.Y <= 0)
position.Y = 0;
if (position.Y >= 720 - spriteTexture.Height)
{
velocity.Y = 0f;
position.Y = 720 - spriteTexture.Height;
hasJumped = false;
}
}
public void Collision(Rectangle newRectangle)
{
if(collisionRectangle.isOnTopOf(newRectangle))
{
collisionRectangle.Y = newRectangle.Y - collisionRectangle.Height;
velocity.Y = 0f;
hasJumped = false;
}
}
private void GravitationOfPlayer()
{
position += velocity;
collisionRectangle = new Rectangle((int)position.X, (int)position.Y, spriteTexture.Width, spriteTexture.Height);
if (key.IsKeyDown(Keys.Space) && hasJumped == false)
{
position.Y -= 30f;
velocity.Y = -10f;
hasJumped = true;
}
if (hasJumped == true)
{
float i = 2;
velocity.Y += 0.30f * i;
}
else if (hasJumped == false)
velocity.Y = 0f;
}
}
}Kód: Vybrat vše
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AdventureGame
{
public class Object
{
public Vector2 position;
public Texture2D spriteTexture;
public Rectangle collisionRectangle;
public string spriteName;
public float rotation = 0.0f;
public float speed;
public float scale = 2.0f;
public Object(Vector2 pos)
{
this.position = pos;
}
public Object()
{
}
public virtual void LoadContent(ContentManager content, string spriteName)
{
this.spriteName = spriteName;
spriteTexture = content.Load<Texture2D>("Sprites\\" + spriteName);
}
public virtual void Update()
{
}
public virtual void Draw(SpriteBatch spriteBatch)
{
//spriteBatch.Draw(spriteTexture, , Color.White, rotation, new Vector2(0, 0), scale, SpriteEffects.None, 0);
spriteBatch.Draw(spriteTexture, collisionRectangle, Color.White);
}
}
}Kód: Vybrat vše
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdventureGame
{
static class RectangleHelper
{
public static bool isOnTopOf(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top &&
r1.Bottom <= r2.Top + (r2.Height / 2) &&
r1.Right >= r2.Left + r2.Width / 5 &&
r1.Left <= r2.Right - r2.Width / 5);
}
}
}