Home › Forum › SOFA › Programming with SOFA › [SOLVED] Translating mechanical objects (not working c++)
Tagged: 64_bits, SOFA_1706, Windows_10
- This topic has 10 replies, 6 voices, and was last updated 6 years, 6 months ago by
BEK.
-
AuthorPosts
-
13 September 2017 at 02:07 #9976
OEA
BlockedHello, I would like some help. I am trying to apply translations to a mechanical model that was loaded by a .scn file, I fetch the object and confirm i have a valid point but the applyScale() function does nothing, as you can see from the code i confirm i have the correct object and i print the scale after modification which returns(1,1,1)
sofa::simulation::Node::SPtr groot = sofa::simulation::getSimulation()->load("./scene.scn"); // print out scene tree, confirm mechnical model existsance groot->printComponents(); groot->getTreeNode(std::string("TT"))->printComponents(); sofa::simulation::Node* lol = groot->getTreeNode(std::string("TT")); //get pointer to mechnical model and print its name to confirm validity sofa::core::behavior::BaseMechanicalState* liver = &lol->mechanicalState[0]; cout << liver->getName(); cout << liver; //if(liver) liver->applyScale(0.7, 9.7, 0.7); // prints scale, the output is (1,1,1) not above scale cout << liver->getScale(); sofa::simulation::tree::getSimulation()->init(groot.get());
My scene graph file is Tetra2TriangleTopologicalMapping_NoInitialTriangle_option.scn
13 September 2017 at 07:16 #9977OEA
BlockedI have also tried transformation visitor with no sucess
13 September 2017 at 17:02 #9981Hugo
KeymasterHi @omarehaly,
If you access the MechanicalObject of your scene, you should be able to do (before the init() ):
your_mechanicalObj->setTranslation(dx, dy, dz);
This should work. HTH,
Hugo
14 September 2017 at 03:35 #9985OEA
BlockedThank you, that’s what I am doing in the pasted coded. I examined the api and it says that MechanicalState only stores information about the physical object and modifying its values will not affect the calculations. I guess that explained why my code is not working.
How can I easily and dynamically alter the position of an object, this object will not need to be constrained by physics.
SOFA is well documented but the sheer number of data types and templates makes it hard to find the right data type and function to use.
18 September 2017 at 19:07 #9988mslib
BlockedHow can I easily and dynamically alter the position of an object, this object will not need to be constrained by physics.
I have done something similar by loading my object twice. MObj_1 has translation applied to it, and MObj_2 is attached to MObj_1 with springs or fixed attachments in certain parts. MObj_2 will react dynamically to MObj_1’s translation.
11 October 2017 at 16:27 #10065Anna
BlockedHi,
Regarding the source, applyTranslation and applyRotation seem to apply your delta directly to the positions so that should work in your case.
9 January 2018 at 10:41 #10291Hugo
KeymasterAnother way, would be to use Python for interacting during runtime. This is the most straightforward approach on my point of view.
4 February 2018 at 05:32 #10413Weng
BlockedHave you tried the MechanicalStateController in SofaUserInteraction Project? The mechanical state could be modified in the following steps:
(1) get the MechanicalState
using core::behavior::MechanicalState;
mState = dynamic_cast<MechanicalState<DataTypes> *> (this->getContext()->getMechanicalState());(2) get the WriteAccessor of position
helper::WriteAccessor<Data<VecCoord> > x = *this->mState->write(core::VecCoordId::position());
helper::WriteAccessor<Data<VecCoord> > xfree = *this->mState->write(core::VecCoordId::freePosition());(3) modify the center or orientation
x[i].getCenter() = position;
xfree[i].getOrientation() = orientation;Best Regards,
Weng Bin7 February 2018 at 12:04 #10467BEK
BlockedThank you Weng Bin.
I also has similar problems to translate object in C++, your comment was very helpful.For me, I usually access using tags from collision models.
LIKE this,<PointModel tags="Tool"/>
this code is in the child node.and tried to move this collision model to certain positions using MechanicalObject.
But it doesn’t works.. (looks like back to the initial position)
Should I access parent node to move object?12 February 2018 at 11:34 #10475Hugo
KeymasterHi Bek,
If you use a mapping, mapping the collision (slave) onto the mechanical model (master), and you change the position of the collision model, the mapping will overwrite the position since the master representation is the mechanical model.
Does this make sense?Are the meshes used for the collision and the mechanics the same mesh?
Could you send us more details about your scene and the code written to move the collision model?Best,
Hugo
21 February 2018 at 03:26 #10554BEK
Blockedsorry for late reply.
Thank you Hugo.
As you said, because of mapping, I should move mechanical model instead of collision model.
I solve that problems using BaseMechanicalState.
Thank you for your help!
Best,BEK
-
AuthorPosts
- You must be logged in to reply to this topic.