Today, I tried to play around Physx and Physx visual debugger and as always, newbies have problems and questions. I'll try to describe my problems as best as I can with my poor english skills.
1) I managed to create a physx scene. added a dynamic actor and manipulated it. I see in Visual Debugger it's motion. It's a standard PxSphereGeometry ball. However, when I add a second ball in the scene, the second one is not visible, but I can see that collision happens. Here's the code and if anyone can point me what's wrong with it I'd be very grateful:
PxMaterial* mMaterial;
mMaterial = mPhysics->createMaterial(0.5f, 0.5f, 0.5f); //static friction, dynamic friction, restitution
if(!mMaterial)
error("createMaterial failed!");
PxVec3 position(0, 50, 0);
PxRigidDynamic* aSphereActor = PxCreateDynamic(*mPhysics, PxTransform(position), PxSphereGeometry(3), *mMaterial, 1.f);
PxRigidDynamic* aTrActor = PxCreateDynamic(*mPhysics, PxTransform(PxVec3(3, 1, 1)), PxSphereGeometry(3), *mMaterial, 1.1f);
if(!aSphereActor)
error("Unable to create sphere actor");
aSphereActor->setMass(1);
aTrActor->setMass(10);
PxRigidStatic* plane = PxCreatePlane(*mPhysics, PxPlane(PxVec3(0,1,0), 0), *mMaterial);
if (!plane)
error("create shape failed!");
mScene->addActor(*plane);
mScene->addActor(*aSphereActor);
mScene->addActor(*aTrActor);
while(true)
{
mScene->simulate(1.0f / 30.0f);
if(!mScene->fetchResults(true))
error("cant fetch result");
Sleep(10);
}
In this scene, aSphereActor collides with aTrActor, but I can't see aTrActor in Visual Debugger, however collision is perfectly visible.
2) Nvidia's documentation is very very poor. It's a torture for newbies like me to find it's way through it. So I wanted to know how can I import a 3d model and add it in the scene. I know there is a Physx plugins for 3ds max, maya etc. Say I have a model exported with this plugin, how can I import it in my app and add it to the scene?
3) During the creation of the scene
sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
, what value should I provide to get a real gravity, the one we have on earth
4) I can assign a mass to an actor, however I don't know in which measurement unit the mass is. For example, if I set aSphereActor->setMass(1); will aSphereActor will be 1kg, gram or what?
Thank you very much everyone. I appreciate your help.
First, I am doing my first Physx project this quarter. (read as, I might be making this up)
1)
You don't check aTrActor's creation, but I don't think that's your problem.
Check if aTrActor in your draw/update callback.
2)
Dunno
3)
-9.81 m/s^2 is the acceleration for Earth's gravity.
I'm guessing that the PxVec3 is the gravity with respect to each axis.
So, PxVec3(0.0, -9.81, 0.0) is no x or z acceleration and -9.81 m/s^2 y acceleration.
4)
The answer to #3 would suggest the units are metric.
You can probably throw it all together in standard, but metric > standard imo.
Just looked at the date, this may not help Davita, but hopefully it'll be of some use to someone.
Related
I am sorry to ask such a basic question about bullet. However, I am having an issue. Here is the setup:
I have a world with no gravity. When I press a key, a 1x1x1 box is created in the center of the world. When I right click, a box of size 0.05*0.05*0.05 is create at the camera position, and is 'shot' in the direction you are looking. Here is where the trouble begins.
When a small cube hits a large cube, the interaction seems wrong. You would expcet a box 1/8000th the size of another to have very little effect. Yet the large cube goes flying, as if it been with a cube of its same size. I assumed it is because I created both objects with the same mass.
To confirm this, I apply an upward force of 1 newton (or whatever unit bullet uses). Both objects accelerate at the same rate.
My code for creating objects is as follows:
btMotionState *state = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btConvexHullShape* shape = new btConvexHullShape();
for(unsigned int i = 0; i < vertices.size(); ++i) {
shape->addPoint(toBt(vertices[i]));
}
shape->setMargin(0.01f);
btScalar mass = 1.f;
btVector3 inertia;
shape->calculateLocalInertia(mass, inertia);
shape->setMargin(0.01f);
btRigidBody::btRigidBodyConstructionInfo ci(mass, state, shape, inertia);
body = new btRigidBody(ci);
I expect that the btScalar mass = 1.f; line is the culprit.
Am I expected to calculate the mass of my objects? This is simple enough for a cube, but what about some weird convex shape? Is there any way I can get bullet to do this for me?
Yes, mass is the culprit. Think about your big box being an empty wooden crate and your small box being a solid cube of iron. Huge difference in size but equal mass. Now your physics seem correct, right?
Yes, you are expected to provide the mass. Whether you calculate it or just state "this box weighs 20kg" is up to you. Actually expecting someone else to calculate the mass for you just makes your problem worse, because you'd have to specify materials, material density, and material distribution of your objects besides its geometry. If you want to go that way there are plenty of other tools available to aid you with such calculations. But I'm sure you agree that just stating some mass through trial and error is easier by far.
Trying to get at least the very simple part 1 of the Lighthouse3D Radar Frustum Culling tutorial to work... and am absolutely baffled that I can't even make that part work in my renderer.
So the first step is: you test if a point is in front the near-plane or behind the far-plane, and early-cull if that's the case. (If not, you'd then perform further tests but I'm stuck just with that first part.)
I use the world-space center (x1y2z3) of a 2x2 cube and have a camera that I can move around and rotate freely. All my vector and matrix stuff must be rather solid as the renderer otherwise works just fine. So here's my take (in Go) of this first part, the simple "Z vs near-or-far" testing:
func (cam *Camera) frustumHasPoint(point *Vec3) bool {
var pc Vec3
v := point.Sub(&cam.Controller.Pos) // point minus camPos
ref := cam.Controller.dir // take a copy of camDir
ref.Z = -ref.Z
ref.Normalize() // camDir was already normalized but anyway...
pc.Z = v.Dot(&ref)
if pc.Z > cam.Perspective.ZFar || pc.Z < cam.Perspective.ZNear {
return false
}
return true
}
Now why do I reverse the Z of ref? Because in the tutorial they write: "Notice that the referential in the figure is not a right hand system (as in OpenGL), because the orientation of Z has been reversed to make the tutorial more intuitive" -- well, in a GL tutorial of course this has the opposite effect...
Well if do reverse Z as above, it culls more than it should about 50% of the time; if I don't, then it "over-culls" about 98% of the time..
Wwhat am I missing?
Resolved. Cause was brain malfunction... the tutorial clearly writes about getting the x/y/z axes first for describing the frustum, somehow I missed that..
I am using OpenGL to create the 3D space.
I have a spaceship which can fire lasers.
Up until now I have had it so that the lasers will simply to deeper into the Z-axis once fired.
But I am attempting to make a proper aiming system with crosshairs so that you can aim and shoot in any direction, but I have not been successfull in trying to update the laser's path.
I have a directional vector based off the lasers end tip and start tip, which is gotten from the aiming.
How should I update the laser's X,Y,Z values (or vectors) properly so that it looks natural?
I think I see.
Let's say you start with the aiming direction as a 3D vector, call it "aimDir". Then in your update loop add all 3 (x, y and z) to the projectile "position". (OK, at the speed of light you wouldn't actually see any movement, but I think I see what you're going for here).
void OnUpdate( float deltaT )
{
// "move" the laser in the aiming direction, scaled by the amount of time elapsed
// since our last update (you probably want another scale factor here to control
// how "fast" the laser appears to move)
Vector3 deltaLaser = deltaT * aimDir; // calc 3d offset for this frame
laserEndpoint += deltaLaser; // add it to the end of the laser
}
then in the render routine draw the laser from the firing point to the new endpoint:
void OnRender()
{
glBegin(GL_LINES);
glVertex3f( gunPos.x, gunPos.Y, gunPos.z );
glVertex3f( laserEndPoint.x, laserEndPoint.y, laserEndPoint.z );
glEnd();
}
I'm taking some liberties because I don't know if you're using glut, sdl or what. But I'm sure you have at least an update function and a render function.
Warning, just drawing a line from the gun to the end of the laser might be disappointing visually, but it will be a critical reference for adding better effects (particle systems, bloom filter, etc.). A quick improvement might be to make the front of the laser (line) a bright color and the back black. And/or make multiple lines like a machine gun. Feel free to experiment ;-)
Also, if the source of the laser is directly in front of the viewer you will just see a dot! So you may want to cheat a bit and fire from just below or to the right of the viewer and then have in fire slightly up or in. Especially if you have one one each side (wing?) that appear to converge as in conventional machine guns.
Hope that's helpful.
I'm really going crazy trying to figure this out, so any help would be really appreciated. I'm trying to hide most of a sprite and show it gradually. This works fine if I only work with rectangles. For example, I found someone's implementation of the "ClippingNode" class and it worked well, namely, this part of the code:
-(void) visit
{
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(clippingRegion.origin.x + positionInPixels_.x, clippingRegion.origin.y + positionInPixels_.y, clippingRegion.size.width, clippingRegion.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
The problem is I need an irregular shape, not just a rectangle. I was hoping I could stack calls to glScissor and create a shape with many smaller rectangles, but unfortunately glScissor only works once (the last time it was called).
It seems that cocos2d doesn't support OpenGLs stencil buffer (does it?) and even if it did, I find OpenGL so hard to understand, I'd still need someone to explain it to me. If I could set a bezier path on the sprite as a mask (which I think you can do in Quartz), that would be great, but it doesn't seem like that's supported.
Please, if anyone has any bit of wisdom here, that'd be great!
Figured it out. You can call glScissor multiple times, you just also need to draw that scissored shape each time:
-(void) visit
{
NSEnumerator *enumerator;
NSValue *val;
CGRect aRegion;
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
enumerator = [regions objectEnumerator];
while ((val = (NSValue *)[enumerator nextObject])) {
aRegion = [val CGRectValue];
glScissor(aRegion.origin.x, aRegion.origin.y,
aRegion.size.width, aRegion.size.height);
[super visit];
}
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
It isn't possible with glScissor, but you could easily achieve this effect using the stencil buffer. Here is the documentation:
http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node117.html
There is also a NeHe tutorial on the stencil buffer, but it is in C++, not Objective C (though it should be easy to translate into whatever application you need):
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=26
EDIT: This is based on the assumption that you want to clip it to some arbitrary shape, for example a star, smiley whatever, instead of just a rectangle.
I have recently been getting into OpenGL/SDL and playing around with objects in 2D/3D Space and as i'm sure most newbies to this area do, have a few queries, about the 'best' way to do something. I quote best, because i'm assuming there isn't a best way, it's personal preference.
So, I have an entity, a simple GL_QUAD, which I want to move around. I have keyboard events setup, i can get the keypress/release events, not a problem.
I have an entity class, which is my GL_QUAD, pseudo implementation....
class Entity
{
void SetVelocity(float x, float y);
}
I then have this event handler code...
if theEvent.Key = UPARROW AND theEvent.State = PRESSED
Entity.SetVelocity(0.0f, -1.0f);
else if theEvent.Key = UPARROW AND theEvent.State = RELEASED
Entity.SetVelocity(0.0f, 0.0f);
My question is, is this the best way to move my entity? This has led me to thinking that we could make it a little more complex, by having a method for adjusting the X/Y Velocity, seperately. As SetVelocity would forget me X velocity if i started moving left? So i could never travel diagonally.
For Example...
class Entity
{
void SetXVelocity(float x);
void SetYVelocity(float y);
}
if theEvent.Key = UPARROW AND theEvent.State = PRESSED
Entity.SetYVelocity(-1.0f);
else if theEvent.Key = UPARROW AND theEvent.State = RELEASED
Entity.SetYVelocity(0.0f);
if theEvent.Key = LEFTARROW AND theEvent.State = PRESSED
Entity.SetXVelocity(-1.0f);
else if theEvent.Key = LEFTARROW AND theEvent.State = RELEASED
Entity.SetXVelocity(0.0f);
This way, if I have an XVelocity and I then press the UPARROW, I will still have my XVelocity, as well as a new YVelocity, thus moving diagonally.
Is there a better way? Am I missing something very simple here?
I am using SDL 1.2, OpenGL, C++. Not sure if there is something in SDL/OpenGL which would help?
Thanks in advance for your answers.
The question is really general since it depends on how you want to model the movement of your objects in your world.
Usually every object has its velocity which is calculated basing on an acceleration and capped to a maximum. This means that a key press alters the acceleration of the object for the specified frame which is then calculated and applied to the current velocity of the object.
This is done through an update phase which goes through all the objects and calculates the velocity change according to the object acceleration. In this way you don't bother to modify the velocity itself but you let your engine to do its calculations depending on the state of every object..
acceleration is applied over a period of time, so in the example by #jack, you would apply an acceleration 10m/s^2 over a time period of one second.
you should also modify your application to make it time based, not frame based.
have a look at this basic game physics introduction, and I would also really have a look at the GameDev.net Physics Tutorials
I assume the way you want movement to work is that you want the player to move only when a key is held.
In short: your solution is fine.
Some potential gotchas to take consideration of: What happens if both left and right is pressed?
Well, what you describe here is a simple finite state machine. You have the different directions in which you can move (plus no movement at all) as the states, and the key-events as transitions. This can usually be implemented quite well using the state pattern, but this is often quite painful in C++ (lots of boilerplate code), and might be over the top for your scenario.
There are of course other ways to represent speed and direction of your entity, e.g. as a 2D-vector (where the length gives the speed). This would enable you to easily represent arbitrary directions and velocities.