Jump to content

[SOLVED] addForce ignores object orientation


Recommended Posts

Let me first introduce myself, since this is my first topic on this forum. I'm Linus, a programmer working on a race/flight type game for Mechamania.

I don't have previous experience with the Unigine engine and am trying out different things to learn how to use it.

 

I ran into a problem when trying to control a node with forces.

I created a Node with a RigidBody and Shape as was shown in the tutorial. When I apply a torque to the RigidBody, the object moves as expected. When I try to move the object forward with RigidBody::addForce() the object seems to ignore its orientation. Why does this ignore the objects orientation and how should I apply a force in the direction the object is facing?

Link to comment

RigidBody::addForce(vec3 force) function gets its direction from the parameter "force". To apply a force in the direction of a node of a certain magnitude you can do;

 

float forceMagnitude = 10;
rBody.addForce(node.getDirection() * forceMagnitude);

Link to comment

Hi Linus,

 

As Carl mentioned, force is a vector value so you can apply it to the body in any direction. If you want to move your object forward then you should get object direction and use it.

Link to comment

Thank you both.

 

I tried the code Carl mentioned, but for some reason the node always returns vec3(0,0,0) as it's direction.

I have no idea what could be causing this. I know for sure I'm calling getDirection on the correct node, because other functions do affect the node.

Link to comment

Can't say I overly use the GetDirection function so perhaps it is bugged? Try this as an alternative

 

#include <core/scripts/utils.h>
void AddForceInNodeDir(Node node, RigidBody rBody, float forceMagnitude)
{
vec3 p, d;
Unigine::decomposeTransformDirection(node.getTransform(),p,d);
rBody.addForce(d * forceMagnitude);
}

Link to comment

Code:

vec3 p, d;
Unigine::decomposeTransformDirection(node.getTransform(),p,d);
log.message("Node at rotation: %s\nnode.getDirection: %s\nUnigine::decomposeTransformDirection: %s\n\n",
 typeinfo(node.getRotation()), typeinfo(node.getDirection()), typeinfo(d));
node.setTransform(node.getTransform() * rotateX(90));
Unigine::decomposeTransformDirection(node.getTransform(),p,d);
log.message("Node at rotation: %s\nnode.getDirection: %s\nUnigine::decomposeTransformDirection: %s\n\n",
 typeinfo(node.getRotation()), typeinfo(node.getDirection()), typeinfo(d));
node.setTransform(node.getTransform() * rotateY(90));
Unigine::decomposeTransformDirection(node.getTransform(),p,d);
log.message("Node at rotation: %s\nnode.getDirection: %s\nUnigine::decomposeTransformDirection: %s\n\n",
 typeinfo(node.getRotation()), typeinfo(node.getDirection()), typeinfo(d));

 

Output:

09:16:52 Node at rotation: quat: 0 0 0 1

09:16:52 node.getDirection: vec3: -0 -0 -1

09:16:52 Unigine::decomposeTransformDirection: vec3: -0 -0 -1

09:16:52

09:16:52 Node at rotation: quat: 0.707107 0 0 0.707107

09:16:52 node.getDirection: vec3: -0 1 4.37114e-008

09:16:52 Unigine::decomposeTransformDirection: vec3: -0 1 4.37114e-008

09:16:52

09:16:52 Node at rotation: quat: 0.5 0.5 0.5 0.5

09:16:52 node.getDirection: vec3: -1 -4.37114e-008 -1.91069e-015

09:16:52 Unigine::decomposeTransformDirection: vec3: -1 -4.37114e-008 -1.91069e-015

 

getDirection() works fine as shown in this test case. You will have to provide a small sample of the problem to get further help as I can't reproduce a zero direction.

Link to comment

Thanks Carl.

 

I made an embarrasing mistake.

The reason why I thought the direction was set to 0 was because I because I was printing the values with printf tag '%d'.

I got the object to move in the right direction in an isolated test. :D

 

edit: The reason I didn't see any difference in the direction of movement after rotating the object was because the object was facing downward and I was rotating the object around the (world) z-axis.

Edited by Linus
Link to comment
×
×
  • Create New...