void taso2(PhysicsObject nappi1) { ClearAll(); PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); IsMouseVisible = true; Level.Background.Image = valintaruutu; PhysicsObject nappi2 = new PhysicsObject(550, 650); nappi2.Color = Color.Transparent; nappi2.Y = Screen.BottomSafe + 220; nappi2.X = Screen.RightSafe - 1400; PhysicsObject nappi3 = new PhysicsObject(650, 650); nappi3.Color = Color.Transparent; nappi3.Y = Screen.BottomSafe + 220; nappi3.X = Screen.RightSafe - 420; Add(nappi2); Add(nappi3); Mouse.ListenOn(nappi2, MouseButton.Left, ButtonState.Pressed, taso4, "", nappi2); Mouse.ListenOn(nappi3, MouseButton.Left, ButtonState.Pressed, taso3, "", nappi3); }
// Opettajalle: Osuman käsittely on void LaserOsui(PhysicsObject ammus, PhysicsObject kohde) { ammus.Destroy(); if (kohde.Tag == "isokivi" || kohde.Tag == "kivi") { kohde.Destroy(); if (kohde.Tag == "isokivi") { for (int i = 0; i < 3; i++) { PhysicsObject sirpale = new PhysicsObject(50, 50, Shape.Rectangle); sirpale.Tag = "kivi"; sirpale.Position = kohde.Position + RandomGen.NextVector(30, 40); Add(sirpale); } } Explosion boom = new Explosion(100); boom.Force = boom.Force / 1000; //boom.UseShockWave = false; boom.Position = kohde.Position; Add(boom); } }
void LuoKentta() { pallo = new PhysicsObject(40.0, 40.0); AddCollisionHandler(pallo, KasittelePallonTormays); pallo.Shape = Shape.Circle; pallo.X = -200.0; pallo.Y = 0.0; pallo.Restitution = 1.0; Add(pallo); maila1 = LuoMaila (Level.Left + 20.0, 0.0); maila2 = LuoMaila (Level.Right - 20.0, 0.0); vasenReuna = Level.CreateLeftBorder(); vasenReuna.Restitution = 1.0; vasenReuna.IsVisible = false; oikeaReuna = Level.CreateRightBorder(); oikeaReuna.Restitution = 1.0; oikeaReuna.IsVisible = false; PhysicsObject alaReuna = Level.CreateBottomBorder(); alaReuna.Restitution = 1.0; alaReuna.IsVisible = false; PhysicsObject yläReuna = Level.CreateTopBorder(); yläReuna.Restitution = 1.0; yläReuna.IsVisible = false; Level.Background.Color = Color.Black; Camera.ZoomToLevel(); }
void PudotaOmenoita(int lukumaara) { for (int i = 0; i < lukumaara; i++) { PhysicsObject omena = new PhysicsObject(80, 80); omena.Shape = Shape.Circle; omena.Color = Color.Red; omena.Y = Screen.Top; omena.Restitution = 0.5; GameObject lehti = new GameObject(30, 30); lehti.Shape = Shape.Star; lehti.Color = Color.Green; lehti.Y = 40; omena.Add(lehti); Add(omena); omena.Hit(RandomGen.NextVector(50, 100)); Mouse.ListenOn(omena, MouseButton.Left, ButtonState.Pressed, OmenaaKlikattu, "omenaa klikattu", omena); } omenoitaIlmassa = lukumaara; }
public override void Begin() { Steve = new PhysicsObject(10, 10); Steve.Shape = Shape.Circle; Steve.Restitution = 2.0; Add(Steve); Camera.Follow(Steve); NormKenttä(); Camera.ZoomFactor = 1; //CreateMonster(); PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); Keyboard.Listen(Key.A, ButtonState.Down, LiikutaPelaajaa, null, new Vector(-200, 0)); Keyboard.Listen(Key.D, ButtonState.Down, LiikutaPelaajaa, null, new Vector(200, 0)); Keyboard.Listen(Key.W, ButtonState.Down, LiikutaPelaajaa, null, new Vector(0, 200)); Keyboard.Listen(Key.S, ButtonState.Down, LiikutaPelaajaa, null, new Vector(00, -200)); Keyboard.Listen(Key.D, ButtonState.Released, LiikutaPelaajaa, null, new Vector(0, 0)); Keyboard.Listen(Key.W, ButtonState.Released, LiikutaPelaajaa, null, new Vector(0, 0)); Keyboard.Listen(Key.S, ButtonState.Released, LiikutaPelaajaa, null, new Vector(0, -0)); Keyboard.Listen(Key.A, ButtonState.Released, LiikutaPelaajaa, null, new Vector(-0, 0)); }
public void LisaaKivi() { PhysicsObject kivi = new PhysicsObject(200, 200, Shape.Hexagon); kivi.Tag = "isokivi"; kivi.Position = RandomGen.NextVector(Screen.Left + 100, Screen.Bottom + 100, Screen.Right- 100, Screen.Top-100); Add(kivi); }
public override void Begin() { Level.Size = Screen.Size; Level.Background.CreateStars(); Surfaces borders = Level.CreateBorders(); Camera.ZoomToLevel(); PhysicsObject pelaaja = new PhysicsObject(50, 100, Shape.Triangle); Weapon ase = new LaserGun(20, 20); ase.IsVisible = false; ase.Angle = Angle.FromDegrees(90); // Opettajalle huom: Laserin osumisen käsittely on Tämän selittäminen ajan kanssa on tärkeää. Kerro tarkasti, mitä min parametri tarkoittaa ja miksi kutsuttava aliohjelmat saa ne parametrit mitkä saavat. ase.ProjectileCollision = LaserOsui; pelaaja.Add(ase); Add(pelaaja); LisaaKivi(); LisaaKivi(); LisaaKivi(); PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); // Opettajalle huom: Näiden selittäminen ajan kanssa on tärkeää. Kerro tarkasti, mitä min parametri tarkoittaa ja miksi kutsuttava aliohjelmat saa ne parametrit mitkä saavat. Keyboard.Listen(Key.Space, ButtonState.Pressed, Ammu, "Ammu rakettia", ase); Keyboard.Listen(Key.Up, ButtonState.Down, KaytaRakettia, "Käytä rakettia", pelaaja); Keyboard.Listen(Key.Left, ButtonState.Down, Kaanny, "Käänny oikealle", pelaaja, 5.0); Keyboard.Listen(Key.Left, ButtonState.Released, Kaanny, "", pelaaja, 0.0); Keyboard.Listen(Key.Right, ButtonState.Down, Kaanny, "Käytä rakettia", pelaaja, -5.0); Keyboard.Listen(Key.Right, ButtonState.Released, Kaanny, "", pelaaja, 0.0); }
void LuoKentta() { pallo = new PhysicsObject(40.0, 40.0); pallo.Shape = Shape.Circle; pallo.X = -200.0; pallo.Y = 0.0; pallo.Restitution = 1.0; pallo.KineticFriction = 0.0; pallo MomentOfIntertia = double.PositiveInfinity; Add(pallo); AddCollisionHandler (pallo, KasittelePallonTormays); maila1 = LuoMaila(Level.Left + 20.0, 0.0); maila2 = LuoMaila(Level.Right - 20.0, 0.0); vasenReuna = Level.CreateBorders(); vasenreuna.Restitution = 1.0; vasenReuna.KineticFriction = 0.0; vasenreuna.IsVisible =false; Level.CreateBorders(1.0, false); Level.BackgroundColor = Color.Black; Camera.ZoomToLevel(); }
void seina(Vector paikka,double leveys,double korkeus) { SeInA = new PhysicsObject(40, 40); SeInA.Color = Color.Blue; SeInA.Position=paikka; Add(SeInA); }
public void SelectItem(PhysicsObject item) { if (ItemSelected != null) { ItemSelected(item); } }
public override void Begin() { PhysicsObject Greg = new PhysicsObject(70, 100); Add(Greg); Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); }
// simulation needs // fire a projectile // instantiate the projectile and the target at a given distance to each other // calculate the path of the projectile with a given mass and speed // calculate the point of impact and how the material will deform // play the simulation // Use this for initialization void Start() { rb = projectile.GetComponent<Rigidbody>(); po = projectile.GetComponent<PhysicsObject>(); projectileStartPosition = projectile.transform.position; targetPosition = target.transform.position; }
void lisaalaatikko(Vector paikka, double leveys, double korkeus) { PhysicsObject laatikko = new PhysicsObject(leveys, korkeus); Add(laatikko); laatikko.Position = paikka; laatikko.Color = Color.Brown; laatikko.Restitution = 0.2; }
// Ammus tuhoaa vihollisen ja maata (ruskeat tiilet) void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde) { if (kohde.Tag == "vihollinen" || kohde.Tag == "maa") { kohde.Destroy(); } ammus.Destroy(); }
void Ammuaseella(PhysicsObject pelaaja) { PhysicsObject ammus = pelaajan1ase.Shoot(); if (ammus != null) { ammus.Size *= 3; } }
void SteveTormaa(PhysicsObject Tormaaja, PhysicsObject Kohde) { Angle kulma = new Angle(); kulma.Degrees = 0; Tormaaja.Angle = kulma; }
void klikattuomenaa(PhysicsObject klikattuOmena) { if (klikattuOmena.Color == Color.Red) klikattuOmena.Destroy(); pistelaskuri.AddValue(100); omenoitailmassa = omenoitailmassa - 1; tarkistaonkokaikkikiinni(); }
public override void Tehtava3() { PhysicsObject pallo = new PhysicsObject(40.0, 40.0); pallo.Shape = Shape.Circle; pallo.Color = Color.Red; Add(pallo); // TODO: Ota tehtävä pois kommenteista ja kirjoita toteutus tähän }
void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde) { if (kohde.Tag == "pallo") { ammus.Destroy(); kohde.Destroy(); } ammus.Destroy(); }
void PossuunOsui(PhysicsObject osuja, PhysicsObject kohde) { if (kohde != Maa) { Explosion PUM = new Explosion(200); PUM.Position = osuja.Position; Add(PUM); osuja.Destroy(); } }
void AsetaNopeus(PhysicsObject maila,Vector nopeus) { if ((nopeus.Y > 0) && (maila.Top > Level.Top)) { maila.Velocity = Vector.Zero; return; } maila.Velocity = nopeus; }
public override void collitionWith(PhysicsObject obj) { if (Alive && (obj is Stone || obj is Bike)) { World.points += 100; Alive = false; Velocity *= new Vector2(-1, 0f); Animation.Stick = true; } }
void AmmusOsui(PhysicsObject tormaaja, PhysicsObject kohde) { if (kohde.Tag == "pelaaja"){ MultiSelectWindow loppuvalikko = new MultiSelectWindow("PelinLoppuvalikko", "käynnistä uudelleen?", "Lopeta?"); loppuvalikko.ItemSelected += PelinLoppuvalikko; Add(loppuvalikko); } }
/// <summary>Adds obj to the data structures, starts updating obj. /// Returns true if obj hadn't already been added.</summary> public bool AddObject(PhysicsObject obj) { if (!objs.Contains(obj)) { objs.Add(obj); if (obj is CollidingObject) colls.Add(obj as CollidingObject); return true; } return false; }
public override void collitionWith(PhysicsObject obj) { if (alive && (obj is Stone || obj is Bike)) { World.points += 300; alive = false; Velocity = new Vector2(Velocity.X / 2, 10f); Acc += new Vector2(0, 1f); Animation.Stick = true; } }
void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) { if (kohde == oikeaReuna) { pelaajan1Pisteet.Value += 1; } else if (kohde == vasenReuna) { pelaajan2Pisteet.Value += 1; } }
void OmenaaKlikattu(PhysicsObject klikattuOmena) { if (klikattuOmena.Color == Color.Red) { klikattuOmena.Destroy(); pisteLaskuri.AddValue(100); omenoitaIlmassa = omenoitaIlmassa - 1; TarkistaOnkoKaikkiKiinni(); } }
void luopilvi() { pilviaLuotu=pilviaLuotu+1; PhysicsObject pilvi = new PhysicsObject(20, 20); pilvi.Image = (pv); pilvi.IgnoresGravity = true; pilvi.Position = Level.GetRandomPosition(); pilvi.Size = pilvi.Size* pilviaLuotu/3.0; Add(pilvi); AddCollisionHandler(hahmo, pilvi, sähkö); Timer.SingleShot(0.5, luopilvi); }
void OmenaaKlikattu(PhysicsObject klikattuOmena) { // TODO: Lisää ehto, tarkista onko punainen klikattuOmena.Destroy(); pisteLaskuri.AddValue(100); omenoitaIlmassa -= 1; TarkistaOnkoKaikkiKiinni(); //moikks muidts tää }
// Use this for initialization void Start() { //Aquire references UiManager = FindObjectOfType <UIManager>(); if (UiManager == null) { Debug.Log("UiManager not found!"); } mainCamController = Camera.main.GetComponent <CamController>(); if (mainCamController == null) { Debug.Log("Main Cam Controller fot Found."); } previewCamCtrlr = FindObjectOfType <ObjectCamCtrlr>(); if (previewCamCtrlr == null) { Debug.Log("Preview Cam Controller not found!"); } //Apply Random Spin around local Y axis Vector3 spinVector = transform.up * Random.Range(0.1f, 2.0f); rb.angularVelocity = spinVector; trailRenderer = GetComponentInChildren <TrailRenderer>(); if (spawnWithOrbit) { float strongestForce = 0.0f; PhysicsObject strongestObj = null; // Sort PhysicsObjects by Mass physicsObjects.Sort((y, x) => x.rb.mass.CompareTo(y.rb.mass)); //Find Object with highest gravitational influence foreach (PhysicsObject obj in physicsObjects) { //Obtain Direction Vector Vector3 dir = rb.position - obj.rb.position; //Obtain Distance, return if 0 float dist = dir.magnitude; if (dist != 0) { //Calculate Magnitude of force float magnitude = G * (rb.mass * obj.rb.mass) / Mathf.Pow(dist, 2); //Calculate force Vector3 force = dir.normalized * magnitude; if (force.magnitude >= strongestForce) { strongestObj = obj; strongestForce = force.magnitude; } } } //Attempt to achive stable orbit if (strongestObj != null) { //Obtain Oblique vector along y plane Vector3 dir = rb.position - strongestObj.rb.position; float dist = dir.magnitude; Vector3 requiredV = new Vector3(dir.z, dir.y, -dir.x); float vMag = Mathf.Sqrt(G * strongestObj.rb.mass / dist); requiredV = requiredV.normalized * vMag; rb.velocity = requiredV + strongestObj.rb.velocity; } } }
public void Create() { PhysicsObject objectBase = null; Shape userShape1 = scene.Factory.ShapeManager.Find("UserShape 1"); Shape userShape2 = scene.Factory.ShapeManager.Find("UserShape 2"); Shape userShape3 = scene.Factory.ShapeManager.Find("UserShape 3"); Shape userShape4 = scene.Factory.ShapeManager.Find("UserShape 4"); Shape userShape5 = scene.Factory.ShapeManager.Find("UserShape 5"); Shape userShape6 = scene.Factory.ShapeManager.Find("UserShape 6"); Shape userShape7 = scene.Factory.ShapeManager.Find("UserShape 7"); Shape userShape8 = scene.Factory.ShapeManager.Find("UserShape 8"); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 1"); objectBase.Shape = userShape1; objectBase.UserDataStr = "UserShape1"; objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(-20.0f, 20.0f, 20.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 2"); objectBase.Shape = userShape2; objectBase.UserDataStr = "UserShape2"; objectBase.CreateSound(true); objectBase.Sound.MinNextImpactForce = 7000.0f; objectBase.InitLocalTransform.SetPosition(20.0f, 20.0f, 20.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 3"); objectBase.Shape = userShape3; objectBase.UserDataStr = "UserShape3"; objectBase.Material.TransparencyFactor = 0.5f; objectBase.CreateSound(true); objectBase.Sound.MinNextImpactForce = 7000.0f; objectBase.InitLocalTransform.SetPosition(20.0f, 20.0f, 5.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 4"); objectBase.Shape = userShape4; objectBase.UserDataStr = "UserShape4"; objectBase.Material.TransparencyFactor = 0.5f; objectBase.CreateSound(true); objectBase.Sound.MinNextImpactForce = 7000.0f; objectBase.InitLocalTransform.SetPosition(0.0f, 20.0f, 20.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 5"); objectBase.Shape = userShape5; objectBase.UserDataStr = "UserShape5"; objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(-20.0f, 20.0f, 40.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 6"); objectBase.Shape = userShape6; objectBase.UserDataStr = "UserShape6"; objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(20.0f, 20.0f, 40.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 7"); objectBase.Shape = userShape7; objectBase.UserDataStr = "UserShape7"; objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(6.0f, 20.0f, 5.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 8"); objectBase.Shape = userShape8; objectBase.UserDataStr = "UserShape8"; objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(0.0f, 20.0f, 40.0f); objectBase.InitLocalTransform.SetScale(2.0f); objectBase.Integral.SetDensity(1.0f); scene.UpdateFromInitLocalTransform(objectBase); }
/// LateUpdate is called every frame, if the Behaviour is enabled. /// It is called after all Update functions have been called. void LateUpdate() { // Check incase object has been destroyed or force is yet to be calculated PhysicsObject newInfluencer; PhysicsEngine.ForceExerter forceExerter; if (physicsEngine.strongest_force.TryGetValue(ID, out forceExerter) && physicsObjects.ContainsKey(forceExerter.id)) { newInfluencer = physicsObjects[forceExerter.id]; // If new influencer if (newInfluencer != biggestGravitationalInfluencer) { if (_drawPastPath) { // Clear previous trail if in new orbit relativeTrailPositions.Clear(); } } // Update biggestGravitationalInfluencer = newInfluencer; } // Future relative predicted path if (UiManager.displayFuturePath) { // First frame setup if (!_drawFuturePath) { // Setup colour keys GradientColorKey[] colorKeys = new GradientColorKey[2]; colorKeys[0].color = Color.blue; colorKeys[0].time = 0.0f; colorKeys[1].color = Color.blue; colorKeys[1].time = 0.0f; // Setup alpha keys GradientAlphaKey[] alphaKeys = new GradientAlphaKey[3]; alphaKeys[0].alpha = 1.0f; alphaKeys[0].time = 0.0f; alphaKeys[1].alpha = 1.0f; alphaKeys[1].time = 0.5f; alphaKeys[2].alpha = 0.0f; alphaKeys[2].time = 1.0f; // Set colour gradient Gradient gradient = new Gradient(); gradient.SetKeys(colorKeys, alphaKeys); lineRenderer.colorGradient = gradient; // Set flag _drawFuturePath = true; } lineRenderer.useWorldSpace = true; uint segments = 500; // Predict future path Vector3[] positions = PredictOrbit(biggestGravitationalInfluencer, segments); if (positions != null) { // Set positions lineRenderer.positionCount = (int)segments; lineRenderer.SetPositions(positions); } } // Past Drawn relative path else if (UiManager.displayPastPath) { // First frame setup if (!_drawPastPath) { // Setup colour keys GradientColorKey[] colorKeys = new GradientColorKey[2]; colorKeys[0].color = Color.cyan; colorKeys[0].time = 0.0f; colorKeys[1].color = Color.cyan; colorKeys[1].time = 0.0f; // Setup alpha keys GradientAlphaKey[] alphaKeys = new GradientAlphaKey[3]; alphaKeys[0].alpha = 0.0f; alphaKeys[0].time = 0.0f; alphaKeys[1].alpha = 1.0f; alphaKeys[1].time = 0.5f; alphaKeys[2].alpha = 1.0f; alphaKeys[2].time = 1.0f; // Set colour gradient Gradient gradient = new Gradient(); gradient.SetKeys(colorKeys, alphaKeys); lineRenderer.colorGradient = gradient; // Set flag _drawPastPath = true; } // Update trail vertex count lineRenderer.positionCount = relativeTrailPositions.Count; // Manually translate all positions to acheive local space without rotation or scale lineRenderer.useWorldSpace = true; // for each position for (int i = 0; i < lineRenderer.positionCount; i++) { Vector3 relativePos = relativeTrailPositions.ElementAt(i); lineRenderer.SetPosition(i, relativePos + biggestGravitationalInfluencer.transform.position); } // Track position if (timeSinceLastPosition > 1.0f && Time.timeScale != 0.0f) { // Add to relative positions queue relativeTrailPositions.Enqueue(transform.position - biggestGravitationalInfluencer.transform.position); // Reset timer timeSinceLastPosition = 0.0f; } // Increment timer else { timeSinceLastPosition += Time.unscaledDeltaTime * 100.0f; } } // If flag is true but realtime value false: // Turn off future path if (_drawFuturePath && !UiManager.displayFuturePath) { // Reset flag _drawFuturePath = false; // Clear path lineRenderer.positionCount = 0; } // If flag is true but realtime value false: // Turn off past path if (_drawPastPath && !UiManager.displayPastPath) { // Reset flag _drawPastPath = false; // Clear path lineRenderer.positionCount = 0; relativeTrailPositions.Clear(); } }
/// <summary> /// Asettaa nopeuden oliolle /// </summary> /// <param name="olio">olio</param> /// <param name="nopeus">nopeus</param> void AsetaNopeus(PhysicsObject olio, Vector nopeus) { olio.Velocity = nopeus; }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } float time = (float)args.Time; Vector3 deltaRotation = vectorZero; Vector3 deltaTranslation = vectorZero; float rotationSpeed = 8.0f; float translationSpeed = 8.0f; float jumpSpeed = 8.0f; float swimUpSpeed = 0.2f; float swimUpOnSurfaceSpeed = 0.06f; float translationInFluidFactor = 0.15f; float soundPositionFactor = 0.1f; bool enableJump = false; bool enableSwimUp = false; bool enableShot = false; bool cameraBodyCollision = cameraBody.IsColliding(); bool cameraDownCollision = cameraDown.IsColliding(); DemoMouseState mouseState = demo.GetMouseState(); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (mouseState[MouseButton.Right]) { deltaRotation.Y += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.X - oldMouseState.X) * time); deltaRotation.X += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.Y - oldMouseState.Y) * time); } mousePosition.X = mouseState.X; mousePosition.Y = mouseState.Y; if (!objectBase.Camera.EnableControl) { if (mouseState[MouseButton.Middle] && !oldMouseState[MouseButton.Middle]) { enableShot = true; } if ((keyboardState[Key.ControlRight] && !oldKeyboardState[Key.ControlRight]) || (keyboardState[Key.ControlLeft] && !oldKeyboardState[Key.ControlLeft])) { enableShot = true; } } PhysicsObject cursorA = scene.Factory.PhysicsObjectManager.Find(cursorAName); PhysicsObject cursorB = scene.Factory.PhysicsObjectManager.Find(cursorBName); if (!demo.EnableMenu) { if (cursorA != null) { cursorA.EnableDrawing = true; } if (cursorB != null) { cursorB.EnableDrawing = true; } } else { if (cursorA != null) { cursorA.EnableDrawing = false; } if (cursorB != null) { cursorB.EnableDrawing = false; } } if (!objectBase.Camera.EnableControl) { if (keyboardState[Key.W]) { deltaTranslation.Z += translationSpeed * time; } if (keyboardState[Key.S]) { deltaTranslation.Z -= translationSpeed * time; } if (keyboardState[Key.D]) { deltaTranslation.X += translationSpeed * time; } if (keyboardState[Key.A]) { deltaTranslation.X -= translationSpeed * time; } if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { enableJump = true; } if (keyboardState[Key.Space]) { enableSwimUp = true; } } if (keyboardState[Key.Tab] && !oldKeyboardState[Key.Tab]) { enableDistance = !enableDistance; } oldMouseState = mouseState; oldKeyboardState = keyboardState; Vector3 gravityDirection = vectorZero; scene.GetGravityDirection(ref gravityDirection); if (!objectBase.Camera.EnableControl) { if (deltaRotation.LengthSquared != 0.0f) { Vector3 euler = vectorZero; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.Camera.SetEuler(ref euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out cameraRotation); objectBase.Camera.SetRotation(ref cameraRotation); Matrix4.CreateRotationY(euler.Y, out rotation); objectBase.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); objectBase.RigidGroupOwner.RecalculateMainTransform(); } } else { Vector3 euler = vectorZero; Matrix4 objectRotation = matrixIdentity; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.RigidGroupOwner.MainWorldTransform.GetRotation(ref objectRotation); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(euler.X, out rotationX); Matrix4.CreateRotationY(euler.Y, out rotationY); Matrix4.Mult(ref rotationX, ref rotationY, out cameraRotation); Matrix4.Mult(ref cameraRotation, ref objectRotation, out rotation); objectBase.Camera.SetEuler(ref euler); objectBase.Camera.SetTransposeRotation(ref rotation); } if (deltaTranslation.LengthSquared != 0.0f) { if (objectBase.RigidGroupOwner.IsUnderFluidSurface) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; if (enableSwimUp) { objectBase.InitLocalTransform.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref direction, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, translationInFluidFactor * objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } else { objectBase.Camera.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, translationInFluidFactor * objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } } else { if (cameraDownCollision) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 100000.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 100000.0f; objectBase.InitLocalTransform.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref direction, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); cameraDown.UpdateFeedbackForce(ref moveForce); } else { if (objectBase.RigidGroupOwner.IsInFluid) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; objectBase.InitLocalTransform.GetTransposeRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref direction, ref rotation, out moveForce); Vector3.Multiply(ref moveForce, translationInFluidFactor * objectBase.RigidGroupOwner.Integral.Mass / (time * time), out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } } } } if (enableSwimUp) { if (cameraDown.IsUnderFluidSurface && cameraBody.IsUnderFluidSurface) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; PhysicsObject fluidPhysicsObject = objectBase.RigidGroupOwner.FluidPhysicsObject; fluidPhysicsObject.InternalControllers.FluidController.GetNormal(ref fluidNormal); Vector3.Multiply(ref fluidNormal, swimUpSpeed * objectBase.RigidGroupOwner.Integral.Mass / time, out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } else if (!cameraDownCollision && cameraBody.IsInFluid && (deltaTranslation.LengthSquared == 0.0f)) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 10.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 10.0f; PhysicsObject fluidPhysicsObject = objectBase.RigidGroupOwner.FluidPhysicsObject; fluidPhysicsObject.InternalControllers.FluidController.GetNormal(ref fluidNormal); Vector3.Multiply(ref fluidNormal, swimUpOnSurfaceSpeed * objectBase.RigidGroupOwner.Integral.Mass / time, out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); } } if (enableJump) { if (!enableControl && !objectBase.Camera.EnableControl && cameraDownCollision && !cameraDown.IsUnderFluidSurface && !cameraBody.IsUnderFluidSurface) { objectBase.RigidGroupOwner.MaxPreUpdateLinearVelocity = 100000.0f; objectBase.RigidGroupOwner.MaxPostUpdateLinearVelocity = 100000.0f; Vector3.Multiply(ref gravityDirection, -jumpSpeed * objectBase.RigidGroupOwner.Integral.Mass / time, out moveForce); objectBase.RigidGroupOwner.WorldAccumulator.AddWorldForce(ref moveForce); cameraDown.UpdateFeedbackForce(ref moveForce); } } if (enableDistance) { if (distance > maxDistance) { distance -= 2.0f; } if (enableDistanceCollision) { float margin = 1.0f; objectBase.MainWorldTransform.GetPosition(ref startPoint); objectBase.Camera.GetTransposeRotation(ref cameraRotation); direction.X = cameraRotation.Row2.X; direction.Y = cameraRotation.Row2.Y; direction.Z = cameraRotation.Row2.Z; Vector3.Multiply(ref direction, distance, out direction); Vector3.Add(ref startPoint, ref direction, out endPoint); scene.UpdatePhysicsObjectsIntersectedBySegment(ref startPoint, ref endPoint, margin, true); float minDistance = float.MaxValue; float curDistance = 0.0f; for (int i = 0; i < scene.IntersectedPhysicsObjectsCount; i++) { PhysicsObject hitObject = scene.GetIntersectedPhysicsObject(i, ref hitPoint); if (hitObject.RigidGroupOwner == objectBase.RigidGroupOwner) { continue; } //if ((hitObject.InternalControllers.FluidController != null) && hitObject.InternalControllers.FluidController.Enabled) // continue; if (!hitObject.EnableCollisions) { continue; } Vector3.Subtract(ref startPoint, ref hitPoint, out hitDistance); curDistance = hitDistance.Length; if (curDistance < minDistance) { minDistance = curDistance; } } if (minDistance < Math.Abs(distance)) { distance = -minDistance; } } } else { if (distance < 0.0f) { distance += 2.0f; } if (distance > 0.0f) { distance = 0.0f; } } if (enableDistance) { if (distance > maxDistance) { if ((cameraUp != null) && (cameraBody != null) && (cameraDown != null)) { objectBase.RigidGroupOwner.EnableDrawing = true; cameraUp.EnableDrawing = true; cameraBody.EnableDrawing = true; cameraDown.EnableDrawing = true; } } } else { if (distance >= 0.0f) { if ((cameraUp != null) && (cameraBody != null) && (cameraDown != null)) { objectBase.RigidGroupOwner.EnableDrawing = false; cameraUp.EnableDrawing = false; cameraBody.EnableDrawing = false; cameraDown.EnableDrawing = false; } } } enableControl = objectBase.Camera.EnableControl; float gravityDistance = 0.0f; Vector3 gravityLinearVelocity = vectorZero; Vector3 tangentLinearVelocity = vectorZero; Vector3 velocity = vectorZero; objectBase.MainWorldTransform.GetLinearVelocity(ref velocity); Vector3.Dot(ref gravityDirection, ref velocity, out gravityDistance); Vector3.Multiply(ref gravityDirection, gravityDistance, out gravityLinearVelocity); Vector3.Subtract(ref velocity, ref gravityLinearVelocity, out tangentLinearVelocity); float tangentLength = tangentLinearVelocity.Length; if (tangentLength > maxTangentLength) { tangentLinearVelocity *= maxTangentLength / tangentLength; } Vector3.Add(ref gravityLinearVelocity, ref tangentLinearVelocity, out velocity); objectBase.RigidGroupOwner.MainWorldTransform.SetLinearVelocity(ref velocity); objectBase.Camera.Projection.CreatePerspectiveLH(1.0f, 11000.0f, 70.0f, demo.WindowWidth, demo.WindowHeight); objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); objectBase.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, distance); objectBase.Camera.UpdateFrustum(); objectBase.Camera.View.GetViewMatrix(ref view); objectBase.Camera.Projection.GetProjectionMatrix(ref projection); Vector3 rayPosition, rayDirection; rayPosition = rayDirection = vectorZero; objectBase.UnProjectToRay(ref mousePosition, 0, 0, demo.WindowWidth, demo.WindowHeight, 0.0f, 1.0f, ref view, ref matrixIdentity, ref projection, ref rayPosition, ref rayDirection); PhysicsObject cursor = scene.Factory.PhysicsObjectManager.Find(cursorName); if (cursor != null) { Vector3 cursorPosition = vectorZero; Matrix4 cursorLocalRotation = matrixIdentity; Matrix4 cursorWorldRotation = matrixIdentity; cursor.InitLocalTransform.GetPosition(ref cursorPosition); cursor.InitLocalTransform.GetRotation(ref cursorLocalRotation); cursor.MainWorldTransform.GetRotation(ref cursorWorldRotation); objectBase.Camera.GetTransposeRotation(ref cameraRotation); Matrix4.Mult(ref cursorLocalRotation, ref cameraRotation, out rotation); Vector3.TransformVector(ref cursorPosition, ref cursorWorldRotation, out position); cursor.MainWorldTransform.SetRotation(ref rotation); Vector3.Add(ref position, ref rayPosition, out position); Vector3.Add(ref position, ref rayDirection, out position); cursor.MainWorldTransform.SetPosition(ref position); cursor.RecalculateMainTransform(); } CursorController cursorController = objectBase.InternalControllers.CursorController; if (cursorController.IsDragging) { if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.InternalControllers.HeightmapController != null) && cursorController.HitPhysicsObject.InternalControllers.HeightmapController.Enabled) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); float dir = direction.Y; if (dir != 0.0f) { Vector3 scale = vectorZero; cursorController.HitPhysicsObject.MainWorldTransform.GetScale(ref scale); float positionX = cursorStartPosition.X + 0.5f * scale.X; float positionY = cursorStartPosition.Y + 0.5f * scale.Y; float positionZ = cursorStartPosition.Z + 0.5f * scale.Z; cursorController.HitPhysicsObject.InternalControllers.HeightmapController.AddHeight(positionX, positionY, positionZ, dir / scale.Y); cursorController.HitPhysicsObject.InternalControllers.HeightmapController.UpdateBounding(); // To change the friction and restitution of the heightmap surface by the cursor, add the following lines of code //cursorController.HitPhysicsObject.InternalControllers.HeightmapController.SetFriction(positionX, positionY, positionZ, 0.0f); //cursorController.HitPhysicsObject.InternalControllers.HeightmapController.SetRestitution(positionX, positionY, positionZ, 2.0f); cursorStartPosition.Y += dir; cursorController.SetAnchor1(ref cursorStartPosition); } } if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.InternalControllers.HeightmapController != null) && cursorController.HitPhysicsObject.InternalControllers.HeightmapController.Enabled) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); if (direction.LengthSquared != 0.0f) { // To move the heightmap surface by the cursor, add the following lines of code //cursorController.HitPhysicsObject.MainWorldTransform.GetPosition(ref cursorStartPosition); //Vector3.Add(ref cursorStartPosition, ref direction, out cursorStartPosition); //cursorController.HitPhysicsObject.MainWorldTransform.SetPosition(ref cursorStartPosition); //cursorController.HitPhysicsObject.RecalculateMainTransform(); } } if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.InternalControllers.FluidController != null) && cursorController.HitPhysicsObject.InternalControllers.FluidController.Enabled) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); if (direction.LengthSquared != 0.0f) { // To move the fluid surface by the cursor, add the following lines of code // and set EnableCursorInteraction flag to true in the Lake class //cursorController.HitPhysicsObject.MainWorldTransform.GetPosition(ref cursorStartPosition); //Vector3.Add(ref cursorStartPosition, ref direction, out cursorStartPosition); //cursorController.HitPhysicsObject.MainWorldTransform.SetPosition(ref cursorStartPosition); //cursorController.HitPhysicsObject.RecalculateMainTransform(); } } } objectBase.MainWorldTransform.GetPosition(ref listenerPosition); objectBase.Camera.GetTransposeRotation(ref rotation); Vector3.Multiply(ref listenerPosition, soundPositionFactor, out position); listenerTopDirection.X = rotation.Row1.X; listenerTopDirection.Y = rotation.Row1.Y; listenerTopDirection.Z = rotation.Row1.Z; listenerFrontDirection.X = rotation.Row2.X; listenerFrontDirection.Y = rotation.Row2.Y; listenerFrontDirection.Z = rotation.Row2.Z; listener.Position = position; listener.TopDirection = listenerTopDirection; listener.FrontDirection = listenerFrontDirection; listenerRange = objectBase.Sound.Range; if (enableShot) { Vector3 shotScale, shotColor; shotCount = (shotCount + 1) % shotTab.Length; string shotCountName = shotCount.ToString(); PhysicsObject shot = scene.Factory.PhysicsObjectManager.FindOrCreate(shotName + shotCountName); PhysicsObject shotBase = scene.Factory.PhysicsObjectManager.FindOrCreate(shotBaseName + shotCountName); PhysicsObject shotLight = scene.Factory.PhysicsObjectManager.FindOrCreate(shotLightName + shotCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); shotTab[shotCount] = shotBase; enableShotTab = true; shotScale = shotColor = vectorZero; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; Vector3.Multiply(ref rayDirection, 300.0f, out rayDirection); shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref rayPosition); shot.InitLocalTransform.SetLinearVelocity(ref rayDirection); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 200; shot.EnableRemovePhysicsObjectsFromManagerAfterMaxSimulationFrameCount = false; //shot.EnableLocalGravity = true; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(10.0f); shotBase.Material.RigidGroup = true; shotBase.EnableBreakRigidGroup = false; shotBase.EnableCollisions = true; shotBase.CreateSound(true); if ((cameraUp != null) && (cameraBody != null) && (cameraDown != null)) { shotBase.DisableCollision(cameraUp, true); shotBase.DisableCollision(cameraBody, true); shotBase.DisableCollision(cameraDown, true); shotBase.MaxDisableCollisionFrameCount = 50; } shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotColor.X = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Y = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Z = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotLight.Light.SetDiffuse(ref shotColor); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.RigidGroup = true; shotLight.Material.UserDataStr = yellowName; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); shotSoundGroup = demo.SoundGroups[hitName]; shotSoundGroup.MaxHitRepeatTime = 0.0f; if (shotSound == null) { shotSound = shotSoundGroup.GetSound(objectBase.Sound, listener, emitter); } shotSound.Update(time); Vector3.Multiply(ref rayPosition, soundPositionFactor, out shotSound.HitPosition); shotSound.HitVolume = 1.0f; shotSound.FrontDirection.X = rotation.Row2.X; shotSound.FrontDirection.Y = rotation.Row2.Y; shotSound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(shotSound); } else { PhysicsObject shotBase; DemoSound shotBaseSound; if (shotSound != null) { shotSound.Update(time); if (shotSound.Stop()) { shotSound.SoundGroup.SetSound(shotSound); shotSound = null; } } if (enableShotTab) { enableShotTab = false; for (int i = 0; i < shotTab.Length; i++) { shotBase = shotTab[i]; if (shotBase != null) { if (shotBase.Sound.UserDataObj != null) { enableShotTab = true; shotBaseSound = (DemoSound)shotBase.Sound.UserDataObj; shotBaseSound.Update(time); if (shotBaseSound.Stop()) { shotBaseSound.SoundGroup.SetSound(shotBaseSound); shotBase.Sound.UserDataObj = null; shotTab[i] = null; } } } } } } objectBase.Camera.UpdatePhysicsObjects(true, true, true); objectBase.Camera.SortDrawPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortTransparentPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortLightPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); PhysicsObject currentPhysicsObject; PhysicsSound currentSound; DemoSoundGroup soundGroup; DemoSound sound; if (demo.SoundQueue.SoundCount > 0) { return; } for (int i = 0; i < scene.TotalPhysicsObjectCount; i++) { if (demo.SoundQueue.SoundCount >= demo.SoundQueue.MaxSoundCount) { break; } currentPhysicsObject = scene.GetPhysicsObject(i); currentSound = !currentPhysicsObject.EnableSoundFromRigidGroupOwner ? currentPhysicsObject.Sound : currentPhysicsObject.RigidGroupOwner.Sound; if (currentSound == null) { continue; } if (!currentSound.Enabled) { continue; } if (currentSound.UserDataStr == null) { soundGroup = demo.SoundGroups[defaultName]; } else { soundGroup = demo.SoundGroups[currentSound.UserDataStr]; } if (currentPhysicsObject.IsSleeping && (currentSound.UserDataObj == null) && !soundGroup.EnableBackground) { continue; } if (currentPhysicsObject.GetSoundData(ref listenerPosition, listenerRange, soundGroup.EnableHit, soundGroup.EnableRoll, soundGroup.EnableSlide, soundGroup.EnableBackground, currentSound.BackgroundVolumeVelocityModulation, ref hitPosition, ref rollPosition, ref slidePosition, ref backgroundPosition, ref hitVolume, ref rollVolume, ref slideVolume, ref backgroundVolume)) { if (currentSound.UserDataObj == null) { sound = soundGroup.GetSound(currentSound, listener, emitter); currentSound.UserDataObj = sound; } else { sound = (DemoSound)currentSound.UserDataObj; } sound.Update(time); currentPhysicsObject.MainWorldTransform.GetTransposeRotation(ref rotation); Vector3.Multiply(ref hitPosition, soundPositionFactor, out sound.HitPosition); Vector3.Multiply(ref rollPosition, soundPositionFactor, out sound.RollPosition); Vector3.Multiply(ref slidePosition, soundPositionFactor, out sound.SlidePosition); Vector3.Multiply(ref backgroundPosition, soundPositionFactor, out sound.BackgroundPosition); sound.HitVolume = hitVolume; sound.RollVolume = rollVolume; sound.SlideVolume = slideVolume; sound.BackgroundVolume = backgroundVolume; sound.FrontDirection.X = rotation.Row2.X; sound.FrontDirection.Y = rotation.Row2.Y; sound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(sound); } else { if (currentSound.UserDataObj != null) { sound = (DemoSound)currentSound.UserDataObj; sound.Update(time); if (sound.Stop()) { soundGroup.SetSound(sound); currentSound.UserDataObj = null; } } } } }
//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 = Vector3.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 = Vector3.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 = Vector3.DistanceSquared(rb.Position, camera.Position); * if (nd < dist) * { * dist = nd; * body = rb; * } * break; * } * } * }*/ } } return(body != null); }
void KaannaPelaajaa(PhysicsObject vaunu, double nopeus) { vaunu.AngularVelocity = nopeus; vaunu.AngularDamping = 0.7; }
public CollisionHelper(PhysicsObject parent) { this.parent = parent; }
void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde) { ammus.Destroy(); muuttuja.Destroy(); }
public Event_PhysicsObjectGrounded(PhysicsObject obj) { Object = obj; }
public void execute(PhysicsObject pObj) { pObj._acceleration.Z = pObj._force.Z / pObj._spec.CurbWeight; pObj._acceleration.X = pObj._force.X / pObj._spec.CurbWeight; }
private void WalkUpdate(DwarfTime time, ChunkManager chunks) { MouseState mouse = Mouse.GetState(); KeyboardState keys = Keyboard.GetState(); if (PhysicsObject == null) { PhysicsObject = new Physics("CameraPhysics", PlayState.ComponentManager.RootComponent, Matrix.CreateTranslation(Target), new Vector3(0.5f, 0.5f, 0.5f), Vector3.Zero, 1.0f, 1.0f, 0.999f, 1.0f, Vector3.Down * 10); PhysicsObject.IsSleeping = false; PhysicsObject.Velocity = Vector3.Down * 0.01f; } bool stateChanged = false; float dt = (float)time.ElapsedGameTime.TotalSeconds; if (KeyManager.RotationEnabled()) { if (!shiftPressed) { shiftPressed = true; mouse = Mouse.GetState(); stateChanged = true; } if (!isLeftPressed && mouse.LeftButton == ButtonState.Pressed) { isLeftPressed = true; stateChanged = true; } else if (mouse.LeftButton == ButtonState.Released) { isLeftPressed = false; } if (!isRightPressed && mouse.RightButton == ButtonState.Pressed) { isRightPressed = true; stateChanged = true; } else if (mouse.RightButton == ButtonState.Released) { isRightPressed = false; } if (stateChanged) { Mouse.SetPosition(GameState.Game.GraphicsDevice.Viewport.Width / 2, GameState.Game.GraphicsDevice.Viewport.Height / 2); mouse = Mouse.GetState(); } float diffX = mouse.X - GameState.Game.GraphicsDevice.Viewport.Width / 2; float diffY = mouse.Y - GameState.Game.GraphicsDevice.Viewport.Height / 2; float filterDiffX = (float)(diffX * dt); float filterDiffY = (float)(diffY * dt); if (Math.Abs(filterDiffX) > 1.0f) { filterDiffX = 1.0f * Math.Sign(filterDiffX); } if (Math.Abs(filterDiffY) > 1.0f) { filterDiffY = 1.0f * Math.Sign(filterDiffY); } targetTheta = Theta - (filterDiffX); targetPhi = Phi - (filterDiffY); Theta = targetTheta * 0.5f + Theta * 0.5f; Phi = targetPhi * 0.5f + Phi * 0.5f; if (Phi < -1.5f) { Phi = -1.5f; } else if (Phi > 1.5f) { Phi = 1.5f; } } else { shiftPressed = false; } Vector3 velocityToSet = Vector3.Zero; if (keys.IsKeyDown(ControlSettings.Mappings.Forward) || keys.IsKeyDown(Keys.Up)) { Vector3 forward = (Target - Position); forward.Normalize(); velocityToSet += forward * CameraMoveSpeed * dt; } else if (keys.IsKeyDown(ControlSettings.Mappings.Back) || keys.IsKeyDown(Keys.Down)) { Vector3 forward = (Target - Position); forward.Normalize(); velocityToSet += -forward * CameraMoveSpeed * dt; } if (keys.IsKeyDown(ControlSettings.Mappings.Left) || keys.IsKeyDown(Keys.Left)) { Vector3 forward = (Target - Position); forward.Normalize(); Vector3 right = Vector3.Cross(forward, UpVector); right.Normalize(); velocityToSet += -right * CameraMoveSpeed * dt; } else if (keys.IsKeyDown(ControlSettings.Mappings.Right) || keys.IsKeyDown(Keys.Right)) { Vector3 forward = (Target - Position); forward.Normalize(); Vector3 right = Vector3.Cross(forward, UpVector); right.Normalize(); velocityToSet += right * CameraMoveSpeed * dt; } if (velocityToSet.LengthSquared() > 0) { Velocity = Velocity * 0.5f + velocityToSet * 0.5f; } LastWheel = mouse.ScrollWheelValue; Velocity = new Vector3(Velocity.X, 0, Velocity.Z); if (keys.IsKeyDown(Keys.Space)) { Velocity += Vector3.Up; } //CollidesWithChunks(PlayState.ChunkManager, Target, true); PhysicsObject.ApplyForce(Velocity * 20, dt); Target = PhysicsObject.GlobalTransform.Translation + Vector3.Up * 0.5f; Velocity *= 0.8f; UpdateBasisVectors(); }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); float time = args.Time; PhysicsObject physicsObjectWithActiveCamera = scene.GetPhysicsObjectWithActiveCamera(0); if (physicsObjectWithActiveCamera == null) { return; } PhysicsObject cameraBody = physicsObjectWithActiveCamera.RigidGroupOwner.FindChildPhysicsObject(cameraBodyName, true, true); PhysicsObject cameraDown = physicsObjectWithActiveCamera.RigidGroupOwner.FindChildPhysicsObject(cameraDownName, true, true); if ((chassisMiddleDown == null) || (steeringGearSwitch == null)) { return; } Constraint CameraConstraint = objectBase.Scene.Factory.ConstraintManager.Find(cameraConstraintName); bool steeringGearWorking = true; if (steeringGearConstraint != null) { if (steeringGearConstraint.PhysicsObject1 != null) { if (steeringGearConstraint.PhysicsObject1.IsBrokenRigidGroup || steeringGearConstraint.IsBroken) { steeringGearWorking = false; } } if (steeringGearConstraint.PhysicsObject2 != null) { if (steeringGearConstraint.PhysicsObject2.IsBrokenRigidGroup || steeringGearConstraint.IsBroken) { steeringGearWorking = false; } } } else { steeringGearWorking = false; } if (!steeringGearWorking) { steeringGearDown.DisableCollision(chassisUpPanel, false); steeringGearDown.DisableCollision(chassisDownPanel, false); steeringGearDown.DisableCollision(upBodyFront, false); steeringGearDown.DisableCollision(upBodyDownBack, false); } if (objectBase.IsBrokenRigidGroup || !steeringGearWorking) { if ((CameraConstraint != null) && (CameraConstraint.PhysicsObject1 == cameraDown)) { objectBase.Scene.Factory.ConstraintManager.Remove(CameraConstraint); physicsObjectWithActiveCamera.Camera.EnableControl = false; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 0.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 0.0f; cameraDown.DisableCollision(steeringWheel, false); cameraDown.DisableCollision(chassisBack, false); cameraDown.DisableCollision(chassisMiddleBack, false); cameraDown.DisableCollision(upBodyFront, false); cameraDown.DisableCollision(upBodyDownBack, false); cameraBody.DisableCollision(steeringGearDown, false); cameraBody.DisableCollision(steeringWheel, false); cameraBody.DisableCollision(chassisBack, false); cameraBody.DisableCollision(chassisMiddleBack, false); cameraBody.DisableCollision(upBodyFront, false); cameraBody.DisableCollision(upBodyDownBack, false); Vector3 euler = vectorZero; Vector3 cameraEuler = vectorZero; Vector3 objectEuler = vectorZero; physicsObjectWithActiveCamera.Camera.GetEuler(ref cameraEuler); physicsObjectWithActiveCamera.MainWorldTransform.GetTransposeRotation(ref objectRotation); physicsObjectWithActiveCamera.InitLocalTransform.GetRotation(ref objectInitRotation); Matrix4.Mult(ref objectRotation, ref objectInitRotation, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); physicsObjectWithActiveCamera.Camera.GetEuler(ref objectEuler); Vector3.Add(ref objectEuler, ref cameraEuler, out euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref euler); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); Matrix4.CreateRotationY(euler.Y, out rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); } return; } DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (physicsObjectWithActiveCamera.Camera.EnableControl && (CameraConstraint != null) && (CameraConstraint.PhysicsObject1 == cameraDown)) { if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { objectBase.Scene.Factory.ConstraintManager.Remove(CameraConstraint); physicsObjectWithActiveCamera.Camera.EnableControl = false; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 0.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 0.0f; cameraDown.DisableCollision(steeringWheel, false); cameraDown.DisableCollision(chassisBack, false); cameraDown.DisableCollision(chassisMiddleBack, false); cameraDown.DisableCollision(upBodyFront, false); cameraDown.DisableCollision(upBodyDownBack, false); cameraBody.DisableCollision(steeringGearDown, false); cameraBody.DisableCollision(steeringWheel, false); cameraBody.DisableCollision(chassisBack, false); cameraBody.DisableCollision(chassisMiddleBack, false); cameraBody.DisableCollision(upBodyFront, false); cameraBody.DisableCollision(upBodyDownBack, false); Vector3 euler = vectorZero; Vector3 cameraEuler = vectorZero; Vector3 objectEuler = vectorZero; physicsObjectWithActiveCamera.Camera.GetEuler(ref cameraEuler); physicsObjectWithActiveCamera.MainWorldTransform.GetTransposeRotation(ref objectRotation); physicsObjectWithActiveCamera.InitLocalTransform.GetRotation(ref objectInitRotation); Matrix4.Mult(ref objectRotation, ref objectInitRotation, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); physicsObjectWithActiveCamera.Camera.GetEuler(ref objectEuler); Vector3.Add(ref objectEuler, ref cameraEuler, out euler); euler.Z = 0.0f; Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref euler); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); Matrix4.CreateRotationY(euler.Y, out rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); oldKeyboardState = keyboardState; return; } if (keyboardState[Key.W]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.S]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.D]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY -= 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY += 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY += 1.0f; } } if (keyboardState[Key.A]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY += 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY -= 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY -= 1.0f; } } } else { if ((cameraBody != null) && steeringGearSwitch.IsColliding(cameraBody)) { if (keyboardState[Key.Space] && !oldKeyboardState[Key.Space]) { physicsObjectWithActiveCamera.Camera.EnableControl = true; cameraDown.RigidGroupOwner.MaxPreUpdateAngularVelocity = 1000.0f; cameraDown.RigidGroupOwner.MaxPostUpdateAngularVelocity = 1000.0f; cameraDown.DisableCollision(steeringWheel, true); cameraDown.DisableCollision(chassisBack, true); cameraDown.DisableCollision(chassisMiddleBack, true); cameraDown.DisableCollision(upBodyFront, true); cameraDown.DisableCollision(upBodyDownBack, true); cameraBody.DisableCollision(steeringGearDown, true); cameraBody.DisableCollision(steeringWheel, true); cameraBody.DisableCollision(chassisBack, true); cameraBody.DisableCollision(chassisMiddleBack, true); cameraBody.DisableCollision(upBodyFront, true); cameraBody.DisableCollision(upBodyDownBack, true); Quaternion cameraOrientationX = quaternionIdentity; Quaternion cameraOrientationY = quaternionIdentity; Quaternion cameraOrientationZ = quaternionIdentity; Quaternion cameraOrientationXY = quaternionIdentity; Quaternion cameraOrientation = quaternionIdentity; Quaternion.Multiply(ref cameraOrientationX, ref cameraOrientationY, out cameraOrientationXY); Quaternion.Multiply(ref cameraOrientationXY, ref cameraOrientationZ, out cameraOrientation); rotation = Matrix4.CreateFromQuaternion(cameraOrientation); physicsObjectWithActiveCamera.Camera.SetOrientation(ref cameraOrientation); physicsObjectWithActiveCamera.Camera.SetRotation(ref rotation); physicsObjectWithActiveCamera.Camera.SetEuler(ref rotation); objectOrientation = Quaternion.FromAxisAngle(unitY, MathHelper.DegreesToRadians(180.0f)); objectRotation = Matrix4.CreateFromQuaternion(objectOrientation); Vector3 carChassisPosition = vectorZero; Matrix4 carChassisRotation = matrixIdentity; chassisMiddleDown.MainWorldTransform.GetPosition(ref carChassisPosition); chassisMiddleDown.MainWorldTransform.GetRotation(ref carChassisRotation); Matrix4.Mult(ref objectRotation, ref carChassisRotation, out rotation); Vector3.TransformVector(ref driverLocalPosition, ref carChassisRotation, out position); Vector3.Add(ref carChassisPosition, ref position, out position); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetRotation(ref rotation); physicsObjectWithActiveCamera.RigidGroupOwner.MainWorldTransform.SetPosition(ref position); physicsObjectWithActiveCamera.RigidGroupOwner.RecalculateMainTransform(); if (CameraConstraint == null) { CameraConstraint = scene.Factory.ConstraintManager.Create(cameraConstraintName); CameraConstraint.PhysicsObject1 = cameraDown; CameraConstraint.PhysicsObject2 = chassisMiddleDown; CameraConstraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1); CameraConstraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1); CameraConstraint.PhysicsObject2.MainWorldTransform.GetOrientation(ref orientation2); carChassisPosition.X = carChassisPosition.Z = 0.0f; carChassisPosition.Y = 2.0f; Vector3.Subtract(ref position1, ref carChassisPosition, out position1); CameraConstraint.SetAnchor1(ref position1); CameraConstraint.SetAnchor2(ref position1); CameraConstraint.SetInitWorldOrientation1(ref orientation1); CameraConstraint.SetInitWorldOrientation2(ref orientation2); CameraConstraint.EnableLimitAngleX = true; CameraConstraint.EnableLimitAngleY = true; CameraConstraint.EnableLimitAngleZ = true; CameraConstraint.LimitAngleForce = 0.4f; CameraConstraint.MinResponseLinearVelocity = 0.005f; CameraConstraint.MinResponseAngularVelocity = 0.005f; CameraConstraint.Update(); } } } if (enableExternalMoving) { if (keyboardState[Key.Up]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, 5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.Down]) { if (wheel1 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel1.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } if (wheel2 != null) { Vector3.Multiply(ref unitY, -5.0f, out velocity); wheel2.MainWorldTransform.AddLocalAngularVelocity(ref velocity); } } if (keyboardState[Key.Right]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY -= 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY += 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY += 1.0f; } } if (keyboardState[Key.Left]) { if (steeringGearConstraint != null) { steeringGearConstraint.ControlDegAngleY += 2.0f; } if (constraint1 != null) { constraint1.ControlDegAngleY -= 1.0f; } if (constraint4 != null) { constraint4.ControlDegAngleY -= 1.0f; } } } } oldKeyboardState = keyboardState; }
void Awake() { startPosition = vacuum.transform.position; phyObj = vacuum.GetComponent <PhysicsObject>(); Reset(); }
public override void RemovePrim(PhysicsObject prim) { }
public override void DeletePrim(PhysicsObject prim) { }
/** * Helper method for getting the vector from PO to Obstacle **/ public Vector3 ToObstacle(PhysicsObject po) { return(transform.position - po.transform.position); }
private static bool IsPlatform(PhysicsObject o) { return(!o.IgnoresCollisionResponse); }
protected void Awake() { playerPhysics = GetComponent <PhysicsObject>(); player = GetComponent <Entity>(); jumpTimer = JumpTimeout; }
public void Create(Vector3 objectPosition, Vector3 objectScale, Quaternion objectOrientation, PhysicsObject joinPhysicsObject) { Shape sphere = scene.Factory.ShapeManager.Find("Sphere"); Shape cylinderY = scene.Factory.ShapeManager.Find("CylinderY"); Shape convex = scene.Factory.ShapeManager.Find("Lamp1Convex"); PhysicsObject objectRoot = null; PhysicsObject objectBase = null; PhysicsObject objectA = null; Vector3 position1 = Vector3.Zero; Quaternion orientation1 = Quaternion.Identity; objectRoot = scene.Factory.PhysicsObjectManager.Create("Lamp 1" + instanceIndexName); objectA = scene.Factory.PhysicsObjectManager.Create("Lamp 1 Body" + instanceIndexName); objectRoot.AddChildPhysicsObject(objectA); objectBase = scene.Factory.PhysicsObjectManager.Create("Lamp 1 Body Main" + instanceIndexName); objectA.AddChildPhysicsObject(objectBase); objectBase.Shape = convex; objectBase.UserDataStr = "Lamp1Convex"; objectBase.Material.UserDataStr = "Iron"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 400.0f; objectBase.InitLocalTransform.SetPosition(0.0f, 0.0f, 0.0f); objectBase.InitLocalTransform.SetScale(1.0f, 2.0f, 1.0f); objectBase.Integral.SetDensity(1.0f); objectBase.CreateSound(true); objectBase = scene.Factory.PhysicsObjectManager.Create("Lamp 1 Body Emitter" + instanceIndexName); objectA.AddChildPhysicsObject(objectBase); objectBase.Shape = cylinderY; objectBase.UserDataStr = "CylinderY"; objectBase.Material.UserDataStr = "Yellow"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 400.0f; objectBase.InitLocalTransform.SetPosition(0.0f, 2.2f, 0.0f); objectBase.InitLocalTransform.SetScale(0.1f, 0.2f, 0.1f); objectBase.Integral.InertiaScaleFactor = 3.0f; objectBase.Integral.SetDensity(1.0f); objectBase.MinResponseAngularVelocity = 0.05f; objectBase.MinResponseLinearVelocity = 0.05f; objectBase.CreateSound(true); objectBase = scene.Factory.PhysicsObjectManager.Create("Lamp 1 Body Light" + instanceIndexName); objectA.AddChildPhysicsObject(objectBase); objectBase.Shape = sphere; objectBase.UserDataStr = "Sphere"; objectBase.Material.RigidGroup = true; objectBase.Material.UserDataStr = "Yellow"; objectBase.InitLocalTransform.SetPosition(0.0f, 2.2f, 0.0f); objectBase.InitLocalTransform.SetScale(15.0f); objectBase.CreateLight(true); objectBase.Light.Type = PhysicsLightType.Point; objectBase.Light.SetDiffuse(1.0f, 0.8f, 0.1f); objectBase.Light.Range = 15.0f; objectBase.EnableBreakRigidGroup = false; objectBase.EnableCollisions = false; objectBase.EnableCursorInteraction = false; objectBase.EnableAddToCameraDrawTransparentPhysicsObjects = false; objectBase = scene.Factory.PhysicsObjectManager.Create("Lamp 1 Body Handle" + instanceIndexName); objectA.AddChildPhysicsObject(objectBase); objectBase.Shape = cylinderY; objectBase.UserDataStr = "CylinderY"; objectBase.Material.UserDataStr = "Iron"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 400.0f; objectBase.InitLocalTransform.SetPosition(-2.0f, 0.0f, 0.0f); objectBase.InitLocalTransform.SetOrientation(Quaternion.FromAxisAngle(Vector3.UnitZ, MathHelper.DegreesToRadians(90.0f))); objectBase.InitLocalTransform.SetScale(0.5f, 2.0f, 0.5f); objectBase.Integral.SetDensity(1.0f); objectBase.CreateSound(true); objectRoot.UpdateFromInitLocalTransform(); objectRoot.InitLocalTransform.SetOrientation(ref objectOrientation); objectRoot.InitLocalTransform.SetScale(ref objectScale); objectRoot.InitLocalTransform.SetPosition(ref objectPosition); scene.UpdateFromInitLocalTransform(objectRoot); Constraint constraint = null; constraint = scene.Factory.ConstraintManager.Create("Lamp 1 Constraint 1" + instanceIndexName); constraint.PhysicsObject1 = scene.Factory.PhysicsObjectManager.Find("Lamp 1 Body Handle" + instanceIndexName); constraint.PhysicsObject2 = joinPhysicsObject; constraint.PhysicsObject1.MainWorldTransform.GetPosition(ref position1); constraint.PhysicsObject1.MainWorldTransform.GetOrientation(ref orientation1); constraint.SetAnchor1(position1 + new Vector3(-2.0f, 0.0f, 0.0f)); constraint.SetAnchor2(position1 + new Vector3(-2.0f, 0.0f, 0.0f)); constraint.SetInitWorldOrientation1(orientation1); constraint.SetInitWorldOrientation2(orientation1); constraint.EnableLimitAngleX = true; constraint.EnableLimitAngleY = true; constraint.EnableLimitAngleZ = true; constraint.EnableBreak = true; constraint.Update(); }
private void Awake() { PhysicsObject = GetComponent <PhysicsObject>(); PhysicsObject.RegisterComponent(this); }
protected override void Awake() { base.Awake(); Player = GetComponent <Player>(); PhysicsObject = GetComponent <PhysicsObject>(); }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } float time = (float)args.Time; Vector3 deltaRotation = vectorZero; Vector3 deltaTranslation = vectorZero; float rotationSpeed = 8.0f; float translationSpeed = 8.0f; bool enableShot = false; DemoMouseState mouseState = demo.GetMouseState(); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (mouseState[MouseButton.Right]) { deltaRotation.Y += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.X - oldMouseState.X) * time); deltaRotation.X += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.Y - oldMouseState.Y) * time); } if (mouseState[MouseButton.Middle] && !oldMouseState[MouseButton.Middle]) { enableShot = true; } if ((keyboardState[Key.ControlRight] && !oldKeyboardState[Key.ControlRight]) || (keyboardState[Key.ControlLeft] && !oldKeyboardState[Key.ControlLeft])) { enableShot = true; } if (keyboardState[Key.W]) { deltaTranslation.Z += translationSpeed * time; } if (keyboardState[Key.S]) { deltaTranslation.Z -= translationSpeed * time; } if (keyboardState[Key.D]) { deltaTranslation.X += translationSpeed * time; } if (keyboardState[Key.A]) { deltaTranslation.X -= translationSpeed * time; } oldMouseState = mouseState; oldKeyboardState = keyboardState; if (deltaRotation.LengthSquared != 0.0f) { Vector3 euler = vectorZero; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.Camera.SetEuler(ref euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out cameraRotation); objectBase.Camera.SetRotation(ref cameraRotation); objectBase.MainWorldTransform.SetTransposeRotation(ref cameraRotation); objectBase.RecalculateMainTransform(); } if (deltaTranslation.LengthSquared != 0.0f) { objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetPosition(ref position); Vector3.Add(ref position, ref direction, out position); objectBase.MainWorldTransform.SetPosition(ref position); objectBase.RecalculateMainTransform(); } objectBase.Camera.Projection.CreatePerspectiveLH(1.0f, 11000.0f, 70.0f, demo.WindowWidth, demo.WindowHeight); objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); objectBase.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, 0.0f); objectBase.Camera.UpdateFrustum(); if (enableShot) { Vector3 shotScale, shotColor; string frameCountName = scene.SimulationFrameCount.ToString(); PhysicsObject shot = scene.Factory.PhysicsObjectManager.Create(shotName + frameCountName); PhysicsObject shotBase = scene.Factory.PhysicsObjectManager.Create(shotBaseName + frameCountName); PhysicsObject shotLight = scene.Factory.PhysicsObjectManager.Create(shotLightName + frameCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); shotScale = shotColor = vectorZero; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); direction.X = cameraRotation.Row2.X; direction.Y = cameraRotation.Row2.Y; direction.Z = cameraRotation.Row2.Z; Vector3.Multiply(ref direction, 300.0f, out direction); shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref position); shot.InitLocalTransform.SetLinearVelocity(ref direction); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 200; //shot.EnableLocalGravity = true; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(10.0f); shotBase.Material.RigidGroup = true; shotBase.EnableBreakRigidGroup = false; shotBase.EnableCollisions = true; shotBase.DisableCollision(objectBase, true); shotBase.MaxDisableCollisionFrameCount = 50; shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotColor.X = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Y = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Z = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotLight.Light.SetDiffuse(ref shotColor); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.RigidGroup = true; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); } objectBase.Camera.UpdatePhysicsObjects(true, true, true); objectBase.Camera.SortDrawPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortTransparentPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); }
public void Create(Vector3 objectPosition, Vector3 objectScale, Quaternion objectOrientation, int maxPlank, Vector3 plankScale, float plankDistance) { Shape box = scene.Factory.ShapeManager.Find("Box"); PhysicsObject objectRoot = null; PhysicsObject objectBase = null; objectRoot = scene.Factory.PhysicsObjectManager.Create("Box 1" + instanceIndexName); for (int i = 0; i < maxPlank; i++) { objectBase = scene.Factory.PhysicsObjectManager.Create("Box 1 Plank Up " + i.ToString() + instanceIndexName); objectRoot.AddChildPhysicsObject(objectBase); objectBase.Shape = box; objectBase.UserDataStr = "Box"; objectBase.Material.UserDataStr = "Wood1"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 50.0f; objectBase.Material.SetSpecular(0.1f, 0.1f, 0.1f); objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(i * 2.0f * (plankScale.X + plankDistance) - maxPlank * (plankScale.X + plankDistance) + (plankScale.X + plankDistance), 0.0f, maxPlank * (plankScale.X + plankDistance) + plankScale.Z - plankDistance); objectBase.InitLocalTransform.SetScale(plankScale); objectBase.Integral.SetDensity(1.0f); } for (int i = 0; i < maxPlank; i++) { objectBase = scene.Factory.PhysicsObjectManager.Create("Box 1 Plank Down " + i.ToString() + instanceIndexName); objectRoot.AddChildPhysicsObject(objectBase); objectBase.Shape = box; objectBase.UserDataStr = "Box"; objectBase.Material.UserDataStr = "Wood1"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 50.0f; objectBase.Material.SetSpecular(0.1f, 0.1f, 0.1f); objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(i * 2.0f * (plankScale.X + plankDistance) - maxPlank * (plankScale.X + plankDistance) + (plankScale.X + plankDistance), 0.0f, -maxPlank * (plankScale.X + plankDistance) - plankScale.Z + plankDistance); objectBase.InitLocalTransform.SetScale(plankScale); objectBase.Integral.SetDensity(1.0f); } for (int i = 0; i < maxPlank; i++) { objectBase = scene.Factory.PhysicsObjectManager.Create("Box 1 Plank Right " + i.ToString() + instanceIndexName); objectRoot.AddChildPhysicsObject(objectBase); objectBase.Shape = box; objectBase.UserDataStr = "Box"; objectBase.Material.UserDataStr = "Wood1"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 50.0f; objectBase.Material.SetSpecular(0.1f, 0.1f, 0.1f); objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(maxPlank * (plankScale.X + plankDistance) + plankScale.Z - plankDistance, 0.0f, i * 2.0f * (plankScale.X + plankDistance) - maxPlank * (plankScale.X + plankDistance) + (plankScale.X + plankDistance)); objectBase.InitLocalTransform.SetScale(plankScale.Z, plankScale.Y, plankScale.X); objectBase.Integral.SetDensity(1.0f); } for (int i = 0; i < maxPlank; i++) { objectBase = scene.Factory.PhysicsObjectManager.Create("Box 1 Plank Left " + i.ToString() + instanceIndexName); objectRoot.AddChildPhysicsObject(objectBase); objectBase.Shape = box; objectBase.UserDataStr = "Box"; objectBase.Material.UserDataStr = "Wood1"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 50.0f; objectBase.Material.SetSpecular(0.1f, 0.1f, 0.1f); objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(-maxPlank * (plankScale.X + plankDistance) - plankScale.Z + plankDistance, 0.0f, i * 2.0f * (plankScale.X + plankDistance) - maxPlank * (plankScale.X + plankDistance) + (plankScale.X + plankDistance)); objectBase.InitLocalTransform.SetScale(plankScale.Z, plankScale.Y, plankScale.X); objectBase.Integral.SetDensity(1.0f); } for (int i = 0; i < maxPlank; i++) { objectBase = scene.Factory.PhysicsObjectManager.Create("Box 1 Plank Top " + i.ToString() + instanceIndexName); objectRoot.AddChildPhysicsObject(objectBase); objectBase.Shape = box; objectBase.UserDataStr = "Box"; objectBase.Material.UserDataStr = "Wood1"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 50.0f; objectBase.Material.SetSpecular(0.1f, 0.1f, 0.1f); objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(i * 2.0f * (plankScale.X + plankDistance) - maxPlank * (plankScale.X + plankDistance) + (plankScale.X + plankDistance), plankScale.Y + plankScale.Z - plankDistance, 0.0f); objectBase.InitLocalTransform.SetScale(plankScale.X, plankScale.Z, maxPlank * (plankScale.X + plankDistance) - plankDistance); objectBase.Integral.SetDensity(1.0f); } for (int i = 0; i < maxPlank; i++) { objectBase = scene.Factory.PhysicsObjectManager.Create("Box 1 Plank Bottom " + i.ToString() + instanceIndexName); objectRoot.AddChildPhysicsObject(objectBase); objectBase.Shape = box; objectBase.UserDataStr = "Box"; objectBase.Material.UserDataStr = "Wood1"; objectBase.Material.RigidGroup = true; objectBase.Material.MinBreakRigidGroupVelocity = 50.0f; objectBase.Material.SetSpecular(0.1f, 0.1f, 0.1f); objectBase.CreateSound(true); objectBase.InitLocalTransform.SetPosition(i * 2.0f * (plankScale.X + plankDistance) - maxPlank * (plankScale.X + plankDistance) + (plankScale.X + plankDistance), -plankScale.Y - plankScale.Z + plankDistance, 0.0f); objectBase.InitLocalTransform.SetScale(plankScale.X, plankScale.Z, maxPlank * (plankScale.X + plankDistance) - plankDistance); objectBase.Integral.SetDensity(1.0f); } objectRoot.InitLocalTransform.SetOrientation(ref objectOrientation); objectRoot.InitLocalTransform.SetScale(ref objectScale); objectRoot.InitLocalTransform.SetPosition(ref objectPosition); scene.UpdateFromInitLocalTransform(objectRoot); }
public void Move(SimulateMethodArgs args) { PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex); PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex); if (!objectBase.Camera.Enabled) { return; } if (!objectBase.Camera.Active) { return; } float time = (float)args.Time; Vector3 deltaRotation = vectorZero; Vector3 deltaTranslation = vectorZero; float rotationSpeed = 8.0f; float translationSpeed = 8.0f; float soundPositionFactor = 0.1f; bool enableShot = false; DemoMouseState mouseState = demo.GetMouseState(); DemoKeyboardState keyboardState = demo.GetKeyboardState(); if (mouseState[MouseButton.Right]) { deltaRotation.Y += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.X - oldMouseState.X) * time); deltaRotation.X += MathHelper.DegreesToRadians(rotationSpeed * (mouseState.Y - oldMouseState.Y) * time); } mousePosition.X = mouseState.X; mousePosition.Y = mouseState.Y; if (mouseState[MouseButton.Middle] && !oldMouseState[MouseButton.Middle]) { enableShot = true; } if ((keyboardState[Key.ControlRight] && !oldKeyboardState[Key.ControlRight]) || (keyboardState[Key.ControlLeft] && !oldKeyboardState[Key.ControlLeft])) { enableShot = true; } PhysicsObject cursorA = scene.Factory.PhysicsObjectManager.Find(cursorAName); PhysicsObject cursorB = scene.Factory.PhysicsObjectManager.Find(cursorBName); if (!demo.EnableMenu) { if (cursorA != null) { cursorA.EnableDrawing = true; } if (cursorB != null) { cursorB.EnableDrawing = true; } } else { if (cursorA != null) { cursorA.EnableDrawing = false; } if (cursorB != null) { cursorB.EnableDrawing = false; } } if (keyboardState[Key.W]) { deltaTranslation.Z += translationSpeed * time; } if (keyboardState[Key.S]) { deltaTranslation.Z -= translationSpeed * time; } if (keyboardState[Key.D]) { deltaTranslation.X += translationSpeed * time; } if (keyboardState[Key.A]) { deltaTranslation.X -= translationSpeed * time; } oldMouseState = mouseState; oldKeyboardState = keyboardState; if (deltaRotation.LengthSquared != 0.0f) { Vector3 euler = vectorZero; objectBase.Camera.GetEuler(ref euler); Vector3.Add(ref euler, ref deltaRotation, out euler); objectBase.Camera.SetEuler(ref euler); Matrix4 rotationX, rotationY; Matrix4.CreateRotationX(-euler.X, out rotationX); Matrix4.CreateRotationY(-euler.Y, out rotationY); Matrix4.Mult(ref rotationY, ref rotationX, out cameraRotation); objectBase.Camera.SetRotation(ref cameraRotation); objectBase.MainWorldTransform.SetTransposeRotation(ref cameraRotation); objectBase.RecalculateMainTransform(); } if (deltaTranslation.LengthSquared != 0.0f) { objectBase.MainWorldTransform.GetRotation(ref rotation); Vector3.TransformVector(ref deltaTranslation, ref rotation, out direction); objectBase.MainWorldTransform.GetPosition(ref position); Vector3.Add(ref position, ref direction, out position); objectBase.MainWorldTransform.SetPosition(ref position); objectBase.RecalculateMainTransform(); } objectBase.Camera.Projection.CreatePerspectiveLH(1.0f, 11000.0f, 70.0f, demo.WindowWidth, demo.WindowHeight); objectBase.MainWorldTransform.GetPosition(ref position); objectBase.Camera.GetTransposeRotation(ref cameraRotation); objectBase.Camera.View.CreateLookAtLH(ref position, ref cameraRotation, 0.0f); objectBase.Camera.UpdateFrustum(); objectBase.Camera.View.GetViewMatrix(ref view); objectBase.Camera.Projection.GetProjectionMatrix(ref projection); Vector3 rayPosition, rayDirection; rayPosition = rayDirection = vectorZero; objectBase.UnProjectToRay(ref mousePosition, 0, 0, demo.WindowWidth, demo.WindowHeight, 0.0f, 1.0f, ref view, ref matrixIdentity, ref projection, ref rayPosition, ref rayDirection); PhysicsObject cursor = scene.Factory.PhysicsObjectManager.Find(cursorName); if (cursor != null) { Vector3 cursorPosition = vectorZero; Matrix4 cursorLocalRotation = matrixIdentity; Matrix4 cursorWorldRotation = matrixIdentity; cursor.InitLocalTransform.GetPosition(ref cursorPosition); cursor.InitLocalTransform.GetRotation(ref cursorLocalRotation); cursor.MainWorldTransform.GetRotation(ref cursorWorldRotation); objectBase.Camera.GetTransposeRotation(ref cameraRotation); Matrix4.Mult(ref cursorLocalRotation, ref cameraRotation, out rotation); Vector3.TransformVector(ref cursorPosition, ref cursorWorldRotation, out position); cursor.MainWorldTransform.SetRotation(ref rotation); Vector3.Add(ref position, ref rayPosition, out position); Vector3.Add(ref position, ref rayDirection, out position); cursor.MainWorldTransform.SetPosition(ref position); cursor.RecalculateMainTransform(); } CursorController cursorController = objectBase.InternalControllers.CursorController; if (cursorController.IsDragging) { if (cursorController.HitPhysicsObject.Integral.IsStatic && (cursorController.HitPhysicsObject.Shape != null) && (cursorController.HitPhysicsObject.Shape.ShapePrimitive != null) && (cursorController.HitPhysicsObject.Shape.ShapePrimitive.ShapePrimitiveType == ShapePrimitiveType.TriangleMesh)) { Vector3 cursorStartPosition = vectorZero; Vector3 cursorEndPosition = vectorZero; cursorController.GetAnchor1(ref cursorStartPosition); cursorController.GetAnchor2(ref cursorEndPosition); Vector3.Subtract(ref cursorEndPosition, ref cursorStartPosition, out direction); if (direction.LengthSquared != 0.0f) { cursorController.HitPhysicsObject.MainWorldTransform.GetPosition(ref cursorStartPosition); Vector3.Add(ref cursorStartPosition, ref direction, out cursorStartPosition); cursorController.HitPhysicsObject.MainWorldTransform.SetPosition(ref cursorStartPosition); cursorController.HitPhysicsObject.RecalculateMainTransform(); } } } objectBase.MainWorldTransform.GetPosition(ref listenerPosition); objectBase.Camera.GetTransposeRotation(ref rotation); Vector3.Multiply(ref listenerPosition, soundPositionFactor, out position); listenerTopDirection.X = rotation.Row1.X; listenerTopDirection.Y = rotation.Row1.Y; listenerTopDirection.Z = rotation.Row1.Z; listenerFrontDirection.X = rotation.Row2.X; listenerFrontDirection.Y = rotation.Row2.Y; listenerFrontDirection.Z = rotation.Row2.Z; listener.Position = position; listener.TopDirection = listenerTopDirection; listener.FrontDirection = listenerFrontDirection; listenerRange = objectBase.Sound.Range; if (enableShot) { Vector3 shotScale, shotColor; shotCount = (shotCount + 1) % shotTab.Length; string shotCountName = shotCount.ToString(); PhysicsObject shot = scene.Factory.PhysicsObjectManager.FindOrCreate(shotName + shotCountName); PhysicsObject shotBase = scene.Factory.PhysicsObjectManager.FindOrCreate(shotBaseName + shotCountName); PhysicsObject shotLight = scene.Factory.PhysicsObjectManager.FindOrCreate(shotLightName + shotCountName); shot.AddChildPhysicsObject(shotBase); shot.AddChildPhysicsObject(shotLight); shotTab[shotCount] = shotBase; enableShotTab = true; shotScale = shotColor = vectorZero; shotScale.X = shotScale.Y = shotScale.Z = 0.5f; Vector3.Multiply(ref rayDirection, 300.0f, out rayDirection); shot.InitLocalTransform.SetRotation(ref matrixIdentity); shot.InitLocalTransform.SetPosition(ref rayPosition); shot.InitLocalTransform.SetLinearVelocity(ref rayDirection); shot.InitLocalTransform.SetAngularVelocity(ref vectorZero); shot.MaxSimulationFrameCount = 200; shot.EnableRemovePhysicsObjectsFromManagerAfterMaxSimulationFrameCount = false; //shot.EnableLocalGravity = true; shotBase.Shape = sphere; shotBase.UserDataStr = sphereName; shotBase.InitLocalTransform.SetScale(ref shotScale); shotBase.Integral.SetDensity(10.0f); shotBase.Material.RigidGroup = true; shotBase.EnableBreakRigidGroup = false; shotBase.EnableCollisions = true; shotBase.DisableCollision(objectBase, true); shotBase.MaxDisableCollisionFrameCount = 50; shotBase.CreateSound(true); shotLight.Shape = sphere; shotLight.UserDataStr = sphereName; shotLight.CreateLight(true); shotLight.Light.Type = PhysicsLightType.Point; shotLight.Light.Range = 20.0f; shotColor.X = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Y = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotColor.Z = (float)Math.Max(random.NextDouble(), random.NextDouble()); shotLight.Light.SetDiffuse(ref shotColor); shotLight.InitLocalTransform.SetScale(20.0f); shotLight.Material.UserDataStr = yellowName; shotLight.Material.RigidGroup = true; shotLight.EnableBreakRigidGroup = false; shotLight.EnableCollisions = false; shotLight.EnableCursorInteraction = false; shotLight.EnableAddToCameraDrawTransparentPhysicsObjects = false; scene.UpdateFromInitLocalTransform(shot); shotSoundGroup = demo.SoundGroups[hitName]; shotSoundGroup.MaxHitRepeatTime = 0.0f; if (shotSound == null) { shotSound = shotSoundGroup.GetSound(objectBase.Sound, listener, emitter); } shotSound.Update(time); Vector3.Multiply(ref rayPosition, soundPositionFactor, out shotSound.HitPosition); shotSound.HitVolume = 1.0f; shotSound.FrontDirection.X = rotation.Row2.X; shotSound.FrontDirection.Y = rotation.Row2.Y; shotSound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(shotSound); } else { PhysicsObject shotBase; DemoSound shotBaseSound; if (shotSound != null) { shotSound.Update(time); if (shotSound.Stop()) { shotSound.SoundGroup.SetSound(shotSound); shotSound = null; } } if (enableShotTab) { enableShotTab = false; for (int i = 0; i < shotTab.Length; i++) { shotBase = shotTab[i]; if (shotBase != null) { if (shotBase.Sound.UserDataObj != null) { enableShotTab = true; shotBaseSound = (DemoSound)shotBase.Sound.UserDataObj; shotBaseSound.Update(time); if (shotBaseSound.Stop()) { shotBaseSound.SoundGroup.SetSound(shotBaseSound); shotBase.Sound.UserDataObj = null; shotTab[i] = null; } } } } } } objectBase.Camera.UpdatePhysicsObjects(true, true, true); objectBase.Camera.SortDrawPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortTransparentPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); objectBase.Camera.SortLightPhysicsObjects(PhysicsCameraSortOrderType.DrawPriorityShapePrimitiveType); PhysicsObject currentPhysicsObject; PhysicsSound currentSound; DemoSoundGroup soundGroup; DemoSound sound; if (demo.SoundQueue.SoundCount > 0) { return; } for (int i = 0; i < scene.TotalPhysicsObjectCount; i++) { if (demo.SoundQueue.SoundCount >= demo.SoundQueue.MaxSoundCount) { break; } currentPhysicsObject = scene.GetPhysicsObject(i); currentSound = !currentPhysicsObject.EnableSoundFromRigidGroupOwner ? currentPhysicsObject.Sound : currentPhysicsObject.RigidGroupOwner.Sound; if (currentSound == null) { continue; } if (!currentSound.Enabled) { continue; } if (currentSound.UserDataStr == null) { soundGroup = demo.SoundGroups[defaultName]; } else { soundGroup = demo.SoundGroups[currentSound.UserDataStr]; } if (currentPhysicsObject.IsSleeping && (currentSound.UserDataObj == null) && !soundGroup.EnableBackground) { continue; } if (currentPhysicsObject.GetSoundData(ref listenerPosition, listenerRange, soundGroup.EnableHit, soundGroup.EnableRoll, soundGroup.EnableSlide, soundGroup.EnableBackground, currentSound.BackgroundVolumeVelocityModulation, ref hitPosition, ref rollPosition, ref slidePosition, ref backgroundPosition, ref hitVolume, ref rollVolume, ref slideVolume, ref backgroundVolume)) { if (currentSound.UserDataObj == null) { sound = soundGroup.GetSound(currentSound, listener, emitter); currentSound.UserDataObj = sound; } else { sound = (DemoSound)currentSound.UserDataObj; } sound.Update(time); currentPhysicsObject.MainWorldTransform.GetTransposeRotation(ref rotation); Vector3.Multiply(ref hitPosition, soundPositionFactor, out sound.HitPosition); Vector3.Multiply(ref rollPosition, soundPositionFactor, out sound.RollPosition); Vector3.Multiply(ref slidePosition, soundPositionFactor, out sound.SlidePosition); Vector3.Multiply(ref backgroundPosition, soundPositionFactor, out sound.BackgroundPosition); sound.HitVolume = hitVolume; sound.RollVolume = rollVolume; sound.SlideVolume = slideVolume; sound.BackgroundVolume = backgroundVolume; sound.FrontDirection.X = rotation.Row2.X; sound.FrontDirection.Y = rotation.Row2.Y; sound.FrontDirection.Z = rotation.Row2.Z; demo.SoundQueue.EnqueueSound(sound); } else { if (currentSound.UserDataObj != null) { sound = (DemoSound)currentSound.UserDataObj; sound.Update(time); if (sound.Stop()) { soundGroup.SetSound(sound); currentSound.UserDataObj = null; } } } } }
public DTCrush(PhysicsObject t) : base((Thing)t) => this._thing = t;
//Implementoin pyörivät tornit jos ehdin void Pyorita(PhysicsObject pelaaja, int torniNopeus) { pelaaja.AngularVelocity = torniNopeus; pelaaja.LinearDamping = 0.8; }
private void TormaaTahteen(PhysicsObject hahmo, PhysicsObject tahti) { maaliAani.Play(); MessageDisplay.Add("Keräsit tähden!"); tahti.Destroy(); }
Vector3[] PredictOrbit(PhysicsObject _strongestObject, uint steps) { try { Vector3[] positions = new Vector3[steps]; PhysicsObjectPair pair = new PhysicsObjectPair(); pair.O1 = this; pair.O2 = _strongestObject; // Calculate Orbital Period for this //float period = (2.0f * Mathf.PI) * Mathf.Sqrt( Mathf.Pow(Vector3.Distance(pair.O1.transform.position, pair.O2.transform.position),3.0f) / G * ( pair.O1.rb.mass + pair.O2.rb.mass) ); Vector3 position1 = this.transform.position; Vector3 position2 = _strongestObject.transform.position; Vector3 velocity1 = this.rb.velocity - _strongestObject.rb.velocity; //Vector3 velocity2 = _strongestObject.rb.velocity; //Vector3 velocity2 = -velocity1; Vector3 velocity2 = Vector3.zero; //Vector3 velocity2 = _strongestObject.rb.velocity - this.rb.velocity; float mass1 = this.rb.mass; float mass2 = _strongestObject.rb.mass; // Note, this is flawed as it assumes circular orbit float period = (Mathf.PI * 2.0f) * Vector3.Distance(pair.O1.transform.position, pair.O2.transform.position) / velocity1.magnitude; float timestep = period / steps; for (int i = 0; i < steps; i++) { positions[i] = position1; // Calculate force Vector3 force = new Vector3(); //Obtain Direction Vector Vector3 dir = position1 - position2; //Obtain Distance, return if 0 float dist = dir.magnitude; if (dist == 0) { force = Vector3.zero; } //Calculate Magnitude of force float magnitude = PhysicsEngine.G * (mass1 * mass2) / Mathf.Pow(dist, 2); force = dir.normalized * magnitude; // Calculate accelerations Vector3 a1 = -force / mass1; Vector3 a2 = force / mass2; // Update positions position1 += velocity1 * timestep + 0.5f * a1 * timestep * timestep; position2 += velocity2 * timestep + 0.5f * a2 * timestep * timestep; // Update velocities velocity1 += a1 * timestep; velocity2 += a2 * timestep; } return(positions); } catch { lineRenderer.positionCount = 0; return(default);
void Start() { po = GetComponent <PhysicsObject>(); anim = GetComponent <Animator>(); sprite = GetComponent <SpriteRenderer>(); }
/// <summary> /// Initializes an instance of this class, along with the values for /// using it. /// </summary> /// <param name="parent"></param> /// <param name="model"></param> /// <param name="blurStackSize"></param> public FlyingObject(GameWindow parent, OBJModel model, int blurStackSize) { Parent = parent; Lifespan = 50; //blur BlurStackSize = blurStackSize; BlurStack = new LinkedList<Vector>(); EnableMotionBlur = true; alphaStep = .5f / BlurStackSize; PhysicalBody = new PhysicsObject(); PhysicalBody.ParentObject = this; PhysicalBody.Velocity = new Vector(GeneralMath.RandomFloat(-0.7f, 0.7f), GeneralMath.RandomFloat(1.5f, 2.5f), GeneralMath.RandomFloat(-0.5f, 0.5f)); angleIncrement = GeneralMath.RandomFloat(5.5f, 6.5f); Body = model; Location = new Vector(0.0f, 0.0f, 0.0f); MainTimer = new Timer(); MainTimer.Interval = 10; MainTimer.Tick += AnimationStep; }