Jump to content

[SOLVED] Physic conflict with setDirection


photo

Recommended Posts

Hi all,

 

I'm using Unigine 2.0 and I want to create a real space environment with player oriented by their position on the planet. To simulate the gravity, i apply a force to the bodyrigid each frame to the direction of the planet. This works fine. But when i use the function Body::setDirection, my force is no longer applied. Why? How can i apply a force and orient a body together?

 

Thanks

Link to comment

Hey Bertrand,

 

Body::setDirection will reset all the velocities and forces. Actually, it's a helper method for Body::setTransform which do the dirty work. You need to use Body::setPreserveTransform (https://developer.unigine.com/en/docs/1.0/scripting/library/physics/class.body#setPreserveTransform_mat4_void) instead.

 

All you need to do is to calculate the matrix properly and pass it to the function, probably by using lookAt or setTo functions (https://developer.unigine.com/en/docs/1.0/scripting/core/math.matrix).

Link to comment

Hi,

 

It'll be more productive if you give me the context of what you're trying to achieve as well as paste your code here just to see how you're setting new transformation matrix.

 

Also, body transformation and node transformation is a little bit different things because body transformation won't apply immediately. If you want to apply it right after you changed it, please use Body::flushTransform.

Link to comment

Actually, this is where I am:

void		applyOrientation(PlayerActor& player)
{
      Body	body = player.getBody();
      Vec3	position = body.getPosition();
      Vec3	up = -_currentGravity;
      mat4	transform = body.getTransform();
      Vec3	direction = transform.m00m10m20;
      Vec3	dir = Vec3Rejection(direction, up);
      Vec3	viewPoint = position + dir;

      body.setPreserveTransform(setTo(position, viewPoint, up));
      body.flushTransform();
}

_currentGravity is a normalized Vec3 which represent the gravity orientation. I use it as the new up vector.

Actually i have a problem with the playeractor which seems to not be affected by this body transformation. So i tried to use player.setUp(up) in this function and it works until the camera flip at each frame. I think it is because i need to set a good forward vector but i don't know how.

 

Edit:

 

I try to change the PlayerActor transform too at the same time:

void                applyOrientation(PlayerActor& player)
{
      Vec3          newUp = -_currentGravity;
      Body          body = player.getBody();
      Vec3          bodyPos = body.getPosition();
      mat4          bodyTr = body.getTransform();
      Vec3          bodyDir = bodyTr.m00m10m20;
      Vec3          playerPos = player.getPosition();
      mat4          playerTr = player.getTransform();
      Vec3          playerDir = playerTr.m00m10m20;

      body.setPreserveTransform(getNewTransform(bodyPos, newUp, bodyDir));
      body.flushTransform();
      player.setTransform(getNewTransform(playerPos, newUp, playerDir));
}

mat4                getNewTransform(Vec3& pos, Vec3& newUp, Vec3 oldDir)
{
      Vec3          newDir = Vec3Rejection(oldDir, newUp);
      Vec3          viewPoint = pos + newDir;

      return (setTo(pos, viewPoint, newUp));
}

but it erase the rotation of the actor to the up/down :(

 

edit2:

 

I tried to save the theta angle and reset it after the player.setTransform and it works. But it seems that the player.setTransform reset the velocity.

Link to comment

Hey Bertrand,

 

Am I correct that you're trying to achieve PlayerActor that will walk across planet surface? If so then all you need to do is to apply gravity force to its body and set up vector to the camera itself.

Link to comment

Thank you very much!

i missed the setViewDirection thing. I think the explanation you gave me in the source file should be in the documentation because, usually, a "set" function don't make any computing.

Link to comment
×
×
  • Create New...