public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { //Get arrays of objects that we'll be operating on NativeArray <PhysicsVelocity> physVel = chunk.GetNativeArray(physVType); NativeArray <PhysicsMass> physMass = chunk.GetNativeArray(physMassType); NativeArray <InputStruct> inputStructs = chunk.GetNativeArray(inputType); NativeArray <PhysicsControllerStruct> controllers = chunk.GetNativeArray(pControlType); NativeArray <LocalToWorld> toWorlds = chunk.GetNativeArray(toWorldType); NativeArray <Rotation> rotations = chunk.GetNativeArray(rotType); NativeArray <Translation> translations = chunk.GetNativeArray(transType); //Essentially IJobForEach for (int i = 0; i < chunk.Count; i++) { //Get data from arrays PhysicsVelocity rb = physVel[i]; PhysicsMass pMass = physMass[i]; InputStruct inp = inputStructs[i]; PhysicsControllerStruct controllerStruct = controllers[i]; LocalToWorld toWorld = toWorlds[i]; Rotation rot = rotations[i]; Translation trans = translations[i]; //Do work on data Move(ref rb, ref pMass, ref inp, ref controllerStruct, ref toWorld, ref rot, ref trans); //Set data in array to data we changed physVel[i] = rb; physMass[i] = pMass; //inputStructs[i] = inp; controllers[i] = controllerStruct; //toWorlds[i] = toWorld; rotations[i] = rot; translations[i] = trans; } }
public void Move(ref PhysicsVelocity rb, ref PhysicsMass mass, ref InputStruct input, ref PhysicsControllerStruct playerData, ref LocalToWorld toWorld, ref Rotation rot, ref Translation trans) { float3 targetVelocity = new float3(); float speed = 9; float gravity = 4; //Set target to input velocity targetVelocity.x = input.move.x; targetVelocity.z = input.move.y; //Calculate how fast we should be moving targetVelocity = TransformDirection(toWorld.Value, targetVelocity); //Change from local space to world space targetVelocity *= speed * deltaTime * 100; //Apply a force that attempts to reach our target velocity float3 velocityChange = (targetVelocity - rb.Linear); velocityChange.y = -gravity * deltaTime * 10; //Apply gravity to the player //Mouse movement rb.Angular.y = -input.mouseX * 2; //* deltaTime; mass.InverseInertia[0] = 0; mass.InverseInertia[2] = 0; if (playerData.isGrounded && input.jump && playerData.timeSinceLastJump > .1f) { velocityChange.y = 10; playerData.timeSinceLastJump = 0; } playerData.timeSinceLastJump += deltaTime; rb.ApplyLinearImpulse(mass, velocityChange); }