About Store Forum Documentation Contact
Donations:
178$/mo



Post Reply 
[solved]freezePos got stuck in trigger
Author Message
dziablo Offline
Member

Post: #1
[solved]freezePos got stuck in trigger
Hi, in my game i want to often use freezePos function, but it stops working after first call in reportTrigger function. I made example code based on '11 -Physics/01 - Physics' tutorial. It also often crash on exit.
Most important(imo smile) parts of code.
Code:
Actor ground,
box ,
ball ;
---------
---------
bool Init()
{
---------
---------
Physics.reportTrigger(*TriggerReport);
---------
---------
box .create(Box (0.3 , Vec(0.1, -1, 0)), 0);
ball .create(Ball(0.3 , Vec(2.5, 0, 2.5)), 1);
box.trigger(true);
return true;
}
---------
---------
Flt timer, timer1=0;
void TriggerReport(ActorInfo &trigger, ActorInfo &actor)
{
   timer=Time.curTime();
   if(timer>=timer1+3) //every 3 secs
   {
      timer1=timer;
      if(ball.freezePosX()) ball.freezePosX(false);
      else ball.freezePosX(true); //works only on first triggering
   }
}

and full code:
Code:
/******************************************************************************/
Actor ground,
box ,
ball ;
/******************************************************************************/
void InitPre()
{
EE_INIT();
App.flag=APP_MS_EXCLUSIVE;
}
bool Init()
{
Cam.dist=4;

// create physics
Physics.create(EE_PHYSX_DLL_PATH); // create physics by specifying the path to physx dll's
Physics.reportTrigger(*TriggerReport);
// create actors
ground.create(Box (15, 1, 15, Vec(0.0, -2, 0)), 0); // create ground actor from Box and density=0 (which means it's a static actor - will not move)

box .create(Box (0.3 , Vec(0.1, -1, 0)), 0);
ball .create(Ball(0.3 , Vec(2.5, 0, 2.5)), 1);
box.trigger(true);
return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
bool Update()
{

if(Kb.bp(KB_ESC))return false;
CamHandle(0.1, 10, CAMH_ZOOM|CAMH_ROT);
// physics update
{
Physics.startSimulation(); // start frame simulation

// physics simulation takes some time, so you can do some calculations here
// important : you may not modify actors between Physics.startSimulation() and Physics.stopSimulation()

Physics.stopSimulation(); // get results of frame simulation
}


flt s=3;

// add forces to ball
if(Kb.b(KB_UP ))ball.addForce(Vec( 0, 0, 1)*s);
if(Kb.b(KB_DOWN ))ball.addForce(Vec( 0, 0, -1)*s);
if(Kb.b(KB_LEFT ))ball.addForce(Vec(-1, 0, 0)*s);
if(Kb.b(KB_RIGHT))ball.addForce(Vec( 1, 0, 0)*s);

// add forces to ball according to camera
if(Kb.b(KB_W))ball.addForce( !PointOnPlane(Cam.matrix.z, Vec(0, 1, 0))*s);
if(Kb.b(KB_S))ball.addForce(-!PointOnPlane(Cam.matrix.z, Vec(0, 1, 0))*s);
if(Kb.b(KB_A))ball.addForce(-!PointOnPlane(Cam.matrix.x, Vec(0, 1, 0))*s);
if(Kb.b(KB_D))ball.addForce( !PointOnPlane(Cam.matrix.x, Vec(0, 1, 0))*s);

// add velocity to ball
if(Kb.bp(KB_SPACE))ball.addVel(Vec(0, 3, 0));
return true;
}
/******************************************************************************/
void Draw()
{
D .clear();
Physics.draw (); // draw physical actors
D.text(0, 0.6, S+"time:"+timer1    );
}
/******************************************************************************/
Flt timer, timer1=0;
void TriggerReport(ActorInfo &trigger, ActorInfo &actor)
{
   timer=Time.curTime();
   if(timer>=timer1+3)
   {
      timer1=timer;
      if(ball.freezePosX()) ball.freezePosX(false);
      else ball.freezePosX(true);
   }
}

Anyone can help with that?
and btw: can i make Actor from ActorInfo?
(This post was last modified: 08-07-2013 12:44 PM by dziablo.)
08-06-2013 06:35 PM
Find all posts by this user Quote this message in a reply
Pherael Offline
Member

Post: #2
RE: freezePos got stuck in trigger
You shouldn't do any changes on physics inside TriggerReport

I just tested this and it work fine:
Code:
/******************************************************************************/
Actor ground,
box ,
ball ;
/******************************************************************************/
void InitPre()
{
EE_INIT();
App.flag=APP_MS_EXCLUSIVE;
}
bool Init()
{
Cam.dist=4;

// create physics
Physics.create(EE_PHYSX_DLL_PATH); // create physics by specifying the path to physx dll's
Physics.reportTrigger(*TriggerReport);
// create actors
ground.create(Box (15, 1, 15, Vec(0.0, -2, 0)), 0); // create ground actor from Box and density=0 (which means it's a static actor - will not move)

box .create(Box (0.3 , Vec(0.1, -1, 0)), 0);
ball .create(Ball(0.3 , Vec(2.5, 0, 2.5)), 1);
box.trigger(true);
return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
bool Update()
{

if(Kb.bp(KB_ESC))return false;
CamHandle(0.1, 10, CAMH_ZOOM|CAMH_ROT);
// physics update
{
Physics.startSimulation(); // start frame simulation

// physics simulation takes some time, so you can do some calculations here
// important : you may not modify actors between Physics.startSimulation() and Physics.stopSimulation()

Physics.stopSimulation(); // get results of frame simulation
}
contactIs();

flt s=3;

// add forces to ball
if(Kb.b(KB_UP ))ball.addForce(Vec( 0, 0, 1)*s);
if(Kb.b(KB_DOWN ))ball.addForce(Vec( 0, 0, -1)*s);
if(Kb.b(KB_LEFT ))ball.addForce(Vec(-1, 0, 0)*s);
if(Kb.b(KB_RIGHT))ball.addForce(Vec( 1, 0, 0)*s);

// add forces to ball according to camera
if(Kb.b(KB_W))ball.addForce( !PointOnPlane(Cam.matrix.z, Vec(0, 1, 0))*s);
if(Kb.b(KB_S))ball.addForce(-!PointOnPlane(Cam.matrix.z, Vec(0, 1, 0))*s);
if(Kb.b(KB_A))ball.addForce(-!PointOnPlane(Cam.matrix.x, Vec(0, 1, 0))*s);
if(Kb.b(KB_D))ball.addForce( !PointOnPlane(Cam.matrix.x, Vec(0, 1, 0))*s);

// add velocity to ball
if(Kb.bp(KB_SPACE))ball.addVel(Vec(0, 3, 0));
return true;
}
/******************************************************************************/
void Draw()
{
D .clear();
Physics.draw (); // draw physical actors
D.text(0, 0.6, S+"time:"+timer1    );
}

/******************************************************************************/
Flt timer, timer1=0;

Bool contact = false;
void TriggerReport(ActorInfo &trigger, ActorInfo &actor)
{
   contact = true;
  
}

void contactIs()
{
   if( contact)
   {
       timer=Time.curTime();
   if(timer>=timer1+3)
   {
      timer1=timer;
      if(ball.freezePosX()) ball.freezePosX(false);
      else ball.freezePosX(true);
   }
      contact = false;
   }
}
08-07-2013 08:30 AM
Find all posts by this user Quote this message in a reply
dziablo Offline
Member

Post: #3
RE: freezePos got stuck in trigger
Thank You very much Pherael. Now it works smile
08-07-2013 12:44 PM
Find all posts by this user Quote this message in a reply
Post Reply