variables in while-loop

Posted By: Anonymous

variables in while-loop - 11/06/12 17:19

Hi,

ok here it is:
i have this while-loop inside my player-move-code:
Code:
while(1)
{
abs_movement_x_0 = player.x;
abs_movement_y_0 = player.y;

...code...

c_move(my,vector(player_movevector[0],player_movevector[1],0),nullvector,IGNORE_PASSABLE|IGNORE_SPRITES|GLIDE | IGNORE_PUSH);

...code...

abs_movement_x_1 = player.x;
abs_movement_y_1 = player.y;
//Distanz der Vektoren berechnen
abs_movement_x_1 = abs_movement_x_1 - abs_movement_x_0;
abs_movement_y_1 = abs_movement_y_1 - abs_movement_y_0;
abs_movement_res = sqrt(abs_movement_x_1*abs_movement_x_1 + abs_movement_y_1*abs_movement_y_1);
		
wait(1);
}


the code above works. var abs_movement_res gets updated correctly.
But now i changed the c_move into:
Code:
pXent_move (my, vector(player_movevector[0],player_movevector[1],player_movevector[2]), NULL);


after this, the variable stay always the same it doesnt get updated.
Does anyone know why?

Thanks for any input!
Posted By: Superku

Re: variables in while-loop - 11/06/12 17:33

I am not sure how pXent_move works, esp. when it is executed resp. when the entity gets moved, but it could be that this only happens in physX_run/ at the end of a frame.
Posted By: Anonymous

Re: variables in while-loop - 11/06/12 17:39

hmmm, so what could i do about it??
i thought no matter how the entity gets moved, it just checks for the players x, y coordinates every frames. why would C_move be any different then pXent_move, both just move the entity...
Posted By: Superku

Re: variables in while-loop - 11/06/12 18:16

Hm no, it's not necessary the same as physX is a plug-in that connects physical bodies to Gamestudio's entities. Maybe only the physX object is moved and the entity is placed at its position at the end of the frame. You could try to read the position of the former, not the position of the entity itself.
Posted By: Anonymous

Re: variables in while-loop - 11/07/12 05:35

Fixed it!
I believe the c_move was so slow that it actually caused a delay between the variables above and below the command, while the pXent_move was way faster that the variables stays the same and thus didnt get new values.
I now wrote a new void function where I wrote the variables and just put a Wait(1); instead of the c_move laugh
It may not be the best solution, but it does work!
Code:
while(1)
{
abs_movement_x_0 = player.x;
abs_movement_y_0 = player.y;

wait(1);

abs_movement_x_1 = player.x;
abs_movement_y_1 = player.y;
//Distanz der Vektoren berechnen
abs_movement_x_1 = abs_movement_x_1 - abs_movement_x_0;
abs_movement_y_1 = abs_movement_y_1 - abs_movement_y_0;
abs_movement_res = sqrt(abs_movement_x_1*abs_movement_x_1 + abs_movement_y_1*abs_movement_y_1);
		
wait(1);
}

Posted By: WretchedSid

Re: variables in while-loop - 11/07/12 06:26

Well, now it does exactly what you thought would be the problem...
Also, this is not how Lite-C works, but congrats on fixing the symptoms.
Posted By: Anonymous

Re: variables in while-loop - 11/07/12 09:00

yes, unfortunally thats the only thing i was able to at the moment, fixing the symptoms, but not the cause...
im not that good in coding, and i never will be but if you can give me a better solution and explane it to me how it works iam very thanksful laugh
Posted By: 3dgs_snake

Re: variables in while-loop - 11/07/12 09:45

I think you can achieve the same thing using

  • init old and new position variables
  • set new position
  • calculate delta
  • save current position in old_pos variable


Code:
function main()
{
   var x = 0, y = 0;
   
   // New position
   var abs_movement_x0 = 1;
   var abs_movement_y0 = 1;
   
   // Last position
   var abs_movement_x1 = 1;
   var abs_movement_y1 = 1;
   
   while(1)
   {
      // Set new position
      abs_movement_x0 = x;  
      abs_movement_y0 = y;
      
      ///pXent_move()...............
      
      // Dist (delta = new - old)
      var dx = abs_movement_x0 - abs_movement_x1;
      var dy = abs_movement_y0 - abs_movement_y1;
      
      var length = sqrt(pow(dx, 2) + pow(dy, 2));
      
      // Save Current position for next frame
      abs_movement_x1 = abs_movement_x0;
      abs_movement_y1 = abs_movement_y0;
      
      DEBUG_VAR(length, 10);
      
      // Simulate PhysX update
      var speed = 5;
      x += speed * time_step;
      y += speed * time_step;
      
      wait(1);
   }
}

© 2023 lite-C Forums