Sgw32
17-04-2010 16:29:10
I have a problem. How can I create, for example,a bullet? On shot,I can create an OgreNewt::Body,and set a force callback. Ok. The bullet shoots. When it collides something, what should I use to learn other collision OgreNewt::Body(a player,npcs,obstacles)? Contact Callback?
I read these lines on newton wiki:
NewtonBodyAddForce
Usage
Add the net force applied to a rigid body.
Parameters
const NewtonBody *bodyPtr - pointer to the body to be destroyed.
const dFloat *forcePtr - pointer to an array of 3 floats containing the net force to be applied to the body.
Return
Nothing.
Remarks
This function is only effective when called from NewtonApplyForceAndTorque callback
So how can I add a force to the shooted body?
koirat
17-04-2010 23:25:14
I use contact callback for bullets. What I'm doing is to have another callback inside Body.UserData so i dispatch this collision to bullet object and body that was hit. You have to store value that your body was hit by the bullet and in a forcecallback you can do your stuff.
kallaspriit
24-04-2010 18:37:06
Using bodies is not a great idea for bullets as they are small and fast and will thus likely pass right through some bodies, because between two updates, the body move so much not to even notice the body between. I would use raycasting and apply forces to objects being hit. You can delay the effect (moving raycast) to simulate realistic bullet speed.
koirat
24-04-2010 18:45:01
@kallaspriit you are right.
I forgot to add that I was using something like a rocket or more a cannon ball as a bullet.
Anyway he was asking about ContactCalback.
kallaspriit
24-04-2010 23:25:08
Try OgreNewt::Body::addImpulse() to shoot it and contact callbacks to learn what it hit
Sgw32
26-04-2010 18:57:54
Now I'm doing this at weapon class. In frame_started I'm using raycast,and find a ray body,then apply him a force, but this works incorrect(sometimes works)
I think that mystake is in this functions:
OgreNewt::Body* Shockrifle::get_ray_shoot()
{
Ogre::Vector3 myPos = mPlayerNode->getPosition(); //a position of body
OgreNewt::BasicRaycast shootRay(mWorld,myPos,myPos+get_direction() * 10000); //a ray from position to multiplyed direction
return shootRay.getFirstHit().mBody; //a first hit
}
Ogre::Vector3 Shockrifle::get_direction()
{
Ogre::Quaternion myOrient = mWeaponNode->getParentSceneNode()->getOrientation(); //an orientation of camera
Ogre::Vector3 direction;
direction = myOrient*Vector3::NEGATIVE_UNIT_Z; //camera orientation multiplyed on first camera direction vector
return direction;
}
1)Next,in framestarted
if (shooting)// if mouse button pressed
bod = get_ray_shoot(); // give a ray body
if (bod&&shooting) //if bod present and mouse is pressed
{
vector<PhysObject*> pobj = POs::getSingleton().getObjects(); // all physics scene objects. PhysObject is a simple class that contains both Node and Body
int i;
for (i=0;i!=pobj.size();i++)
{
if (pobj[i]->isThisPO(bod))//we are finding a physobject with correct body
pobj[i]->addForce(get_direction()*10000); //and applying force in physobjects body callback
}
}
2)Physobject calback and addForce
bool PhysObject::isThisPO(OgreNewt::Body* bod2)
{
if (bod2->getName()==bod->getName())// if names of body are equal(my own function add in OgreNewt)
return true;
return false;
}
void PhysObject::addForce(Vector3 force)
{
mForce = force;//set variables to use in callback
applyforce=true;
}
void PhysObject::camera_force_callback( OgreNewt::Body* me )
{
//apply a simple gravity force.
Ogre::Real mass;
Ogre::Vector3 inertia;
me->getMassMatrix(mass, inertia);
Ogre::Vector3 force(0,gravity,0);
force *= mass;
me->addForce( force );
if (applyforce) // if we want to apply a force
{
me->addForce(Vector3(0,100000,0)); //applying force correctly in body callback,if body is static,then callback is not called
applyforce=false;// for applying once
}
}
This code doesn't work,and I think the error is in the raycast! Please,somebody,help!