A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by rystills » Wed Jun 12, 2019 2:14 pm
Hi,
I've recently switched my game engine from using Bullet Physics to using Newton Dynamics, but I'm having a little trouble converting everything to Newton. I suspect the core issue stems from how I'm syncing rotations between my engine (in which I store rotations in glm 4x4 matrices) and my NewtonBodies, but rather than trouble you with my own bugs, I'd like to get debug rendering operational so I can see for myself what's happening. What is the simplest way to go about debug rendering Newton Dynamics in OpenGL? Ideally I would like to be able to iterate over NewtonCollision shape triangles so that I can render them as a wireframe during my engine's post-render stage. I recall reading about a callback that might allow me to do just that, but I don't quite know how to use it.
Last edited by
rystills on Thu Jun 13, 2019 12:28 am, edited 2 times in total.
-
rystills
-
- Posts: 17
- Joined: Wed Jun 12, 2019 2:01 pm
by Julio Jerez » Wed Jun 12, 2019 2:40 pm
if you doaloaded the SDK there is a file tha constain many unility function tat do that.
void DebugRenderWorldCollision (const NewtonWorld* const world, DEBUG_DRAW_MODE mode);
will iterate over the world, qeuering each body and form each body each collision shape and converting them to glBegin(GL_LINES);
-
Julio Jerez
- Moderator
-
- Posts: 12253
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by Sweenie » Wed Jun 12, 2019 2:49 pm
The function NewtonCollisionForEachPolygonDo is what you need to use.
Mind though that the callback can return a 4-sided polygon for some collision shapes, not just triangles.
But you could just split the 4-sided one in two if you only want triangles i suppose.
http://www.newtondynamics.com/wiki/inde ... hPolygonDo
-
Sweenie
-
- Posts: 498
- Joined: Mon Jan 24, 2005 7:59 am
- Location: Sweden
by Julio Jerez » Wed Jun 12, 2019 3:27 pm
Sweenie wrote:Mind though that the callback can return a 4-sided polygon for some collision shapes, not just triangles.
an n sided, but is a convex so is easy to fan it
-
Julio Jerez
- Moderator
-
- Posts: 12253
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by rystills » Wed Jun 12, 2019 5:25 pm
Brilliant, thank you both!
NewtonCollisionForEachPolygonDo, along with the NewtonCollisionIterator callback, made it quick and easy to render some debug wireframes. As a result, I have spotted some rotation errors and am working to resolve them, however I will elucidate the nature of those errors in a separate thread if necessary.
For future reference, below is the code that I used to achieve debug rendering. The newton debug render callback was copied from newton_dynamics\applications\demosSandbox\sdkDemos\toolBox\DebugDisplay.cpp -> DebugShowGeometryCollision, and the line render method I think is adapted from an old forum post explaining debug rendering in Bullet Physics (though of course, it is just as useful with Newton).
- Code: Select all
void debugDrawLine(const glm::vec3& from, const glm::vec3 &to, const glm::vec3& color) {
// Vertex data
GLfloat points[12];
points[0] = from.x;
points[1] = from.y;
points[2] = from.z;
points[3] = color.x;
points[4] = color.y;
points[5] = color.z;
points[6] = to.x;
points[7] = to.y;
points[8] = to.z;
points[9] = color.x;
points[10] = color.y;
points[11] = color.z;
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glBindVertexArray(0);
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, 2);
glBindVertexArray(0);
}
void debugDrawNewtonCallback(void* const userData, int vertexCount, const dFloat* const faceVertec, int faceId) {
int index = vertexCount - 1;
dVector p0(faceVertec[index * 3 + 0], faceVertec[index * 3 + 1], faceVertec[index * 3 + 2]);
for (int i = 0; i < vertexCount; i++) {
dVector p1(faceVertec[i * 3 + 0], faceVertec[i * 3 + 1], faceVertec[i * 3 + 2]);
debugDrawLine(glm::vec3(GLfloat(p0.m_x), GLfloat(p0.m_y), GLfloat(p0.m_z)), glm::vec3(GLfloat(p1.m_x), GLfloat(p1.m_y), GLfloat(p1.m_z)), glm::vec3(255, 255, 255));
p0 = p1;
}
}
void debugDrawNewton() {
for (int i = 0; i < gameObjects.size(); ++i) {
dMatrix tm;
NewtonBodyGetMatrix(gameObjects[i]->body, &tm[0][0]);
NewtonCollisionForEachPolygonDo(NewtonBodyGetCollision(gameObjects[i]->body), &tm[0][0], debugDrawNewtonCallback, NULL);
}
}
-
rystills
-
- Posts: 17
- Joined: Wed Jun 12, 2019 2:01 pm
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 3 guests