12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273 |
- /*
- * Copyright (c) Contributors, http://opensimulator.org/
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the OpenSimulator Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- // Revision by Ubit 2011/12
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using OpenMetaverse;
- using OpenSim.Framework;
- using OpenSim.Region.PhysicsModules.SharedBase;
- using log4net;
- namespace OpenSim.Region.PhysicsModule.ubOde
- {
- /// <summary>
- /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
- /// </summary>
- public enum dParam : int
- {
- LowStop = 0,
- HiStop = 1,
- Vel = 2,
- FMax = 3,
- FudgeFactor = 4,
- Bounce = 5,
- CFM = 6,
- StopERP = 7,
- StopCFM = 8,
- LoStop2 = 256,
- HiStop2 = 257,
- Vel2 = 258,
- FMax2 = 259,
- StopERP2 = 7 + 256,
- StopCFM2 = 8 + 256,
- LoStop3 = 512,
- HiStop3 = 513,
- Vel3 = 514,
- FMax3 = 515,
- StopERP3 = 7 + 512,
- StopCFM3 = 8 + 512
- }
- public class OdeCharacter : PhysicsActor
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- internal AABB2D _AABB2D;
- internal Vector3 _position;
- public int Colliderfilter = 0;
- public float CapsuleSizeZ;
- public float CapsuleRadius;
- internal Vector2 Orientation2D;
- internal float AvaSizeXsq = 0.3f;
- internal float AvaSizeYsq = 0.2f;
- internal float feetOff = 0;
- internal float boneOff = 0;
- public IntPtr Body = IntPtr.Zero;
- public int m_bodydisablecontrol = 0;
- private float m_scenegravityForceZ;
- int m_colliderObjectfilter = 0;
- int m_colliderGroundfilter = 0;
- private UBOdeNative.Quaternion m_NativeOrientation2D;
- private Vector3 _zeroPosition;
- internal Quaternion m_orientation;
- private Vector3 _velocity;
- private Vector3 m_rotationalVelocity;
- private Vector3 _acceleration;
- private Vector3 m_lastFallVel;
- private Vector3 m_size;
- private float m_mass = 80f;
- private float m_massInvTimeScaled = 1600f;
- public readonly float m_density = 60f;
- private bool m_pidControllerActive = true;
- internal Vector3 CollideNormal;
- public int _charsListIndex;
- const float basePID_D = 0.55f; // scaled for unit mass unit time (2200 /(50*80))
- const float basePID_P = 0.225f; // scaled for unit mass unit time (900 /(50*80))
- public float PID_D;
- public float PID_P;
- private readonly ODEScene m_parent_scene;
- private readonly float m_sceneTimeStep;
- private readonly float m_sceneInverseTimeStep;
- public readonly float m_walkMultiplier = 1.0f / 1.3f;
- public readonly float m_runMultiplier = 1.0f / 0.8f;
- private bool m_flying = false;
- private bool m_iscolliding = false;
- private bool m_iscollidingGround = false;
- private bool m_iscollidingObj = false;
- private bool _zeroFlag = false;
- private bool m_haveLastFallVel = false;
- public bool m_returnCollisions = false;
- // taints and their non-tainted counterparts
- public bool m_isPhysical = false; // the current physical status
- public float MinimumGroundFlightOffset = 3f;
- private bool m_freemove = false;
- // private string m_name = String.Empty;
- // other filter control
- // Default we're a Character
- private const CollisionCategories m_collisionCategories = (CollisionCategories.Character);
- // Default, Collide with Other Geometries, spaces, bodies and characters.
- private const CollisionCategories m_collisionFlags = (CollisionCategories.Character
- | CollisionCategories.Geom
- | CollisionCategories.VolumeDtc
- );
- // we do land collisions not ode | CollisionCategories.Land);
- //private IntPtr capsule = IntPtr.Zero;
- public IntPtr collider = IntPtr.Zero;
- public IntPtr Amotor = IntPtr.Zero;
- internal UBOdeNative.Mass ShellMass;
- public int m_eventsubscription = 0;
- private int m_cureventsubscription = 0;
- private readonly CollisionEventUpdate CollisionEventsThisFrame = new();
- private bool SentEmptyCollisionsEvent;
- public bool bad = false;
- private readonly float m_frictionmu;
- // HoverHeight control
- private float m_PIDHoverHeight;
- private float m_PIDHoverTau;
- private bool m_useHoverPID;
- private PIDHoverType m_PIDHoverType;
- private float m_targetHoverHeight;
- public OdeCharacter(uint localID, String avName, ODEScene parent_scene, Vector3 pos, Vector3 pSize, float pfeetOffset, float density, float walk_divisor, float rundivisor)
- {
- m_baseLocalID = localID;
- m_parent_scene = parent_scene;
- m_sceneTimeStep = parent_scene.ODE_STEPSIZE;
- m_sceneInverseTimeStep = 1.0f / m_sceneTimeStep;
- if (pos.IsFinite())
- {
- if (pos.Z > Constants.MaxSimulationHeight)
- {
- pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
- }
- if (pos.Z < Constants.MinSimulationHeight) // shouldn't this be 0 ?
- {
- pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
- }
- _position = pos;
- }
- else
- {
- _position = new Vector3(((float)m_parent_scene.WorldExtents.X * 0.5f), ((float)m_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f);
- m_log.Warn("[PHYSICS]: Got NaN Position on Character Create");
- }
- m_size.X = pSize.X > 0.01f ? 0.5f * pSize.X : 0.01f;
- m_size.Y = pSize.Y > 0.01f ? 0.5f * pSize.Y : 0.01f;
- m_size.Z = pSize.Z > 0.01f ? 0.5f * pSize.Z : 0.01f;
- CapsuleRadius = MathF.Max(m_size.X, m_size.Y);
- CapsuleSizeZ = m_size.Z;
- AvaSizeXsq = m_size.X;
- AvaSizeXsq *= AvaSizeXsq;
- AvaSizeYsq = m_size.Y;
- AvaSizeYsq *= AvaSizeYsq;
- m_orientation = Quaternion.Identity;
- Orientation2D = new(0f, 1f);
- m_NativeOrientation2D.X = 0f;
- m_NativeOrientation2D.Y = 0f;
- m_NativeOrientation2D.Z = 0f;
- m_NativeOrientation2D.W = 1f;
- m_density = density;
- // force lower density for testing
- m_density = 3.0f;
- m_frictionmu = m_parent_scene.AvatarFriction;
- m_walkMultiplier = 1.0f / walk_divisor;
- m_runMultiplier = 1.0f / rundivisor;
- m_mass = 8f * m_density * m_size.X * m_size.Y * m_size.Z;
- m_massInvTimeScaled = m_mass * m_sceneInverseTimeStep;
- // sure we have a default
- PID_D = basePID_D * m_massInvTimeScaled;
- PID_P = basePID_P * m_massInvTimeScaled;
- m_scenegravityForceZ = parent_scene.gravityz * m_mass;
- m_isPhysical = false; // current status: no ODE information exists
- Name = avName;
- UpdateAABB2D();
- AddChange(changes.Add, null);
- }
- public override int PhysicsActorType
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return (int)ActorTypes.Agent;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- return;
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void getContactData(ref ContactData cdata)
- {
- cdata.mu = m_frictionmu;
- cdata.bounce = 0;
- cdata.softcolide = false;
- }
- public override bool Building
- {
- get; set;
- }
- /// <summary>
- /// If this is set, the avatar will move faster
- /// </summary>
- private bool m_alwaysRun = false;
- public override bool SetAlwaysRun
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_alwaysRun;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- m_alwaysRun = value;
- }
- }
- public override PhysicsActor ParentActor
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return (PhysicsActor)this;
- }
- }
- public override bool Grabbed
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- return;
- }
- }
- public override bool Selected
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- return;
- }
- }
- private float m_buoyancy = 0f;
- public override float Buoyancy
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_buoyancy;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- m_buoyancy = value;
- }
- }
- public override bool IsPhysical
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_isPhysical;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- return;
- }
- }
- public override bool ThrottleUpdates
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- return;
- }
- }
- public override bool Flying
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_flying;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- m_flying = value;
- //m_log.DebugFormat("[PHYSICS]: Set OdeCharacter Flying to {0}", flying);
- }
- }
- /// <summary>
- /// Returns if the avatar is colliding in general.
- /// This includes the ground and objects and avatar.
- /// </summary>
- public override bool IsColliding
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return (m_iscolliding || m_iscollidingGround);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- if (value)
- {
- Colliderfilter += 3;
- if (Colliderfilter > 9)
- Colliderfilter = 9;
- }
- else
- {
- Colliderfilter--;
- if (Colliderfilter < 0)
- Colliderfilter = 0;
- }
- if (Colliderfilter < 6)
- m_iscolliding = false;
- else
- {
- m_pidControllerActive = true;
- m_iscolliding = true;
- m_freemove = false;
- }
- }
- }
- /// <summary>
- /// Returns if an avatar is colliding with the ground
- /// </summary>
- public override bool CollidingGround
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_iscollidingGround;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- /* we now control this
- if (value)
- {
- m_colliderGroundfilter += 2;
- if (m_colliderGroundfilter > 2)
- m_colliderGroundfilter = 2;
- }
- else
- {
- m_colliderGroundfilter--;
- if (m_colliderGroundfilter < 0)
- m_colliderGroundfilter = 0;
- }
- if (m_colliderGroundfilter == 0)
- m_iscollidingGround = false;
- else
- m_iscollidingGround = true;
- */
- }
- }
- /// <summary>
- /// Returns if the avatar is colliding with an object
- /// </summary>
- public override bool CollidingObj
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_iscollidingObj;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- if (value)
- {
- m_colliderObjectfilter += 2;
- if (m_colliderObjectfilter > 2)
- m_colliderObjectfilter = 2;
- }
- else
- {
- m_colliderObjectfilter--;
- if (m_colliderObjectfilter < 0)
- m_colliderObjectfilter = 0;
- }
- if (m_colliderObjectfilter == 0)
- m_iscollidingObj = false;
- else
- m_iscollidingObj = true;
- // m_iscollidingObj = value;
- if (m_iscollidingObj)
- m_pidControllerActive = false;
- else
- m_pidControllerActive = true;
- }
- }
- /// <summary>
- /// turn the PID controller on or off.
- /// The PID Controller will turn on all by itself in many situations
- /// </summary>
- /// <param name="status"></param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void SetPidStatus(bool status)
- {
- m_pidControllerActive = status;
- }
- public override bool Stopped
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return _zeroFlag;
- }
- }
- /// <summary>
- /// This 'puts' an avatar somewhere in the physics space.
- /// Not really a good choice unless you 'know' it's a good
- /// spot otherwise you're likely to orbit the avatar.
- /// </summary>
- public override Vector3 Position
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return _position;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- if (value.IsFinite())
- {
- if (value.Z < -100f || value.Z > 9999999f)
- value.Z = m_parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
- AddChange(changes.Position, value);
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character");
- }
- }
- }
- public override Vector3 RotationalVelocity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_rotationalVelocity;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- m_rotationalVelocity = value;
- }
- }
- /// <summary>
- /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
- /// and use it to offset landings properly
- /// </summary>
- public override Vector3 Size
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_size * 2f;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- if (value.IsFinite())
- {
- if (value.X < 0.01f)
- value.X = 0.01f;
- if (value.Y < 0.01f)
- value.Y = 0.01f;
- if (value.Z < 0.01f)
- value.Z = 0.01f;
- AddChange(changes.Size, value);
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
- }
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void setAvatarSize(Vector3 size, float feetOffset)
- {
- if (size.IsFinite())
- {
- if (size.X < 0.01f)
- size.X = 0.01f;
- if (size.Y < 0.01f)
- size.Y = 0.01f;
- if (size.Z < 0.01f)
- size.Z = 0.01f;
- strAvatarSize st = new()
- {
- size = size,
- };
- AddChange(changes.AvatarSize, st);
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN AvatarSize from Scene on a Character");
- }
- }
- /// <summary>
- /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
- /// This may be used in calculations in the scene/scenepresence
- /// </summary>
- public override float Mass
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_mass;
- }
- }
- public override void link(PhysicsActor obj)
- {
- }
- public override void delink()
- {
- }
- public override void LockAngularMotion(byte axislocks)
- {
- }
- public override Vector3 Force
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_targetVelocity;
- }
- set
- {
- }
- }
- public override int VehicleType
- {
- get
- {
- return 0;
- }
- set
- {
- return;
- }
- }
- public override void VehicleFloatParam(int param, float value)
- {
- }
- public override void VehicleVectorParam(int param, Vector3 value)
- {
- }
- public override void VehicleRotationParam(int param, Quaternion rotation)
- {
- }
- public override void VehicleFlags(int param, bool remove)
- {
- }
- public override void SetVolumeDetect(int param)
- {
- }
- public override Vector3 CenterOfMass
- {
- get
- {
- return _position;
- }
- }
- public override Vector3 GeometricCenter
- {
- get
- {
- return _position;
- }
- }
- public override PrimitiveBaseShape Shape
- {
- set
- {
- return;
- }
- }
- public override Vector3 rootVelocity
- {
- get
- {
- return _velocity;
- }
- }
- public override Vector3 Velocity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return _velocity;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- if (value.IsFinite())
- {
- AddChange(changes.Velocity, value);
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
- }
- }
- }
- public override Vector3 TargetVelocity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_targetVelocity;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- if (value.IsFinite())
- {
- AddChange(changes.TargetVelocity, value);
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
- }
- }
- }
- public override Vector3 Torque
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return Vector3.Zero;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- return;
- }
- }
- public override float CollisionScore
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return 0f;
- }
- set
- {
- }
- }
- public override bool Kinematic
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return false;
- }
- set
- {
- }
- }
- public override Quaternion Orientation
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_orientation;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- //fakeori = value;
- //givefakeori++;
- value.Normalize();
- AddChange(changes.Orientation, value);
- }
- }
- public override Vector3 Acceleration
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return _acceleration;
- }
- set
- {
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void SetAcceleration(Vector3 accel)
- {
- m_pidControllerActive = true;
- _acceleration = accel;
- if (Body != IntPtr.Zero)
- UBOdeNative.BodyEnable(Body);
- }
- /// <summary>
- /// Adds the force supplied to the Target Velocity
- /// The PID controller takes this target velocity and tries to make it a reality
- /// </summary>
- /// <param name="force"></param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void AddForce(Vector3 force, bool pushforce)
- {
- if (force.IsFinite())
- {
- if (pushforce)
- {
- AddChange(changes.Force, force * m_density * m_sceneInverseTimeStep / 28f);
- }
- else
- {
- AddChange(changes.TargetVelocity, force);
- }
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character");
- }
- //m_lastUpdateSent = false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void AvatarJump(float impulseZ)
- {
- // convert back to force and remove mass effect
- AddChange(changes.Force, new Vector3(0, 0, impulseZ * m_massInvTimeScaled));
- }
- public override void AddAngularForce(Vector3 force, bool pushforce)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void SetMomentum(Vector3 momentum)
- {
- if (momentum.IsFinite())
- AddChange(changes.Momentum, momentum);
- }
- private void AvatarGeomAndBodyCreate()
- {
- float sx = m_size.X;
- float sy = m_size.Y;
- float sz = m_size.Z;
- float bot = -sz;
- boneOff = bot + 0.3f;
- float feetsz = sz * 0.45f;
- if (feetsz > 0.6f)
- feetsz = 0.6f;
- feetOff = bot + feetsz;
- AvaSizeXsq = sx;
- AvaSizeXsq *= AvaSizeXsq;
- AvaSizeYsq = sy;
- AvaSizeYsq *= AvaSizeYsq;
- CapsuleRadius = MathF.Max(sx, sy);
- float l = sz - CapsuleRadius;
- CapsuleSizeZ = sz;
- collider = UBOdeNative.CreateCapsule(m_parent_scene.TopSpace, CapsuleRadius, 2.0f * l);
- UBOdeNative.GeomSetCategoryBits(collider, (uint)m_collisionCategories);
- UBOdeNative.GeomSetCollideBits(collider, (uint)m_collisionFlags);
- // update mass
- m_mass = 8f * m_density * sx * sy * sz;
- UBOdeNative.MassSetBoxTotal(out ShellMass, m_mass, 2f * sx, 2f * sy, 2f * sz);
- m_massInvTimeScaled = m_mass * m_sceneInverseTimeStep;
- PID_D = basePID_D * m_massInvTimeScaled;
- PID_P = basePID_P * m_massInvTimeScaled;
- m_scenegravityForceZ = m_parent_scene.gravityz * m_mass;
- Body = UBOdeNative.BodyCreate(m_parent_scene.world);
- _zeroFlag = false;
- m_pidControllerActive = true;
- m_freemove = false;
- _velocity = Vector3.Zero;
- // SafeNativeMethods.BodySetAutoDisableFlag(Body,false);
- UBOdeNative.BodySetAutoDisableFlag(Body, true);
- m_bodydisablecontrol = 0;
- UBOdeNative.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
- UBOdeNative.BodySetMass(Body, ref ShellMass);
- //SafeNativeMethods.GeomSetBody(capsule, Body);
- UBOdeNative.GeomSetBody(collider, Body);
- // The purpose of the AMotor here is to keep the avatar's physical
- // surrogate from rotating while moving
- Amotor = UBOdeNative.JointCreateAMotor(m_parent_scene.world, IntPtr.Zero);
- UBOdeNative.JointAttach(Amotor, Body, IntPtr.Zero);
- UBOdeNative.JointSetAMotorMode(Amotor, 0);
- UBOdeNative.JointSetAMotorNumAxes(Amotor, 3);
- UBOdeNative.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
- UBOdeNative.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
- UBOdeNative.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
- UBOdeNative.JointSetAMotorAngle(Amotor, 0, 0);
- UBOdeNative.JointSetAMotorAngle(Amotor, 1, 0);
- UBOdeNative.JointSetAMotorAngle(Amotor, 2, 0);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.StopCFM, 0f); // make it HARD
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.StopCFM2, 0f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.StopCFM3, 0f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.StopERP, 0.8f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.StopERP2, 0.8f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.StopERP3, 0.8f);
- // These lowstops and high stops are effectively (no wiggle room)
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -1e-5f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 1e-5f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -1e-5f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 1e-5f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -1e-5f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 1e-5f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.Vel, 0);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.Vel2, 0);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)UBOdeNative.JointParam.Vel3, 0);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.FMax, 5e8f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.FMax2, 5e8f);
- UBOdeNative.JointSetAMotorParam(Amotor, (int)dParam.FMax3, 5e8f);
- }
- /// <summary>
- /// Destroys the avatar body and geom
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void AvatarGeomAndBodyDestroy()
- {
- // Kill the Amotor
- if (Amotor != IntPtr.Zero)
- {
- UBOdeNative.JointDestroy(Amotor);
- Amotor = IntPtr.Zero;
- }
- if (Body != IntPtr.Zero)
- {
- //kill the body
- UBOdeNative.BodyDestroy(Body);
- Body = IntPtr.Zero;
- }
- //kill the Geoms
- /*
- if (capsule != IntPtr.Zero)
- {
- m_parent_scene.actor_name_map.Remove(capsule);
- //m_parent_scene.waitForSpaceUnlock(collider);
- SafeNativeMethods.GeomDestroy(capsule);
- capsule = IntPtr.Zero;
- }
- if (collider != IntPtr.Zero)
- {
- SafeNativeMethods.SpaceDestroy(collider);
- collider = IntPtr.Zero;
- }
- */
- if (collider != IntPtr.Zero)
- {
- m_parent_scene.actor_name_map.Remove(collider);
- //m_parent_scene.waitForSpaceUnlock(m_parent_scene.CharsSpace);
- UBOdeNative.GeomDestroy(collider);
- collider = IntPtr.Zero;
- }
- }
- //in place 2D rotation around Z assuming rot is normalised and is a rotation around Z
- public static void RotateXYonZ(ref float x, ref float y, ref Quaternion rot)
- {
- float sin = 2.0f * rot.Z * rot.W;
- float cos = rot.W * rot.W - rot.Z * rot.Z;
- float tx = x;
- x = tx * cos - y * sin;
- y = tx * sin + y * cos;
- }
- public static void RotateXYonZ(ref float x, ref float y, float sin, float cos)
- {
- float tx = x;
- x = tx * cos - y * sin;
- y = tx * sin + y * cos;
- }
- public static void invRotateXYonZ(ref float x, ref float y, float sin, float cos)
- {
- float tx = x;
- x = tx * cos + y * sin;
- y = -tx * sin + y * cos;
- }
- public static void invRotateXYonZ(ref float x, ref float y, in Quaternion rot)
- {
- float sin = -2.0f * rot.Z * rot.W;
- float cos = rot.W * rot.W - rot.Z * rot.Z;
- float tx = x;
- x = tx * cos - y * sin;
- y = tx * sin + y * cos;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool Collide(IntPtr other, bool reverse, ref UBOdeNative.ContactGeom contact,
- ref UBOdeNative.ContactGeom altContact, ref bool useAltcontact, ref bool feetcollision)
- {
- feetcollision = false;
- useAltcontact = false;
- Vector3 offset;
- float h = contact.pos.Z - _position.Z;
- offset.Z = h - feetOff;
- offset.X = contact.pos.X - _position.X;
- offset.Y = contact.pos.Y - _position.Y;
- UBOdeNative.GeomClassID gtype = UBOdeNative.GeomGetClass(other);
- if (gtype == UBOdeNative.GeomClassID.SphereClass && UBOdeNative.GeomGetBody(other) != IntPtr.Zero)
- {
- if (UBOdeNative.GeomSphereGetRadius(other) < 0.5)
- return true;
- }
- if (offset.Z > 0 || contact.normal.Z > 0.35f)
- {
- if (offset.Z <= 0)
- {
- feetcollision = true;
- if (h < boneOff)
- {
- CollideNormal = Unsafe.As<UBOdeNative.Vector3, Vector3>(ref contact.normal);
- IsColliding = true;
- }
- }
- return true;
- }
- if (m_flying)
- return true;
- feetcollision = true;
- if (h < boneOff)
- {
- CollideNormal = Unsafe.As<UBOdeNative.Vector3, Vector3>(ref contact.normal);
- IsColliding = true;
- }
- useAltcontact = true;
- offset.Z -= 0.2f;
- offset.Normalize();
- float tdp = contact.depth;
- float t = offset.X;
- t = MathF.Abs(t);
- if (t > 1e-6)
- {
- tdp /= t;
- tdp *= contact.normal.X;
- }
- else
- tdp *= 10;
- if (tdp > 0.25f)
- tdp = 0.25f;
- altContact.pos = contact.pos;
- altContact.depth = tdp;
- if (reverse)
- {
- altContact.normal = Unsafe.As<Vector3, UBOdeNative.Vector3>(ref offset);
- }
- else
- {
- altContact.normal.X = -offset.X;
- altContact.normal.Y = -offset.Y;
- altContact.normal.Z = -offset.Z;
- }
- return true;
- }
- /// <summary>
- /// Called from Simulate
- /// This is the avatar's movement control + PID Controller
- /// </summary>
- /// <param name="timeStep"></param>
- public void Move()
- {
- if (Body == IntPtr.Zero)
- return;
- if (!UBOdeNative.BodyIsEnabled(Body))
- {
- if (++m_bodydisablecontrol < 50)
- return;
- // clear residuals
- UBOdeNative.BodySetAngularVel(Body, 0f, 0f, 0f);
- UBOdeNative.BodySetLinearVel(Body, 0f, 0f, 0f);
- _zeroFlag = true;
- UBOdeNative.BodyEnable(Body);
- }
- m_bodydisablecontrol = 0;
- // the Amotor still lets avatar rotation to drift during colisions
- // so force it back to identity
- UBOdeNative.BodySetQuaternion(Body, ref m_NativeOrientation2D);
- _position = UBOdeNative.BodyGetPositionOMV(Body);
- // check outbounds forcing to be in world
- bool fixbody = false;
- if ((Single.IsNaN(_position.X) || Single.IsInfinity(_position.X)))
- {
- fixbody = true;
- _position.X = 128f;
- }
- else if (_position.X < 0.0f)
- {
- fixbody = true;
- _position.X = 0.1f;
- }
- else if (_position.X > m_parent_scene.WorldExtents.X - 0.1f)
- {
- fixbody = true;
- _position.X = m_parent_scene.WorldExtents.X - 0.1f;
- }
- if ((Single.IsNaN(_position.Y) || Single.IsInfinity(_position.Y)))
- {
- fixbody = true;
- _position.Y = 128f;
- }
- else if (_position.Y < 0.0f)
- {
- fixbody = true;
- _position.Y = 0.1f;
- }
- else if (_position.Y > m_parent_scene.WorldExtents.Y - 0.1f)
- {
- fixbody = true;
- _position.Y = m_parent_scene.WorldExtents.Y - 0.1f;
- }
- if ((Single.IsNaN(_position.Z) || Single.IsInfinity(_position.Z)))
- {
- fixbody = true;
- _position.Z = 128f;
- }
- if (fixbody)
- {
- m_freemove = false;
- UBOdeNative.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
- }
- if (!m_pidControllerActive)
- _zeroPosition = _position;
- //Update AABB
- UpdateAABB2D();
- float aabbminz = _position.Z - CapsuleSizeZ;
- //float aabbmaxz = _position.Z + CapsuleSizeZ;
- bool tviszero = m_targetVelocity.IsZero();
- Vector3 ctv;
- if (tviszero)
- ctv = Vector3.Zero;
- else
- {
- if (m_alwaysRun)
- {
- ctv = new( m_targetVelocity.X * m_runMultiplier,
- m_targetVelocity.Y * m_runMultiplier,
- m_targetVelocity.Z);
- }
- else
- {
- ctv = new ( m_targetVelocity.X * m_walkMultiplier,
- m_targetVelocity.Y * m_walkMultiplier,
- m_targetVelocity.Z);
- }
- }
- Vector3 vel = UBOdeNative.BodyGetLinearVelOMV(Body);
- //******************************************
- // colide with land
- float tmpX = _position.X;
- float tmpY = _position.Y;
- if (m_flying)
- {
- tmpX += vel.X * m_sceneTimeStep;
- tmpY += vel.Y * m_sceneTimeStep;
- }
- Vector3 vec = Vector3.Zero;
- float terrainheight = m_parent_scene.GetTerrainHeightAtXY(tmpX, tmpY);
- if (aabbminz < terrainheight)
- {
- if (ctv.Z < 0f)
- ctv.Z = 0f;
- if (!m_haveLastFallVel)
- {
- m_lastFallVel = vel;
- m_haveLastFallVel = true;
- }
- float pidp50 = PID_P * 50;
- float depth = terrainheight - aabbminz;
- vec.Z = depth * pidp50;
- Vector3 n = m_parent_scene.GetTerrainNormalAtXY(tmpX, tmpY);
- if (!m_flying)
- {
- vec.Z -= vel.Z * PID_D;
- if (n.Z < 0.4f)
- {
- vec.X = depth * pidp50 - vel.X * PID_D;
- vec.X *= n.X;
- vec.Y = depth * pidp50 - vel.Y * PID_D;
- vec.Y *= n.Y;
- vec.Z *= n.Z;
- if (n.Z < 0.1f)
- {
- // cancel the slope pose
- n.X = 0f;
- n.Y = 0f;
- n.Z = 1.0f;
- }
- }
- }
- if (depth < 0.2f)
- {
- m_colliderGroundfilter++;
- if (m_colliderGroundfilter > 2)
- {
- m_iscolliding = true;
- Colliderfilter = 2;
- if (m_colliderGroundfilter > 10)
- {
- m_colliderGroundfilter = 10;
- m_freemove = false;
- }
- CollideNormal = n;
- m_iscollidingGround = true;
- ContactPoint contact = new()
- {
- PenetrationDepth = depth,
- Position = new( _position.X, _position.Y, terrainheight),
- SurfaceNormal = -n,
- RelativeSpeed = Vector3.Dot(m_lastFallVel, n),
- CharacterFeet = true
- };
- AddCollisionEvent(0,contact);
- m_lastFallVel = Vector3.Zero;
- }
- }
- else
- {
- m_colliderGroundfilter -= 5;
- if (m_colliderGroundfilter <= 0)
- {
- m_colliderGroundfilter = 0;
- m_iscollidingGround = false;
- }
- }
- }
- else
- {
- m_haveLastFallVel = false;
- m_colliderGroundfilter -= 5;
- if (m_colliderGroundfilter <= 0)
- {
- m_colliderGroundfilter = 0;
- m_iscollidingGround = false;
- }
- }
- bool hoverPIDActive = false;
- if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
- {
- hoverPIDActive = true;
- switch (m_PIDHoverType)
- {
- case PIDHoverType.Ground:
- m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
- break;
- case PIDHoverType.GroundAndWater:
- if (terrainheight > m_parent_scene.WaterLevel)
- m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
- else
- m_targetHoverHeight = m_parent_scene.WaterLevel + m_PIDHoverHeight;
- break;
- } // end switch (m_PIDHoverType)
- // don't go underground
- if (m_targetHoverHeight > terrainheight + _position.Z)
- {
- float fz = (m_targetHoverHeight - _position.Z);
- // if error is zero, use position control; otherwise, velocity control
- if (MathF.Abs(fz) < 0.01f)
- {
- ctv.Z = 0;
- }
- else
- {
- _zeroFlag = false;
- fz /= m_PIDHoverTau;
- float tmp = MathF.Abs(fz);
- if (tmp > 50f)
- fz = 50f * MathF.Sign(fz);
- else if (tmp < 0.1f)
- fz = 0.1f * MathF.Sign(fz);
- ctv.Z = fz;
- }
- }
- }
- //******************************************
- if (!m_iscolliding)
- CollideNormal.Z = 0;
- if (!tviszero)
- {
- m_freemove = false;
- // movement relative to surface if moving on it
- // dont disturbe vertical movement, ie jumps
- if (m_iscolliding && !m_flying && ctv.Z == 0f && CollideNormal.Z > 0.2f && CollideNormal.Z < 0.94f)
- {
- float p = ctv.X * CollideNormal.X + ctv.Y * CollideNormal.Y;
- ctv.X *= MathF.Sqrt(1 - CollideNormal.X * CollideNormal.X);
- ctv.Y *= MathF.Sqrt(1 - CollideNormal.Y * CollideNormal.Y);
- ctv.Z -= p;
- if (ctv.Z < 0f)
- ctv.Z *= 2f;
- }
- }
- float breakfactor;
- if (!m_freemove)
- {
- // if velocity is zero, use position control; otherwise, velocity control
- if (tviszero)
- {
- if (m_iscolliding || m_flying)
- {
- // keep track of where we stopped. No more slippin' & slidin'
- if (!_zeroFlag)
- {
- _zeroFlag = true;
- _zeroPosition = _position;
- if(!m_pidControllerActive)
- {
- float pidd0833 = PID_D * 0.833f;
- vec.X -= vel.X * pidd0833;
- vec.Y -= vel.Y * pidd0833;
- }
- }
- if (m_pidControllerActive)
- {
- // We only want to deactivate the PID Controller if we think we want to have our surrogate
- // react to the physics scene by moving it's position.
- // Avatar to Avatar collisions
- // Prim to avatar collisions
- float pidd2 = PID_D * 2f;
- float pidp5 = PID_P * 5;
- vec.X = -vel.X * pidd2 + (_zeroPosition.X - _position.X) * pidp5;
- vec.Y = -vel.Y * pidd2 + (_zeroPosition.Y - _position.Y) * pidp5;
- if (vel.Z > 0)
- vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - _position.Z) * PID_P;
- else
- vec.Z += (-vel.Z * PID_D + (_zeroPosition.Z - _position.Z) * PID_P) * 0.2f;
- }
- }
- else
- {
- _zeroFlag = false;
- float pidd0833 = PID_D * 0.833f;
- vec.X += (ctv.X - vel.X) * pidd0833;
- vec.Y += (ctv.Y - vel.Y) * pidd0833;
- // hack for breaking on fall
- if (ctv.Z == -9999f)
- vec.Z += -vel.Z * PID_D - m_scenegravityForceZ;
- }
- }
- else
- {
- m_pidControllerActive = true;
- _zeroFlag = false;
- if (m_iscolliding)
- {
- if (!m_flying)
- {
- // we are on a surface
- if (ctv.Z > 0f)
- {
- // moving up or JUMPING
- vec.Z += (ctv.Z - vel.Z) * PID_D * 2f;
- vec.X += (ctv.X - vel.X) * PID_D;
- vec.Y += (ctv.Y - vel.Y) * PID_D;
- }
- else
- {
- // we are moving down on a surface
- if (ctv.Z == 0)
- {
- if (vel.Z > 0)
- vec.Z -= vel.Z * PID_D * 2f;
- vec.X += (ctv.X - vel.X) * PID_D;
- vec.Y += (ctv.Y - vel.Y) * PID_D;
- }
- // intencionally going down
- else
- {
- if (ctv.Z < vel.Z)
- vec.Z += (ctv.Z - vel.Z) * PID_D;
- else
- {
- }
- if (MathF.Abs(ctv.X) > MathF.Abs(vel.X))
- vec.X += (ctv.X - vel.X) * PID_D;
- if (MathF.Abs(ctv.Y) > MathF.Abs(vel.Y))
- vec.Y += (ctv.Y - vel.Y) * PID_D;
- }
- }
- // We're standing on something
- }
- else
- {
- // We're flying and colliding with something
- float pidd00625 = PID_D * 0.0625f;
- vec.X += (ctv.X - vel.X) * pidd00625;
- vec.Y += (ctv.Y - vel.Y) * pidd00625;
- vec.Z += (ctv.Z - vel.Z) * pidd00625;
- }
- }
- else // ie not colliding
- {
- if (m_flying || hoverPIDActive) //(!m_iscolliding && flying)
- {
- // we're in mid air suspended
- vec.X += (ctv.X - vel.X) * PID_D;
- vec.Y += (ctv.Y - vel.Y) * PID_D;
- vec.Z += (ctv.Z - vel.Z) * PID_D;
- }
- else
- {
- // we're not colliding and we're not flying so that means we're falling!
- // m_iscolliding includes collisions with the ground.
- // d.Vector3 pos = d.BodyGetPosition(Body);
- float pidd0833 = PID_D * 0.833f;
- vec.X += (ctv.X - vel.X) * pidd0833;
- vec.Y += (ctv.Y - vel.Y) * pidd0833;
- // hack for breaking on fall
- if (ctv.Z == -9999f)
- vec.Z += -vel.Z * PID_D - m_scenegravityForceZ;
- }
- }
- }
- float velLengthSquared = vel.LengthSquared();
- if (velLengthSquared > 2500.0f) // 50m/s apply breaks
- {
- breakfactor = 0.16f * m_mass;
- vec.X -= breakfactor * vel.X;
- vec.Y -= breakfactor * vel.Y;
- vec.Z -= breakfactor * vel.Z;
- }
- }
- else
- {
- breakfactor = m_mass;
- vec.X -= breakfactor * vel.X;
- vec.Y -= breakfactor * vel.Y;
- if (m_flying)
- vec.Z -= 0.5f * breakfactor * vel.Z;
- else
- vec.Z -= .16f * m_mass * vel.Z;
- }
- if (m_flying || hoverPIDActive)
- {
- vec.Z -= m_scenegravityForceZ;
- if (!hoverPIDActive)
- {
- //Added for auto fly height. Kitto Flora
- float target_altitude = terrainheight + MinimumGroundFlightOffset;
- if (_position.Z < target_altitude)
- {
- vec.Z += (target_altitude - _position.Z) * PID_P * 5.0f;
- }
- // end add Kitto Flora
- }
- }
- else if (m_buoyancy != 0.0)
- {
- vec.Z -= m_scenegravityForceZ * m_buoyancy;
- }
- if ((vec.Z != 0 || vec.X != 0 || vec.Y != 0))
- UBOdeNative.BodyAddForce(Body, vec.X, vec.Y, vec.Z);
- if (_zeroFlag)
- {
- _velocity = Vector3.Zero;
- _acceleration = Vector3.Zero;
- m_rotationalVelocity = Vector3.Zero;
- }
- else
- {
- Vector3 a = _velocity; // previous velocity
- SetSmooth(ref _velocity, ref vel, 2);
- a = (_velocity - a) * m_sceneInverseTimeStep;
- SetSmooth(ref _acceleration, ref a, 2);
- m_rotationalVelocity = UBOdeNative.BodyGetAngularVelOMVforAvatar(Body);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void round(ref Vector3 v, int digits)
- {
- v.X = MathF.Round(v.X, digits);
- v.Y = MathF.Round(v.Y, digits);
- v.Z = MathF.Round(v.Z, digits);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void SetSmooth(ref Vector3 dst, ref Vector3 value, int rounddigits)
- {
- dst.X = 0.4f * dst.X + 0.6f * value.X;
- dst.X = MathF.Round(dst.X, rounddigits);
- dst.Y = 0.4f * dst.Y + 0.6f * value.Y;
- dst.Y = MathF.Round(dst.Y, rounddigits);
- dst.Z = 0.4f * dst.Z + 0.6f * value.Z;
- dst.Z = MathF.Round(dst.Z, rounddigits);
- }
- /// <summary>
- /// Updates the reported position and velocity.
- /// Used to copy variables from unmanaged space at heartbeat rate and also trigger scene updates acording
- /// also outbounds checking
- /// copy and outbounds now done in move(..) at ode rate
- ///
- /// </summary>
- /*
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void UpdatePositionAndVelocity()
- {
- }
- */
- /// <summary>
- /// Cleanup the things we use in the scene.
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Destroy()
- {
- AddChange(changes.Remove, null);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void CrossingFailure()
- {
- }
- public override Vector3 PIDTarget
- {
- set
- {
- return;
- }
- }
- public override bool PIDActive
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_pidControllerActive;
- }
- set
- {
- }
- }
- public override float PIDTau
- {
- set
- {
- }
- }
- public override float PIDHoverHeight
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- AddChange(changes.PIDHoverHeight, value);
- }
- }
- public override bool PIDHoverActive
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return m_useHoverPID;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- AddChange(changes.PIDHoverActive, value);
- }
- }
- public override PIDHoverType PIDHoverType
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- AddChange(changes.PIDHoverType, value);
- }
- }
- public override float PIDHoverTau
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- float tmp = 0;
- if (value > 0)
- {
- float mint = m_sceneTimeStep < 0.05f ? 0.05f : m_sceneTimeStep;
- tmp = value < mint ? mint : value;
- }
- AddChange(changes.PIDHoverTau, tmp);
- }
- }
- public override Quaternion APIDTarget
- {
- set
- {
- }
- }
- public override bool APIDActive
- {
- set
- {
- }
- }
- public override float APIDStrength
- {
- set
- {
- }
- }
- public override float APIDDamping
- {
- set
- {
- return;
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void SubscribeEvents(int ms)
- {
- m_eventsubscription = ms;
- m_cureventsubscription = 0;
- CollisionEventsThisFrame.Clear();
- SentEmptyCollisionsEvent = false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void UnSubscribeEvents()
- {
- m_eventsubscription = 0;
- m_parent_scene.RemoveCollisionEventReporting(this);
- lock (CollisionEventsThisFrame)
- CollisionEventsThisFrame.Clear();
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
- {
- lock (CollisionEventsThisFrame)
- CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
- m_parent_scene.AddCollisionEventReporting(this);
- }
- public void SendCollisions(int timestep)
- {
- if (m_cureventsubscription < 50000)
- m_cureventsubscription += timestep;
- if (m_cureventsubscription < m_eventsubscription)
- return;
- if (Body != IntPtr.Zero && !UBOdeNative.BodyIsEnabled(Body))
- return;
- lock (CollisionEventsThisFrame)
- {
- int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
- if (!SentEmptyCollisionsEvent || ncolisions > 0)
- {
- base.SendCollisionUpdate(CollisionEventsThisFrame);
- m_cureventsubscription = 0;
- if (ncolisions == 0)
- {
- SentEmptyCollisionsEvent = true;
- //_parent_scene.RemoveCollisionEventReporting(this);
- }
- else
- {
- SentEmptyCollisionsEvent = false;
- CollisionEventsThisFrame.Clear();
- }
- }
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override bool SubscribedEvents()
- {
- if (m_eventsubscription > 0)
- return true;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changePhysicsStatus(bool NewStatus)
- {
- if (NewStatus != m_isPhysical)
- {
- if (NewStatus)
- {
- AvatarGeomAndBodyDestroy();
- AvatarGeomAndBodyCreate();
- m_parent_scene.actor_name_map[collider] = this;
- m_parent_scene.AddCharacter(this);
- }
- else
- {
- m_parent_scene.RemoveCollisionEventReporting(this);
- m_parent_scene.RemoveCharacter(this);
- // destroy avatar capsule and related ODE data
- AvatarGeomAndBodyDestroy();
- }
- m_freemove = false;
- m_isPhysical = NewStatus;
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeAdd()
- {
- changePhysicsStatus(true);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeRemove()
- {
- changePhysicsStatus(false);
- }
- /*
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeShape(PrimitiveBaseShape arg)
- {
- }
- */
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeAvatarSize(strAvatarSize st)
- {
- changeSize(st.size);
- }
- private void changeSize(Vector3 pSize)
- {
- if (pSize.IsFinite())
- {
- // for now only look to Z changes since viewers also don't change X and Y
- if (pSize.Z != m_size.Z)
- {
- float oldsz = m_size.Z;
- m_size = pSize * 0.5f;
- float sz = m_size.Z;
- float bot = -sz;
- boneOff = bot + 0.3f;
- float feetsz = sz * 0.9f;
- if (feetsz > 0.6f)
- feetsz = 0.6f;
- feetOff = bot + feetsz;
- float sx = m_size.X;
- AvaSizeXsq = sx;
- AvaSizeXsq *= AvaSizeXsq;
- float sy = m_size.Y;
- AvaSizeYsq = sy;
- AvaSizeYsq *= AvaSizeYsq;
- CapsuleRadius = MathF.Max(sx, sy);
- float l = sz - CapsuleRadius;
- CapsuleSizeZ= sz;
- UBOdeNative.GeomCapsuleSetParams(collider, CapsuleRadius, 2f * l);
- m_mass = 8f * m_density * sx * sy * sz; // update mass
- m_massInvTimeScaled = m_mass * m_sceneInverseTimeStep;
- PID_D = basePID_D * m_massInvTimeScaled;
- PID_P = basePID_P * m_massInvTimeScaled;
- UBOdeNative.MassSetBoxTotal(out ShellMass, m_mass, 2f * sx, 2f * sy, 2f * sz);
- UBOdeNative.BodySetMass(Body, ref ShellMass);
- m_scenegravityForceZ = m_parent_scene.gravityz * m_mass;
- _position.Z += sz - oldsz;
- UBOdeNative.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
- UpdateAABB2D();
- m_bodydisablecontrol = 0;
- _zeroFlag = false;
- _velocity = Vector3.Zero;
- m_targetVelocity = Vector3.Zero;
- }
- m_freemove = false;
- m_pidControllerActive = true;
- }
- else
- {
- m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changePosition(Vector3 newPos)
- {
- if (Body != IntPtr.Zero)
- {
- UBOdeNative.BodySetPosition(Body, newPos.X, newPos.Y, newPos.Z);
- UBOdeNative.BodyEnable(Body);
- }
- _position = newPos;
- UpdateAABB2D();
- m_freemove = false;
- _zeroFlag = false;
- m_pidControllerActive = true;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeOrientation(Quaternion newOri)
- {
- if (m_orientation.NotEqual(newOri))
- {
-
- m_orientation = newOri;
- Orientation2D.X = newOri.Z;
- Orientation2D.Y = newOri.W;
- Orientation2D.Normalize();
- m_NativeOrientation2D.X = 0;
- m_NativeOrientation2D.Y = 0;
- m_NativeOrientation2D.Z = Orientation2D.X;
- m_NativeOrientation2D.W = Orientation2D.Y;
- if (Body != IntPtr.Zero)
- {
- UBOdeNative.BodySetQuaternion(Body, ref m_NativeOrientation2D);
- UBOdeNative.BodyEnable(Body);
- }
- }
- else if (Body != IntPtr.Zero)
- UBOdeNative.BodyEnable(Body);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeVelocity(Vector3 newVel)
- {
- _velocity = newVel;
- setFreeMove();
- if (Body != IntPtr.Zero)
- {
- UBOdeNative.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
- UBOdeNative.BodyEnable(Body);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeTargetVelocity(Vector3 newVel)
- {
- //m_pidControllerActive = true;
- //m_freemove = false;
- m_targetVelocity = newVel;
- if (Body != IntPtr.Zero)
- UBOdeNative.BodyEnable(Body);
- }
- /*
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeSetTorque(Vector3 newTorque)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeAddForce(Vector3 newForce)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeAddAngularForce(Vector3 arg)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeAngularLock(byte arg)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeFloatOnWater(bool arg)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeVolumedetetion(bool arg)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeSelectedStatus(bool arg)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeDisable(bool arg)
- {
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeBuilding(bool arg)
- {
- }
- */
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void setFreeMove()
- {
- m_pidControllerActive = true;
- _zeroFlag = false;
- m_targetVelocity = Vector3.Zero;
- m_freemove = true;
- Colliderfilter = int.MinValue;
- m_colliderObjectfilter = -1;
- m_colliderGroundfilter = -1;
- m_iscolliding = false;
- m_iscollidingGround = false;
- m_iscollidingObj = false;
- CollisionEventsThisFrame.Clear();
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeForce(Vector3 newForce)
- {
- setFreeMove();
- if (Body != IntPtr.Zero)
- {
- if (newForce.X != 0f || newForce.Y != 0f || newForce.Z != 0)
- UBOdeNative.BodyAddForce(Body, newForce.X, newForce.Y, newForce.Z);
- UBOdeNative.BodyEnable(Body);
- }
- }
- // for now momentum is actually velocity
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changeMomentum(Vector3 newmomentum)
- {
- _velocity = newmomentum;
- setFreeMove();
- if (Body != IntPtr.Zero)
- {
- UBOdeNative.BodySetLinearVel(Body, newmomentum.X, newmomentum.Y, newmomentum.Z);
- UBOdeNative.BodyEnable(Body);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changePIDHoverHeight(float val)
- {
- m_PIDHoverHeight = val;
- if (val == 0)
- m_useHoverPID = false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changePIDHoverType(PIDHoverType type)
- {
- m_PIDHoverType = type;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changePIDHoverTau(float tau)
- {
- m_PIDHoverTau = tau;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void changePIDHoverActive(bool active)
- {
- m_useHoverPID = active;
- }
- /*
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private void donullchange()
- {
- }
- */
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void UpdateAABB2D()
- {
- _AABB2D.minx = _position.X - CapsuleRadius;
- _AABB2D.maxx = _position.X + CapsuleRadius;
- _AABB2D.miny = _position.Y - CapsuleRadius;
- _AABB2D.maxy = _position.Y + CapsuleRadius;
- }
- public bool DoAChange(changes what, object arg)
- {
- if (collider == IntPtr.Zero && what != changes.Add && what != changes.Remove)
- {
- return false;
- }
- switch (what)
- {
- case changes.Add:
- changeAdd();
- break;
- case changes.Remove:
- changeRemove();
- break;
- case changes.Position:
- changePosition((Vector3)arg);
- break;
- case changes.Orientation:
- changeOrientation((Quaternion)arg);
- break;
- /*
- case changes.PosOffset:
- donullchange();
- break;
- case changes.OriOffset:
- donullchange();
- break;
- */
- case changes.Velocity:
- changeVelocity((Vector3)arg);
- break;
- case changes.TargetVelocity:
- changeTargetVelocity((Vector3)arg);
- break;
- /*
- case changes.Acceleration:
- changeacceleration((Vector3)arg);
- break;
- case changes.AngVelocity:
- changeangvelocity((Vector3)arg);
- break;
- */
- case changes.Force:
- changeForce((Vector3)arg);
- break;
- /*
- case changes.Torque:
- changeSetTorque((Vector3)arg);
- break;
- case changes.AddForce:
- changeAddForce((Vector3)arg);
- break;
- case changes.AddAngForce:
- changeAddAngularForce((Vector3)arg);
- break;
- case changes.AngLock:
- changeAngularLock((byte)arg);
- break;
- */
- case changes.Size:
- changeSize((Vector3)arg);
- break;
- case changes.AvatarSize:
- changeAvatarSize((strAvatarSize)arg);
- break;
- case changes.Momentum:
- changeMomentum((Vector3)arg);
- break;
- case changes.PIDHoverHeight:
- changePIDHoverHeight((float)arg);
- break;
- case changes.PIDHoverType:
- changePIDHoverType((PIDHoverType)arg);
- break;
- case changes.PIDHoverTau:
- changePIDHoverTau((float)arg);
- break;
- case changes.PIDHoverActive:
- changePIDHoverActive((bool)arg);
- break;
- /*
- case changes.Shape:
- changeShape((PrimitiveBaseShape)arg);
- break;
- case changes.CollidesWater:
- changeFloatOnWater((bool)arg);
- break;
- case changes.VolumeDtc:
- changeVolumedetetion((bool)arg);
- break;
- */
- case changes.Physical:
- changePhysicsStatus((bool)arg);
- break;
- /*
- case changes.Selected:
- changeSelectedStatus((bool)arg);
- break;
- case changes.disabled:
- changeDisable((bool)arg);
- break;
- case changes.building:
- changeBuilding((bool)arg);
- break;
- */
- //case changes.Null:
- default:
- break;
- }
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void AddChange(changes what, object arg)
- {
- m_parent_scene.AddChange(this, what, arg);
- }
- private struct strAvatarSize
- {
- public Vector3 size;
- }
- }
- }
|