Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Transform.translate in tiny

Discussion in 'Project Tiny' started by WellC, Jan 1, 2019.

  1. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48

    How to implement this movement in Tiny-ecs?
    transform.Translate (Vector3.down * speed * Time.deltaTime, Space.Self);
     
  2. Stijn_cts

    Stijn_cts

    Joined:
    Nov 14, 2016
    Posts:
    4
    Something like this

    Code (JavaScript):
    1.   let dt = this.scheduler.deltaTime();          
    2.  
    3.             let speed = 2;
    4.             let movement = new Vector3(0, speed * dt, 0);
    5.             this.world.forEach([ut.Entity, ut.Core2D.TransformLocalPosition],
    6.                 (entity, localPosition)=>{
    7.                  
    8.                     localPosition.position.add(movement);
    9.  
    10.                 });
     
  3. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    Thanks for your reply.
    I tried your codes. It is work fine , but it is no movement by self-space.
     
  4. Nkon

    Nkon

    Unity Technologies

    Joined:
    Jun 12, 2017
    Posts:
    65
    Hi there,

    There are multiple ways to accomplish this. I'll attach an example here that uses a version of Rodriques' formula. In my opinion the pros of this method are ease of writing and avoiding unnecessary operations.

    Code (JavaScript):
    1. namespace game {
    2.     export class SelfMoveSystem extends ut.ComponentSystem {
    3.         OnUpdate():void {
    4.             this.world.forEach(
    5.                 [ut.Entity, ut.Core2D.TransformLocalPosition, ut.Core2D.TransformLocalRotation, game.SelfMove],
    6.                 (entity, localPos, localRot, selfMove) => {
    7.                 // movement speed
    8.                 const speed = this.scheduler.deltaTime();
    9.                 // object rotation
    10.                 const q = localRot.rotation;
    11.                 // required translation in self-space
    12.                 const v = new Vector3(0, speed, 0);
    13.  
    14.                 // rotate v according to Rodrigues' formula
    15.                 const axis = new Vector3(q.x, q.y, q.z);
    16.                 const temp = new Vector3(2 * axis.x, 2 * axis.y, 2 * axis.z);
    17.                 axis.cross(v);
    18.                 axis.add(new Vector3(q.w * v.x, q.w * v.y, q.w * v.z));
    19.                 temp.cross(axis);
    20.                 const move = new Vector3(v.x + temp.x, v.y + temp.y, v.z + temp.z);
    21.  
    22.                 // set new position
    23.                 const pos = localPos.position;
    24.                 localPos.position = new Vector3(pos.x + move.x, pos.y + move.y, pos.z + move.z);
    25.             });
    26.         }
    27.     }
    28. }
    To use the system, create an empty component named 'SelfMove' and add it to a sprite for example. The sprite should then move along its local y-axis.

    Cheers!
     
    ER_Dolleman likes this.
  5. Nkon

    Nkon

    Unity Technologies

    Joined:
    Jun 12, 2017
    Posts:
    65
    In the example, you can remove the calls to cross() and add() by writing the operations manually:

    Code (JavaScript):
    1. const ax = q.y * v.z - q.z * v.y + q.w * v.x;
    2. const ay = q.z * v.x - q.x * v.z + q.w * v.y;
    3. const az = q.x * v.y - q.y * v.x + q.w * v.z;
    4. const tx = q.x + q.x;
    5. const ty = q.y + q.y;
    6. const tz = q.z + q.z;
    7. const move = new Vector3(
    8.         ty * az - tz * ay + v.x,
    9.         tz * ax - tx * az + v.y,
    10.         tx * ay - ty * ax + v.z);
    If you prefer this way, just replace the part under "rotate v according to Rodriques' formula" with the above.

    Cheers!
     
  6. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    LOL,Thanks you for your reply.
    You solved my big problem.