Archive 17/01/2023.

Get constraint coordinates

capelenglish

I’m trying to log some coordinates associated with constraints. For example:

plateConstraint->SetWorldPosition(plateNode->GetPosition());

How do I extract these coordinates for logging with URHO3D_LOGINFO?

TheComet

URHO3D_LOGINFOF("The coordinates are: %s", plateConstraint_->GetWorldPostion().ToString());

capelenglish
URHO3D_LOGINFO("The World Position coordinates for the plate constraint are: %s", plateNode->GetPostion().ToString());

returns

[Thu May 31 07:12:08 2018] INFO: The World Position coordinates for the plate constraint are: %s

If I do a QuickWatch on plateNode->GetPostion() VS2017 tells me class “Urho3D::Node” has no member “GetPostion”

What am I missing?

TheComet

It’s URHO3D_LOGINFOF not URHO3D_LOGINFO and you spelled “Position” wrong, i.e. I spelled it wrong in my last response.

capelenglish

The dangers of copy and paste!

So QuickWatch evaluates plateNode->GetWorldPosition().ToString() correctly, but now String.h throws an exception: Exception thrown at 0x0137FB7C (ucrtbased.dll) in Urho3D_Test_d.exe: 0xC0000005: Access violation reading location 0x0000000E.

capelenglish

Looks like it’s a format specification issue. Where the heck can I find those?

TheComet

That particular error means you are dereferencing a nullptr. Most likely, plateNode is NULL. Have you checked with a debugger?

capelenglish

Yes.
Vector3 coords = plateNode->GetWorldPosition();
returns a vector with the correct coordinates.

TheComet

Something is NULL, it’s up to you to debug it.

capelenglish

I’m missing something more fundamental here. I get the same exception when I do this:

for (int i = 0; i < plateNodes_.Size(); ++i)
{
  if (DEBUG) URHO3D_LOGINFOF("Reset plate %s", String(i));
  plateNodes_[i]->SetRotation(Quaternion(0.0f, 0.0f, 0.0f));
}

This compiles fine, but I get a run-time exception. How do I convert int and floats to strings in Urho3D?

Sorry for the stupid questions, I’m a C# guy trying to come up to speed on C++…

Lumak

try

if (DEBUG) URHO3D_LOGINFOF("Reset plate %s", String(i).CString());
capelenglish

@Lumak thanks! To confirm I understand what’s going on: String(i) converts the int to an Urho3D string and then .CString() converts it into a C++ string?

Lumak

Correct, the String is Urho3D::String, and String.CString() returns const char* , see Urho3D/Container/Str.h

Most of string-to-value or value-to-string conversions can be found in: Urho3D/Core/StringUtils.h

TheComet

You’ll want to do

URHO3D_LOGINFOF("Reset plate %d", i);

for integers.

I thought you could pass String to the log macros without having to first convert it to const char* but I was wrong.

capelenglish

Where do I find the documentation for the formats like %s and %d? I assume there are others for floats etc…

TheComet

Great question, we can’t answer it because the documentation is lackluster.

The best I can do is link you do the function that gets called: https://github.com/urho3d/Urho3D/blob/master/Source/Urho3D/Container/Str.cpp#L1087

It appears to support a subset of what printf() supports, d, i, u, l, f, c, s, x and p

This could actually be improved on if we just forwarded everything to vsnprintf()

capelenglish

Along these lines, how do you convert an Urho3D::string to a std::string?

string nodeName = String(hitNode->GetName().CString()); // doesn't work

I need to use boost::starts_with() and it pukes on an Urho3D string.

TheComet

You did it correctly, you just accidentally capitalized “String” so you’re converting it back to an Urho3D string.

std::string nodeName = std::string(hitNode->GetName().CString()); // does work

capelenglish

Now I need to go the other way std::string to Urho3D::string formatted to 2 places.

x = 45.12345
string strHitTimeInSec = boost::str(boost::format("%.2f") % x); // works fine (45.12)
URHO3D_LOGINFOF("hit at %s seconds ", strHitTimeInSec); // doesn't work

It’s not obvious to me how to convert this…

kostik1337

URHO3D_LOGINFOF("hit at %s seconds ", strHitTimeInSec.c_str());

capelenglish

@kostik1337 Thanks! This is just what I was looking for.