problem with entity movement

Posted By: V_Sola

problem with entity movement - 02/06/09 22:55

Hi guys,

I want my model to move on an certain key. But up until now after i pressed for example Z the model continues moving.
So how could I tell the engine: when I am holding W - Move the model with a regular speed until I stop holding ? that's my Code:

till now


////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>


////////////////////////////////////////////////////////////////////

ENTITY* wizard;

////////////////////////////////////////////////////////////////////



function move_up()
{
wizard.z += 5; // addiere jedes mal, wenn diese Funktion aufgerufen wird 5 Quants auf die Höhe von wizard
}
function move_down()
{
wizard.z -= 5;
}
function move_ahead()
{
while(1)
{
wizard.x += 5*time_step*;
wait(1);
}
}

function move_back()
{
while(1)
{
wizard.x += -5* time_step;
wait(1);
}
}
function main()
{
//video_mode = 7; // 800x600 pixels
video_switch (8,32,1);
level_load ("Drohne_Levelc.wmb");
on_u = move_up;
on_i = move_down;
on_z = move_ahead;
on_y = move_back;

}
action wizard_with_pointer()
{
wizard = my;
my.ambient = 100;
}
action wizard_simple()
{
my.ambient = 100;
}

P.S does it gave any value for a model's speed?

Posted By: er_willy

Re: problem with entity movement - 02/07/09 06:28

function move_ahead()
{
wizard.x += 5*time_step*;
}


...
while(1)
{
on_u = move_up;
on_i = move_down;
on_z = move_ahead;
on_y = move_back;
wait(1);
}
Posted By: heinekenbottle

Re: problem with entity movement - 02/07/09 06:57

A much more concise method would be using the key_XX variables (where XX is the key being pressed). These variables are 0 if not pressed, 1 if pressed and can be used in any equation. They are also predefined.

So we can take move_up, move_down, move_ahead and move_back and condense them into shorter lines of code:
Code:
action wizard_with_pointer()
{
   my.ambient = 100;
   while(1)
   {
      my.x = 5 * (key_z - key_y) * time_step;
      my.z = 5 * (key_i - key_u) * time_step;
      wait(1);
   }
}


The way it works is this:

Key Z is pressed, Key Y is not pressed:
my.x = 5 * (1 - 0) * time_step = 5 * 1 * time_step = 5 * time_step
Key Y is pressed, Key Z is not pressed:
my.x = 5 * (0 - 1) * time_step = 5 * -1 * time_step = -5 * time_step
Both are pressed:
my.x = 5 * (1 - 1) * time_step = 5 * 0 * time_step = 0
Neither are pressed
my.x = 5 * (0 - 0) * time_step = 5 * 0 * time_step = 0

This removes the need for those 4 functions and the four on_key commands in function main.
Posted By: EvilSOB

Re: problem with entity movement - 02/07/09 12:10

Hey heinekenbottle, I think you done a typing error, shouldnt it be as follows? Note the '+=' instead of just '='.
Code:
while(1)
   {
      my.x += 5 * (key_z - key_y) * time_step;
      my.z += 5 * (key_i - key_u) * time_step;
      wait(1);
   }

Anyway, another way to look at it is like so (and a bit more flexible IMHO).
Code:
function move_up() 
{ 
   //Sorry I cant read this comment so I'll leave up & down unchanged
   wizard.z += 5; // addiere jedes mal, wenn diese Funktion aufgerufen wird 5 Quants auf die Höhe von wizard 
} 

function move_down()
{
   wizard.z -= 5;
}

function move_ahead()
{   
   wizard.x += 5*time_step*; 
}

function move_back()
{
   wizard.x += -5* time_step; 
}


function main()
{
   //video_mode = 7; // 800x600 pixels
   video_switch (8,32,1);
   level_load ("Drohne_Levelc.wmb");
   //   
   while(1)
   {
      if(key_u == 1)  move_up();
      if(key_j == 1)  move_down();
      if(key_z == 1)  move_ahead();
      if(key_y == 1)  move_back();
      wait(1);
   }
}

Posted By: V_Sola

Re: problem with entity movement - 02/07/09 14:17

thx everyone, my model is moving right.

how could I let the camera be constantly behind my model (3rd Person View) ?
Posted By: Cowabanga

Re: problem with entity movement - 02/07/09 14:25

Put this code:

Code:
camera.x = wizard.x - 15; // change it like you want.
camera.y = wizard.y; // don't touch it!
camera.z = wizard.z + 5; // change it like you want.

Posted By: V_Sola

Re: problem with entity movement - 02/07/09 14:37

Nice thx,
Now my code looks like this:

////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>


////////////////////////////////////////////////////////////////////

ENTITY* wizard;

////////////////////////////////////////////////////////////////////


}
function main()
{
//video_mode = 7; // 800x600 pixels
video_switch (8,32,1);
level_load ("Drohne_Levelc.wmb");


}a

action wizard_with_pointer()
{
wizard = my;
my.ambient = 100;
while(1)
{

c_move(my, nullvector, nullvector, GLIDE );
my.x += 5 * (key_a - key_d) * time_step;
my.y += 5 * (key_s - key_w) * time_step;
my.pan += -50 * mouse_force.x * time_step;

camera.x = my.x;
camera.y = my.y + 100;
camera.z = my.z + 15;

camera.pan = my.pan + 270;
camera.tilt = - 12;

c_move(my, nullvector, nullvector, GLIDE );
wait(1);
}
}

action wizard_simple()
{
my.ambient = 100;
}

My Problem is that the model moves absolutly and not relative.
Does anyone have a solution which doesn't need to change my code completly?
Posted By: heinekenbottle

Re: problem with entity movement - 02/07/09 15:41

Code:
c_move(my, nullvector, nullvector, GLIDE );
my.x += 5 * (key_a - key_d) * time_step;
my.y += 5 * (key_s - key_w) * time_step;
my.pan += -50 * mouse_force.x * time_step;


You have c_move in here, but you aren't using it.

c_move has two vectors, the first one is relative distance, the second it absolute.

Relative will make the model move in relation to his orientation and origin, so all we need to do is put all of this into c_move:
Code:
action wizard_with_pointer()
{
   VECTOR speed;
   //...rest of code
   while(1)
   {
      my.pan = -50 * mouse_force.x * time_step;
      speed.x = 5 * (key_a - key_d) * time_step;
      speed.y = 5 * (key_s - key_w) * time_step;
      c_move(my,speed,nullvector,IGNORE_PASSABLE | GLIDE);
      //...rest of code

Posted By: V_Sola

Re: problem with entity movement - 02/07/09 20:24

now my Code looks like this:

(...)
action wizard_with_pointer()
{
wizard = my;
my.ambient = 100;
//VECTOR speed;

while(1)
{

my.pan += -50 * mouse_force.x * time_step;

my.x += 5 * (key_a - key_d) * time_step;
my.y += 5 * (key_s - key_w) * time_step;

c_move (my,my,nullvector,IGNORE_PASSABLE | GLIDE);

camera.x = my.x;
camera.y = my.y + 150;
camera.z = my.z + 15;

camera.pan = my.pan + 270;
camera.tilt = - 12;


wait(1);
}
}
(...)
but unfortunately it doesn't work
what's my mistake?
Posted By: heinekenbottle

Re: problem with entity movement - 02/07/09 23:12


Code:
c_move (my,[b]my[/b],nullvector,IGNORE_PASSABLE | GLIDE);


You placed a pointer where there should be a vector.

"my" is a pointer.

In these two lines:

Code:
my.x += 5 * (key_a - key_d) * time_step;
my.y += 5 * (key_s - key_w) * time_step;


Replace "my" with a vector and put that vector in the c_move command.
Posted By: V_Sola

Re: problem with entity movement - 02/08/09 13:41

now it looks like this but didn't work:

(...)
VECTOR speed;

(...)

speed.x += 5 * (key_a - key_d) * time_step;
speed.y += 5 * (key_s - key_w) * time_step;

c_move (my,speed,nullvector,IGNORE_PASSABLE | GLIDE)

(...)
© 2024 lite-C Forums