Damping dependency of framerate

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Damping dependency of framerate

Postby Julio Jerez » Fri Jul 17, 2009 8:10 am

this area += dp1 * dp0;

is a cross product

area += crossproduct (dp1, dp0);
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Damping dependency of framerate

Postby kallaspriit » Fri Jul 17, 2009 8:40 am

You are right about the cross-product, I though that this was how its overloaded in OgreNewt too. Even after changing this, I still got wierd values until I changed
Ogre::Vector3 p1(faceVertices[1 * 3 + 1], faceVertices[1 * 3 + 1], faceVertices[1 * 3 + 2]);
to
Ogre::Vector3 p1(faceVertices[1 * 3 + 0], faceVertices[1 * 3 + 1], faceVertices[1 * 3 + 2]);
I guess this was a typo :P

Anyway it seems to work great now, getting values from 3 when going straight to about 13 when grabbing the vehicle up from the roof. I'll try to use this to create a somewhat realistic wind drag efect, I'll let people know how it turns out.

Thanks alot, NGD is really great :D
kallaspriit
 
Posts: 216
Joined: Sun Aug 14, 2005 6:31 pm

Re: Damping dependency of framerate

Postby JernejL » Fri Jul 17, 2009 12:09 pm

kallaspriit wrote:Convex collision, does it matter?


well, if it was a convex compound collision or something similar, then double faces into each direction would count as double - it would likely not work properly with "holes".
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Damping dependency of framerate

Postby kallaspriit » Fri Jul 17, 2009 2:10 pm

You are correct but yeah, just convex :P
kallaspriit
 
Posts: 216
Joined: Sun Aug 14, 2005 6:31 pm

Re: Damping dependency of framerate

Postby JernejL » Fri Jul 17, 2009 2:23 pm

okay in that case it's ok, if you want convex you'll have to calculate the sillhuette tho.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Damping dependency of framerate

Postby kallaspriit » Mon Nov 30, 2009 9:58 am

I wanted to report my success using the mesh area in the drag equation that produces pretty nice results, it even gets the terminal velocity correct. The mesh area calculator returns the area twice the size it really is, is it because it also adds the faces on the opposite side of the mesh? For example, it calculates the area of a box 5m wide and deep as 50 when it should be 25. Is there a way around it, right now I just divide the result with two but I guess that is not always correct.

Here's my code:

MeshAreaCalculator.h
Code: Select all
#ifndef MESHAREACALCULATOR_H
#define MESHAREACALCULATOR_H

#include <OgreNewt.h>
#include <Ogre.h>

class MeshAreaCalculator
{
public:
    static float getAreaOf(OgreNewt::Body* body, Ogre::Vector3 dir);
    static float getAreaAgainstWind(OgreNewt::Body* body);

    float calculateArea(OgreNewt::Body* body, Ogre::Vector3 dir);
    Ogre::Vector3 getDir() { return direction; };
    void addArea(float sub) { area += sub; };

private:
    static void calculateArea(void* userData, int vertexCount, const float* faceVertices, int id);

    Ogre::Vector3 direction;
    float area;

};

#endif // MESHAREACALCULATOR_H


MeshAreaCalculator.cpp
Code: Select all
#include "MeshAreaCalculator.h"

float MeshAreaCalculator::calculateArea(OgreNewt::Body* body, Ogre::Vector3 dir)
{
    direction = dir;

    float matrix[16];
    NewtonBodyGetMatrix(body->getNewtonBody(), &matrix[0]);

    float squaredMagnitude = direction.squaredLength();

    if(squaredMagnitude > 0.001f)
    {
        direction.normalise();
        NewtonCollisionForEachPolygonDo(NewtonBodyGetCollision(body->getNewtonBody()), &matrix[0], calculateArea, this);
    }

    return area / 2.0f; // area calculated is twice as big
}

float MeshAreaCalculator::getAreaOf(OgreNewt::Body* body, Ogre::Vector3 dir)
{
    MeshAreaCalculator* calc = new MeshAreaCalculator;

    float area = calc->calculateArea(body, dir);

    delete calc;

    return area;
}

float MeshAreaCalculator::getAreaAgainstWind(OgreNewt::Body* body)
{
    return getAreaOf(body, body->getVelocity());
}

void MeshAreaCalculator::calculateArea(void* userData, int vertexCount, const float* faceVertices, int id)
{
    Ogre::Vector3 area(0.0f, 0.0f, 0.0f);
    Ogre::Vector3 p0(faceVertices[0 * 3 + 0], faceVertices[0 * 3 + 1], faceVertices[0 * 3 + 2]);
    Ogre::Vector3 p1(faceVertices[1 * 3 + 0], faceVertices[1 * 3 + 1], faceVertices[1 * 3 + 2]);

    Ogre::Vector3 dp0(p1 - p0);

    for(int i = 2; i < vertexCount; i++)
    {
        Ogre::Vector3 p1(faceVertices[i * 3 + 0], faceVertices[i * 3 + 1], faceVertices[i * 3 + 2]);
        Ogre::Vector3 dp1(p1 - p0);

        //area += dp1 * dp0;
        area += dp1.crossProduct(dp0);
        dp0 = dp1;
    }

    MeshAreaCalculator* areaInfo = (MeshAreaCalculator*)userData;

    Ogre::Real value = area.dotProduct(areaInfo->getDir());

    if(value > 0.0f)
    {
        areaInfo->addArea(value);
    }
}


ForceCallback.cpp
Code: Select all
void ForceCallback::forceCallback(OgreNewt::Body* body, float timeStep, int threadIndex)
{
   Ogre::Vector3 windVelocity(0.0f, 0.0f, 0.0f);
   Ogre::Real mass;
   Ogre::Vector3 inertia;

   body->getMassMatrix(mass, inertia);

   Ogre::Real fluidMassDensity = 1.293f;
   Ogre::Vector3 objectVelocity = body->getVelocity() - windVelocity;
   Ogre::Real dragCoefficent = 0.5f;
   Ogre::Real velocityMagnitude = objectVelocity.normalise();
   Ogre::Real area = MeshAreaCalculator::getAreaOf(body, objectVelocity);

   // drag formula
   Ogre::Real dragMagnitude = (fluidMassDensity * powf(velocityMagnitude, 2) * dragCoefficent * area) / 2.0f;

   Ogre::Vector3 gravityForce(0.0f, -9.8 * mass, 0.0f);
   Ogre::Vector3 dragForce = dragMagnitude * objectVelocity; // velocity is normalised

   body->addForce(gravityForce - dragForce);
}


Is what I'm doing correct and what about the double area problem?
kallaspriit
 
Posts: 216
Joined: Sun Aug 14, 2005 6:31 pm

Re: Damping dependency of framerate

Postby Julio Jerez » Mon Nov 30, 2009 10:42 am

The double area problem is bacause the area of a triangle is proportional to the cross product, not equal.
It turns out that the proportinality factor is 0.5 here is the proff.
say you have a triangle of dimention A, B , C
the cross produc of that triinlage is given by

cross (A, B) = mag(A) * mag(B) * sin (angle bewteen A and B)

but it happens that B * sin (angle bewteen A and B) is the perpendicular of vertoc B over Vector A

so the Magnitud of the Cross product is just the Area if the axis aligned Box of the triangle.
so if you divide that rectangle by two you get the Area of the triangle forming the box.
and that will be:

area = mag(a) * [(0.5) mag(B) * sin (angle between A and B)] = 0.5 * cross (A, B)
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron