//Select by bounding box, not by mesh bool SelectionCast(Vector3 rayOrigin, Vector3 direction, float maxDist, out PhysicsObject body) { float dist = float.MaxValue; body = null; var jitterDir = direction * maxDist; foreach (var rb in world.Physics.Objects) { if (rb.Tag == player) { continue; } if (rb.Collider is SphereCollider) { //Test spheres var sph = (SphereCollider)rb.Collider; if (SphereRayIntersect(rayOrigin, direction, maxDist, rb.Position, sph.Radius)) { var nd = VectorMath.DistanceSquared(rb.Position, camera.Position); if (nd < dist) { dist = nd; body = rb; } } } else { //var tag = rb.Tag as GameObject; if (!rb.GetBoundingBox().RayIntersect(ref rayOrigin, ref jitterDir)) { continue; } body = rb; /*if (tag == null || tag.CmpParts.Count == 0) * { * //Single part * var nd = VectorMath.DistanceSquared(rb.Position, camera.Position); * if (nd < dist) * { * dist = nd; * body = rb; * } * } * else * { * //Test by cmp parts * var sh = (CompoundSurShape)rb.Shape; * for (int i = 0; i < sh.Shapes.Length; i++) * { * sh.Shapes[i].UpdateBoundingBox(); * var bb = sh.Shapes[i].BoundingBox; * bb.Min += rb.Position; * bb.Max += rb.Position; * if (bb.RayIntersect(ref rayOrigin, ref jitterDir)) * { * * var nd = VectorMath.DistanceSquared(rb.Position, camera.Position); * if (nd < dist) * { * dist = nd; * body = rb; * } * break; * } * } * }*/ } } return(body != null); }
public override void FixedUpdate(TimeSpan time) { var world = Parent.GetWorld(); var player = world.GetObject("player"); if (player == null) { return; } if (VectorMath.DistanceSquared(player.PhysicsComponent.Position, Field.Zone.Position) > activateDist) { return; } var cds = (Field.CubeSize + COLLIDE_DISTANCE); cds *= cds; for (int i = bodies.Count - 1; i >= 0; i--) { var distance = VectorMath.DistanceSquared(player.PhysicsComponent.Position, bodies[i].Position); if (distance > cds) { world.Physics.RemoveBody(bodies[i]); bodies.RemoveAt(i); } } var close = AsteroidFieldShared.GetCloseCube(player.PhysicsComponent.Position, Field.CubeSize); var cubeRad = new Vector3(Field.CubeSize) * 0.5f; int amountCubes = (int)Math.Floor((COLLIDE_DISTANCE / Field.CubeSize)) + 1; for (int x = -amountCubes; x <= amountCubes; x++) { for (int y = -amountCubes; y <= amountCubes; y++) { for (int z = -amountCubes; z <= amountCubes; z++) { var center = close + new Vector3(x * Field.CubeSize, y * Field.CubeSize, z * Field.CubeSize); if (!Field.Zone.Shape.ContainsPoint(center)) { continue; } if (VectorMath.DistanceSquared(player.PhysicsComponent.Position, center) > cds) { continue; } float tval; if (!AsteroidFieldShared.CubeExists(center, Field.EmptyCubeFrequency, out tval)) { continue; } if (GetExclusionZone(center) != null) { continue; } bool create = true; for (int i = 0; i < bodies.Count; i++) { if ((bodies[i].Position - center).LengthFast < 3) { create = false; break; } } if (create) { var body = new RigidBody(shape); body.Orientation = new Matrix3(Field.CubeRotation.GetRotation(tval)); body.Position = center; body.IsStatic = true; bodies.Add(body); world.Physics.AddBody(body); } } } } }
void ProcessInput(TimeSpan delta) { input.Update(); if (!hud.TextEntry) { if (input.ActionDown(InputAction.ID_THROTTLEUP)) { shipInput.Throttle += (float)(delta.TotalSeconds); shipInput.Throttle = MathHelper.Clamp(shipInput.Throttle, 0, 1); } else if (input.ActionDown(InputAction.ID_THROTTLEDOWN)) { shipInput.Throttle -= (float)(delta.TotalSeconds); shipInput.Throttle = MathHelper.Clamp(shipInput.Throttle, 0, 1); } } StrafeControls strafe = StrafeControls.None; if (!hud.TextEntry) { if (input.ActionDown(InputAction.ID_STRAFELEFT)) { strafe |= StrafeControls.Left; } if (input.ActionDown(InputAction.ID_STRAFERIGHT)) { strafe |= StrafeControls.Right; } if (input.ActionDown(InputAction.ID_STRAFEUP)) { strafe |= StrafeControls.Up; } if (input.ActionDown(InputAction.ID_STRAFEDOWN)) { strafe |= StrafeControls.Down; } } var pc = player.PhysicsComponent; shipInput.Viewport = new Vector2(Game.Width, Game.Height); shipInput.Camera = camera; if (Game.Mouse.IsButtonDown(MouseButtons.Left) || mouseFlight) { var mX = Game.Mouse.X; var mY = Game.Mouse.Y; camera.MousePosition = new Vector2( mX, Game.Height - mY ); shipInput.MouseFlight = true; shipInput.MousePosition = new Vector2(mX, mY); camera.MouseFlight = true; } else { shipInput.MouseFlight = false; camera.MouseFlight = false; } control.CurrentStrafe = strafe; //control.EnginePower = Velocity / MAX_VELOCITY; var obj = GetSelection(Game.Mouse.X, Game.Mouse.Y); current_cur = obj == null ? cur_arrow : cur_reticle; var ep = VectorMath.UnProject(new Vector3(Game.Mouse.X, Game.Mouse.Y, 0.25f), camera.Projection, camera.View, new Vector2(Game.Width, Game.Height)); var tgt = VectorMath.UnProject(new Vector3(Game.Mouse.X, Game.Mouse.Y, 0f), camera.Projection, camera.View, new Vector2(Game.Width, Game.Height)); var dir = (tgt - ep).Normalized(); var dir2 = new Matrix3(player.PhysicsComponent.Body.Transform.ClearTranslation()) * Vector3.UnitZ; tgt += dir * 750; weapons.AimPoint = tgt; if (!Game.Mouse.IsButtonDown(MouseButtons.Left) && Game.TotalTime - lastDown < 0.25) { var newselected = GetSelection(Game.Mouse.X, Game.Mouse.Y); if (newselected != null) { selected = newselected; } } if (Game.Mouse.IsButtonDown(MouseButtons.Right)) { weapons.FireAll(); } }
public override void PrepareRender(ICamera camera, NebulaRenderer nr) { CalculatedVisible = true; if (Nebula != null && nr != Nebula) { CalculatedVisible = false; return; } var dsq = VectorMath.DistanceSquared(pos, camera.Position); if (LODRanges != null) //Fastest cull { var maxd = LODRanges[LODRanges.Length - 1] * sysr.LODMultiplier; maxd *= maxd; if (dsq > maxd) { CalculatedVisible = false; return; } } if (Model != null) { if (Model.Levels.Length != 0) { var center = VectorMath.Transform(Model.Levels[0].Center, World); var lvl = GetLevel(Model, center, camera.Position); if (lvl == null) { CalculatedVisible = false; } var bsphere = new BoundingSphere( center, Model.Levels[0].Radius ); if (!camera.Frustum.Intersects(bsphere)) { CalculatedVisible = false; //Culled } } } else if (Cmp != null || CmpParts != null) { //Check if -something- renders CalculatedVisible = false; var partCol = (IEnumerable <Part>)CmpParts ?? Cmp.Parts.Values; bool cmpParts = CmpParts != null; foreach (Part p in partCol) { if (cmpParts) { p.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); } var model = p.Model; Matrix4 w = World; if (p.Construct != null) { w = p.Construct.Transform * World; } if (model.Levels.Length > 0) { var center = VectorMath.Transform(model.Levels[0].Center, w); var lvl = GetLevel(model, center, camera.Position); if (lvl == null) { continue; } var bsphere = new BoundingSphere( center, model.Levels[0].Radius ); if (camera.Frustum.Intersects(bsphere)) { CalculatedVisible = true; break; } } } } else if (Sph != null) { var bsphere = new BoundingSphere( pos, radiusAtmosphere); if (!camera.Frustum.Intersects(bsphere)) { CalculatedVisible = false; } } }
public override void Draw(ICamera camera, CommandBuffer commands, SystemLighting lights, NebulaRenderer nr) { if (sysr == null) { return; } if (!Visible) { return; } if (Dfm != null) { Dfm.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); var center = VectorMath.Transform(Vector3.Zero, World); //var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, 20, nr, LitAmbient, LitDynamic, NoFog); Dfm.DrawBuffer(commands, World, Lighting.Empty); } if (Model != null) { if (Model.Levels.Length > 0) { Model.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); var center = VectorMath.Transform(Model.Levels[0].Center, World); var lvl = GetLevel(Model, center, camera.Position); if (lvl == null) { return; } var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, Model.Levels[0].Radius, nr, LitAmbient, LitDynamic, NoFog); var r = Model.Levels [0].Radius + lighting.FogRange.Y; if (lighting.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, center) <= (r * r)) { Model.DrawBufferLevel(lvl, commands, World, lighting); } } } else if (Cmp != null) { Cmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); foreach (Part p in Cmp.Parts.Values) { var model = p.Model; Matrix4 w = World; if (p.Construct != null) { w = p.Construct.Transform * World; } if (model.Levels.Length > 0) { var center = VectorMath.Transform(model.Levels[0].Center, w); var lvl = GetLevel(model, center, camera.Position); if (lvl == null) { continue; } var bsphere = new BoundingSphere( center, model.Levels[0].Radius ); if (camera.Frustum.Intersects(bsphere)) { var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, model.Levels[0].Radius, nr, LitAmbient, LitDynamic, NoFog); var r = model.Levels [0].Radius + lighting.FogRange.Y; if (lighting.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, center) <= (r * r)) { model.DrawBufferLevel(lvl, commands, w, lighting); } } } } } else if (CmpParts != null) { _parentCmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); foreach (Part p in CmpParts) { p.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); var model = p.Model; Matrix4 w = World; if (p.Construct != null) { w = p.Construct.Transform * World; } if (model.Levels.Length > 0) { var center = VectorMath.Transform(model.Levels[0].Center, w); var lvl = GetLevel(model, center, camera.Position); if (lvl == null) { continue; } var bsphere = new BoundingSphere( center, model.Levels[0].Radius ); if (camera.Frustum.Intersects(bsphere)) { var lighting = RenderHelpers.ApplyLights(lights, LightGroup, center, model.Levels[0].Radius, nr, LitAmbient, LitDynamic, NoFog); var r = model.Levels[0].Radius + lighting.FogRange.Y; if (lighting.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, center) <= (r * r)) { model.DrawBufferLevel(lvl, commands, w, lighting); } } } } } else if (Sph != null) { Sph.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); var l = RenderHelpers.ApplyLights(lights, LightGroup, pos, Sph.Radius, nr, LitAmbient, LitDynamic, NoFog); var r = Sph.Radius + l.FogRange.Y; if (l.FogMode != FogModes.Linear || VectorMath.DistanceSquared(camera.Position, pos) <= (r * r)) { Sph.DrawBuffer(commands, World, l); } } }
public override bool OutOfView(ICamera camera) { if (Model != null) { if (Model.Levels.Length != 0) { var center = VectorMath.Transform(Model.Levels[0].Center, World); var lvl = GetLevel(Model, center, camera.Position); if (lvl == null) { return(true); } var bsphere = new BoundingSphere( center, Model.Levels[0].Radius ); if (!camera.Frustum.Intersects(bsphere)) { return(true); //Culled } } } else if (Cmp != null || CmpParts != null) { //Check if -something- renders bool doCull = true; var partCol = (IEnumerable <Part>)CmpParts ?? Cmp.Parts.Values; foreach (Part p in partCol) { var model = p.Model; Matrix4 w = World; if (p.Construct != null) { w = p.Construct.Transform * World; } if (model.Levels.Length > 0) { var center = VectorMath.Transform(model.Levels[0].Center, w); var lvl = GetLevel(model, center, camera.Position); if (lvl == null) { continue; } var bsphere = new BoundingSphere( center, model.Levels[0].Radius ); if (camera.Frustum.Intersects(bsphere)) { doCull = false; break; } } } return(doCull); } else if (Sph != null) { var bsphere = new BoundingSphere( pos, radiusAtmosphere); if (!camera.Frustum.Intersects(bsphere)) { return(true); } } return(false); }
public override void DepthPrepass(ICamera camera, RenderState rstate) { if (!Visible) { return; } if (Model != null) { Model.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); if (Model.Levels.Length != 0) { var center = VectorMath.Transform(Model.Levels[0].Center, World); var lvl = GetLevel(Model, center, camera.Position); if (lvl == null) { return; } Model.DepthPrepassLevel(lvl, rstate, World); } } else if (Cmp != null || CmpParts != null) { if (Cmp != null) { Cmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); } else { _parentCmp.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); } //Check if -something- renders var partCol = (IEnumerable <Part>)CmpParts ?? Cmp.Parts.Values; foreach (Part p in partCol) { var model = p.Model; Matrix4 w = World; if (p.Construct != null) { w = p.Construct.Transform * World; } if (model.Levels.Length > 0) { var center = VectorMath.Transform(model.Levels[0].Center, w); var lvl = GetLevel(model, center, camera.Position); if (lvl == null) { continue; } var bsphere = new BoundingSphere( center, model.Levels[0].Radius ); if (camera.Frustum.Intersects(bsphere)) { model.DepthPrepassLevel(lvl, rstate, w); } } } } else if (Sph != null) { Sph.Update(camera, TimeSpan.Zero, TimeSpan.FromSeconds(sysr.Game.TotalTime)); Sph.DepthPrepass(rstate, World); } }
public void Transform(ref Matrix4 matrix, out BoundingSphere result) { result.Center = VectorMath.Transform(this.Center, matrix); result.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33))))); }
public static Lighting ApplyLights(SystemLighting src, int lightGroup, Vector3 c, float r, NebulaRenderer nebula, bool lambient = true, bool ldynamic = true, bool nofog = false) { var lights = Lighting.Create(); lights.Ambient = lambient ? src.Ambient : Color4.Black; lights.NumberOfTilesX = src.NumberOfTilesX; if (nofog) { lights.FogMode = FogModes.None; } else { lights.FogMode = src.FogMode; lights.FogDensity = src.FogDensity; lights.FogColor = src.FogColor; lights.FogRange = src.FogRange; } if (ldynamic) { for (int i = 0; i < src.Lights.Count; i++) { if (src.Lights[i].LightGroup != lightGroup) { continue; } if (!src.Lights[i].Active) { continue; } var l = src.Lights[i].Light; var r2 = r + l.Range; //l.Kind > 0 - test if not directional if (l.Kind > 0 && VectorMath.DistanceSquared(l.Position, c) > (r2 * r2)) { continue; } //Advanced spotlight cull if ((l.Kind == LightKind.Spotlight) && SpotlightTest(ref l, c, r)) { continue; } lights.Lights.Add(l); } } if (nebula != null) { Color4? ambient; bool fogenabled; Vector2 fogrange; Color4 fogcolor; RenderLight?lightning; nebula.GetLighting(out fogenabled, out ambient, out fogrange, out fogcolor, out lightning); if (ambient != null) { lights.Ambient = ambient.Value; } if (fogenabled) { lights.FogMode = FogModes.Linear; lights.FogColor = fogcolor; lights.FogRange = fogrange; } if (lightning != null && src.NumberOfTilesX == -1) { lights.Lights.Add(lightning.Value); } } return(lights); }
public static float GetZ(Vector3 cameraPosition, Vector3 vec) { var res = VectorMath.DistanceSquared(vec, cameraPosition); return(res); }
public static float GetZ(Matrix4 world, Vector3 cameraPosition, Vector3 vec) { var res = VectorMath.DistanceSquared(world.Transform(vec), cameraPosition); return(res); }
public void DrawPerspective( Texture2D texture, Vector3 pos, Matrix4 world, Vector2 size, Color4 color, Vector2 topleft, Vector2 topright, Vector2 bottomleft, Vector2 bottomright, Vector3 normal, float angle, int layer, BlendMode blend = BlendMode.Normal ) { var upref = Vector3.UnitY; if ((Vector3.UnitY - normal).Length < float.Epsilon) { upref = Vector3.UnitZ; } var srcright = Vector3.Cross(normal, upref); var srcup = Vector3.Cross(srcright, normal); srcup.Normalize(); srcright.Normalize(); Vector3 up, right; if (Math.Abs(angle) < float.Epsilon) { up = srcup; right = srcright; } else { var s = (float)Math.Sin(angle); var c = (float)Math.Cos(angle); up = c * srcright - s * srcup; right = s * srcright + c * srcup; } rendat[billboardCount] = new RenderData( texture, blend, RenderKind.Rect, (ushort)vertexCountRect ); // var sz = 0.5f * size; verticesRect[vertexCountRect++] = new VertexPositionColorTexture( VectorMath.Transform( pos - right * sz.X - up * sz.Y, world ), color, bottomleft ); verticesRect[vertexCountRect++] = new VertexPositionColorTexture( VectorMath.Transform( pos + right * sz.X - up * sz.Y, world ), color, topleft ); verticesRect[vertexCountRect++] = new VertexPositionColorTexture( VectorMath.Transform( pos - right * sz.X + up * sz.Y, world ), color, bottomright ); verticesRect[vertexCountRect++] = new VertexPositionColorTexture( VectorMath.Transform( pos + right * sz.X + up * sz.Y, world ), color, topright ); // var z = RenderHelpers.GetZ(camera.Position, pos); buffer.AddCommand( this, rendat[billboardCount].GetHashCode(), billboardCount, layer, z ); billboardCount++; }