public void Update(GameTime gameTime) { ComponentTransform temp = _entityManager.GetEntity("testEntity").GetComponent <ComponentTransform>(); ComponentPhysics tempPhys = _entityManager.GetEntity("testEntity").GetComponent <ComponentPhysics>(); //Vector2 tempPos = ((MouseInput.CurrentPos - temp.GetPos()).Normalized().MultiplyLength(100.0).MultiplyLength(gameTime.ElapsedGameTime.TotalSeconds)); if (MouseInput.CheckLeftPressed()) { temp.Pos = MouseInput.CurrentPos; } if (KeyboardInput.CheckIsKeyDown(Keys.A)) { tempPhys.AddVelocity(new Vector2(-20.0f, 0.0f)); } if (KeyboardInput.CheckIsKeyDown(Keys.D)) { tempPhys.AddVelocity(new Vector2(20.0f, 0.0f)); } if (KeyboardInput.CheckIsKeyDown(Keys.Space)) { tempPhys.AddVelocity(new Vector2(0.0f, -30.0f)); } _entityManager.Update(gameTime); if (KeyboardInput.CheckIsPressed(Keys.Escape)) { Game1.State = State.MAIN_MENU; } }
private void UpdateCylinder(ComponentCollsion collsionComp, Matrix4 cylinderMatrix) { for (int i = 0; i < m_EntityList.Count; i++) { List <IComponent> components = m_EntityList[i].Components; IComponent CylinderCollision = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_CYLINDERCOLLSION); }); if (CylinderCollision != null) { IComponent SphereTransfrom = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_TRANSFORM); }); ComponentTransform sphere = (ComponentTransform)SphereTransfrom; Vector3 spherePosition = sphere.Position; IComponent physicsComp = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_PHYSICS); }); ComponentPhysics spherePhys = (ComponentPhysics)physicsComp; Vector3 circleInCollsionSpace = Vector3.TransformPosition(spherePosition, cylinderMatrix.Inverted()); Vector3 LNormal = (collsionComp.LinePointOne - collsionComp.LinePointTwo).Normalized(); Vector3 A = Vector3.Dot(circleInCollsionSpace - collsionComp.LinePointTwo, LNormal) * LNormal; float LowerBoundsA = Vector3.Dot(circleInCollsionSpace - collsionComp.LinePointTwo, LNormal); //red line = ltwo + A - pos Vector3 redLine = (collsionComp.LinePointTwo + A) - circleInCollsionSpace; //if redline < radius of circle if (redLine.Length < (spherePhys.Radius)) { if (A.Length > collsionComp.LineDistance.Length || LowerBoundsA < 0) { //do nothing } else { spherePhys.Velocity = Vector3.Transform(spherePhys.Velocity, cylinderMatrix.ExtractRotation().Inverted()); Vector3 rad = new Vector3(spherePhys.Radius, spherePhys.Radius, spherePhys.Radius); Vector3 normal = Vector3.Transform((circleInCollsionSpace - redLine).Normalized(), cylinderMatrix.ExtractRotation().Inverted()); //sphere.Position = sphere.OldPosition + rad * normal; spherePhys.Velocity -= 2 * Vector3.Dot(normal, spherePhys.Velocity) * normal; spherePhys.Velocity = Vector3.Transform(spherePhys.Velocity, cylinderMatrix.ExtractRotation()); float distance = Math.Abs(Vector3.Dot(circleInCollsionSpace, normal)); Vector3 direction = spherePhys.Velocity.Normalized(); } } } } }
public Vector3 UpdatePhysicsPosition(ComponentTransform transform, ComponentPhysics physics, float dt) { Vector3 position = transform.Position; transform.OldPosition = position; Vector3 velocity = physics.Velocity; return(position += (velocity + m_gravity) * dt); }
public void UpdatePhysicsCollsion(Entity self, ComponentTransform selfPosition, ComponentPhysics selfPhysics) { for (int i = 0; i < m_EntityList.Count; i++) { List <IComponent> components = m_EntityList[i].Components; IComponent physCollsion = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_PHYSICSCOLLSION); }); if (physCollsion != null && m_EntityList[i] != self) { IComponent sphereTransfrom = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_TRANSFORM); }); ComponentTransform collidedWithPosition = (ComponentTransform)sphereTransfrom; IComponent physicsComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_PHYSICS); }); ComponentPhysics collidedwithPhysics = (ComponentPhysics)physicsComponent; if ((selfPosition.Position - collidedWithPosition.Position).Length < (selfPhysics.Radius + collidedwithPhysics.Radius)) { Vector3 normal = (collidedWithPosition.Position - selfPosition.Position).Normalized(); Vector3 rad = new Vector3(selfPhysics.Radius, selfPhysics.Radius, selfPhysics.Radius); //selfPosition.Position = selfPosition.OldPosition + rad * normal; Vector3 selfVelocityCopy = selfPhysics.Velocity; Vector3 collidedwithVelocityCopy = collidedwithPhysics.Velocity; selfPhysics.Velocity -= Vector3.Dot(selfPhysics.Velocity, normal) * normal; //velocity two collidedwithPhysics.Velocity -= Vector3.Dot(collidedwithPhysics.Velocity, -normal) * -normal; //velocity of this instance of the sphere //𝑣1 = 𝑚1 * 𝑢1 + 𝑚2 * 𝑢2 + 𝑒 * 𝑚2(𝑢2 − 𝑢1) / 𝑚1 + 𝑚2 Vector3 first = selfPhysics.Mass * selfVelocityCopy; Vector3 second = collidedwithPhysics.Mass * collidedwithVelocityCopy; Vector3 co = collidedwithPhysics.CoEfficent * collidedwithPhysics.Mass * (collidedwithVelocityCopy - selfVelocityCopy); Vector3 final = (first + second + co) / (selfPhysics.Mass + collidedwithPhysics.Mass); selfPhysics.Velocity = final; //velocity of the second instance of the sphere //𝑣2 = (𝑚2 − 𝑚1 / 𝑚2 + 𝑚1) * 𝑢2 + (2𝑚1 𝑚2 + 𝑚1) * 𝑢1 float firstPart = (collidedwithPhysics.Mass - selfPhysics.Mass) / (collidedwithPhysics.Mass + selfPhysics.Mass); float secondPart = (selfPhysics.Mass * 2) / (collidedwithPhysics.Mass + selfPhysics.Mass); collidedwithPhysics.Velocity = (firstPart * collidedwithVelocityCopy) + (secondPart * selfVelocityCopy); } } } }
/// <summary> /// this method checks everything that can collide with a doom sphere and checks to see if it has /// if the radius happens to be zero, it remove the ball from the list /// </summary> /// <param name="doomPosition"></param> /// <param name="doomSphereRadius"></param> public void UpdateDoomSphere(Vector3 doomPosition, float doomSphereRadius) { for (int i = 0; i < m_EntityList.Count; i++) { List <IComponent> components = m_EntityList[i].Components; IComponent doomCollision = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_DOOMCOLLSION); }); if (doomCollision != null) { IComponent sphereTransfrom = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_TRANSFORM); }); ComponentTransform collidedWithPosition = (ComponentTransform)sphereTransfrom; IComponent physicsTransform = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_PHYSICS); }); ComponentPhysics collidedWithPhysics = (ComponentPhysics)physicsTransform; float radius = collidedWithPhysics.Radius; if (radius <= 0) { m_EntityList.RemoveAt(i); } else if ((doomPosition - collidedWithPosition.Position).Length < (doomSphereRadius + radius)) { Vector3 ballScale = collidedWithPosition.Scale; Vector3 scale = new Vector3(ballScale.X - .1f, ballScale.Y - .1f, ballScale.Y - .1f); collidedWithPhysics.ResizeBall(); collidedWithPosition.Scale = scale; } } } }
public void OnAction(Entity entity, float dt) { if ((entity.Mask & MASK) == MASK) { List <IComponent> components = entity.Components; IComponent transfromComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_TRANSFORM); }); ComponentTransform transform = (ComponentTransform)transfromComponent; IComponent physicsComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_PHYSICS); }); ComponentPhysics physics = (ComponentPhysics)physicsComponent; transform.Position = UpdatePhysicsPosition(transform, physics, dt); UpdatePhysicsCollsion(entity, transform, physics); } }
public void UpdateBoxCollsion(Matrix4 modelMatrix) { //loops through each entity again, looking for a certain component to make sure calculations do not happen //on game objects that do not need it foreach (var item in m_EntityList) { List <IComponent> components = item.Components; IComponent worldCollsion = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_WORLDCOLLSION); }); if (worldCollsion != null) { //this is the spheres transform IComponent transfromComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_TRANSFORM); }); ComponentTransform transform = (ComponentTransform)transfromComponent; //this is the spheres transform IComponent physicsComponent = components.Find(delegate(IComponent component) { return(component.ComponentType == ComponentTypes.COMPONENT_PHYSICS); }); ComponentPhysics physics = (ComponentPhysics)physicsComponent; Vector3 sphereinTowerSpace = Vector3.TransformPosition(transform.Position, modelMatrix.Inverted()); float mCircleRadius = transform.Scale.X; if (transform.Position.Y < -3.0) { //change this to teleport transform.Position = new Vector3(-1, 4.2f, 0); physics.Velocity = new Vector3(2, -1, 0); } else if (transform.Position.Y > 4.5) { Vector3 normal = Vector3.Transform(new Vector3(0, 1, 0), modelMatrix.ExtractRotation()); ChangeDirection(physics, transform, normal); } else if (transform.Position.Z < -1.2) { Vector3 normal = Vector3.Transform(new Vector3(0, 0, -1), modelMatrix.ExtractRotation()); ChangeDirection(physics, transform, normal); } else if (transform.Position.Z > 1.2) { Vector3 normal = Vector3.Transform(new Vector3(0, 0, 1), modelMatrix.ExtractRotation()); ChangeDirection(physics, transform, normal); } else if (transform.Position.X < -1.2) { Vector3 normal = Vector3.Transform(new Vector3(1, 0, 0), modelMatrix.ExtractRotation()); ChangeDirection(physics, transform, normal); } else if (transform.Position.X > 1.2) { Vector3 normal = Vector3.Transform(new Vector3(-1, 0, 0), modelMatrix.ExtractRotation()); ChangeDirection(physics, transform, normal); } } } }
public void ChangeDirection(ComponentPhysics physics, ComponentTransform transform, Vector3 normal) { transform.Position = transform.OldPosition; physics.Velocity -= 2 * Vector3.Dot(normal, physics.Velocity) * normal; }
public void Update(GameTime gameTime) { //float speed = 50.0f; float parallax = 0.075f; float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; ComponentTransform playerTransform = _entityManager.GetEntity("player").GetComponent <ComponentTransform>(); ComponentPhysics playerPhysics = _entityManager.GetEntity("player").GetComponent <ComponentPhysics>(); // _backgroundPosition.X -= dt * speed * parallax; // _entityManager.MoveAllBy(new Vector2(-dt * speed, 0.0f)); // _position.X += dt * speed; if (KeyboardInput.CheckIsKeyDown(Microsoft.Xna.Framework.Input.Keys.A)) { // _backgroundPosition.X += dt * speed * parallax; playerPhysics.Velocity = new Vector2(-300.0f, playerPhysics.Velocity.Y); // _entityManager.MoveAllBy(new Vector2(dt * speed,0.0f)); // _position.X -= dt * speed; //playerTransform.Pos += new Vector2(dt * speed, 0.0f); } else if (KeyboardInput.CheckIsKeyDown(Microsoft.Xna.Framework.Input.Keys.D)) { // _backgroundPosition.X -= dt * speed * parallax; //playerPhysics.AddVelocity(new Vector2(speed, 0.0f)); playerPhysics.Velocity = new Vector2(300.0f, playerPhysics.Velocity.Y); // _entityManager.MoveAllBy(new Vector2(-dt * speed, 0.0f)); //playerTransform.Pos += new Vector2(dt * speed, 0.0f); // _position.X += dt * speed; } if (KeyboardInput.CheckIsPressed(Microsoft.Xna.Framework.Input.Keys.Space)) { if (!playerPhysics.InAir) { playerPhysics.Velocity = new Vector2(playerPhysics.Velocity.X, -450.0f); } } if (KeyboardInput.CheckIsKeyDown(Microsoft.Xna.Framework.Input.Keys.W)) { //_backgroundPosition.Y += dt * speed * parallax; // _entityManager.MoveAllBy(new Vector2(0.0f, dt * speed)); // playerPhysics.AddVelocity(new Vector2(0.0f, -speed)); // _position.Y -= dt * speed; } else if (KeyboardInput.CheckIsKeyDown(Microsoft.Xna.Framework.Input.Keys.S)) { //_backgroundPosition.Y -= dt * speed * parallax; // _entityManager.MoveAllBy(new Vector2(0.0f, -dt * speed)); // playerPhysics.AddVelocity(new Vector2(0.0f, +dt * speed)); // _position.Y += dt * speed; } Rectangle backgroundBounds = _textureBackground.Bounds; if (_backgroundPosition.X > 0) { _backgroundPosition.X -= backgroundBounds.Width; } else if (_backgroundPosition.X < -backgroundBounds.Width) { _backgroundPosition.X += backgroundBounds.Width; } if (_backgroundPosition.Y > 0) { _backgroundPosition.Y -= backgroundBounds.Height; } else if (_backgroundPosition.Y < -backgroundBounds.Height) { _backgroundPosition.Y += backgroundBounds.Height; } if (MouseInput.CheckLeftPressed()) { _entityManager.GetEntity("warpBall").GetComponent <ComponentTransform>().Pos = playerTransform.Pos + new Vector2(0, -36.0f); ComponentPhysics ballPhys = _entityManager.GetEntity("warpBall").GetComponent <ComponentPhysics>(); ballPhys.Velocity = new Vector2(MouseInput.CurrentPos.X - playerTransform.Pos.X, MouseInput.CurrentPos.Y - playerTransform.Pos.Y) .Normalized().MultiplyLength(500.0f); } if (KeyboardInput.CheckIsPressed(Microsoft.Xna.Framework.Input.Keys.E)) { Entity warpBall = _entityManager.GetEntity("warpBall"); if (!warpBall.GetComponent <ComponentPhysics>().InAir) { Entity player = _entityManager.GetEntity("player"); Vector2 playerPos = player.GetComponent <ComponentTransform>().Pos; playerPos = warpBall.GetComponent <ComponentTransform>().Pos; playerPos.Y -= 32; playerPos.X += 16; Vector2 vel = player.GetComponent <ComponentPhysics>().Velocity; vel.Y = 0; player.GetComponent <ComponentPhysics>().Velocity = vel; player.GetComponent <ComponentTransform>().Pos = playerPos; } } _entityManager.Update(gameTime); Vector2 delta = playerTransform.WorldTransform.Translate - _oldPlayerPos; _backgroundPosition.X += delta.X * parallax; _backgroundPosition.Y += delta.Y * parallax; _entityManager.MoveAllBy(new Vector2(-delta.X, -delta.Y)); //playerTransform.Pos += new Vector2(delta.X, delta.Y); _position += new Vector2(delta.X, delta.Y); _oldPlayerPos = playerTransform.WorldTransform.Translate; }