moving object away from camera (or model)?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
boredom

moving object away from camera (or model)?

Post by boredom »

hi,

i was surprised to see that there doesn't seem to be a function for simply moving an object "forward" (that is, in the direction it's facing - in my case, a bullet away from the camera). can anyone help? thanks!
Gorgon Zola
Posts: 118
Joined: Thu Sep 18, 2003 10:05 pm
Location: switzerland

Post by Gorgon Zola »

direction away from camera:
vector3df vAway=camer->getTarget()-camera->getPosition();

if you want to have away direction from a normal scenenode you'll have to
define a away/forward vector in the model coordinate system and transform it with the transformation matrix of the node.

cheers
Guest

Post by Guest »

thanks very much for the reply, but i'm still having problems getting the projectiles to move correctly. since it's easiest (with this engine) to move objects relative to their parent, i was thinking maybe an array of "dummy" nodes would work...that is, have them attached to the camera, and the projectiles attached to the dummy. then when the user shoots, detach the dummy so it stays where it was fired from, allowing me to simply move the projectile away from it. i've tried for awhile now, though, and i can't get it to work...any help is appreciated!
WhytWulf
Posts: 51
Joined: Mon Sep 08, 2003 11:14 am
Location: Australia, Tamworth, NSW
Contact:

Post by WhytWulf »

Another method would be to store the firing origin and use that as the start point of the line.

EG:
User presses shoot
storecurrent position of camera, and cameratarget
move projectile along the stored line coords


I think that would work..

Code: Select all

cameraline.start = camera->getPosition();
cameraline.end = cameraline.start + (camera->getTarget()->cameraline.start).normalize() * 1000.0f;
....
This is what I use to store the position when I want to switch between camera locations..
Project Admin -
http://shadowrunonline.sourceforge.net

------------------------------------
There are 10 kinds of people, those that understand binary and those that don't.
Gorgon Zola
Posts: 118
Joined: Thu Sep 18, 2003 10:05 pm
Location: switzerland

Post by Gorgon Zola »

Niko has implemented a shooting function in his TechDemo
just look at the function

Code: Select all

CDemo::shoot();
cool, he uses animators to make the movement.

cheers
Gorgon Z.
boredom

Post by boredom »

thanks for all the help...i'm now using the code from the techdemo (with animators), and now i'm just trying to tweak it by having the projectiles shoot from the gun model and not the middle of the screen...from what i've read,

Code: Select all

static scene::ISceneNode *dummy = smgr->addTestSceneNode(1.0f,camera,-1,core::vector3df(10.0f,4.0f,1.0f));
should create a node that moves/rotates with the camera, and stays 10 units to the right, 4 units up, and 1 unit in front of the camera. it seems to be doing this, but when i change the beginning of the line i want the bullet to follow from the camera's position to the dummy node's position:

Code: Select all

core::vector3df start = dummy->getPosition();
core::vector3df end = (camera->getTarget() - start);
end.normalize();
end = start + (end * camera->getFarValue());
the bullets actually fly from the place i started on the level at the camera ( no matter where i go). is this a bug? it seems like strange behavior to me..

thanks again!
Gorgon Zola
Posts: 118
Joined: Thu Sep 18, 2003 10:05 pm
Location: switzerland

Post by Gorgon Zola »

I reckon the problem is that getPosition returns the relative position to the parent node ( the camera). if you take the position from the camera then the relative position is equal to the absolute one, because your camera is located at the root of the scenetree. You might try to use

Code: Select all

start=dummy->getAbsolutePosition();
dir=(camera->getTarget() - camera->getPosition()).normalize();
end=start + dir * camera->getFarValue();
cheers
GZ
t

Post by t »

Gorgon Zola wrote:direction away from camera:
vector3df vAway=camer->getTarget()-camera->getPosition();

if you want to have away direction from a normal scenenode you'll have to
define a away/forward vector in the model coordinate system and transform it with the transformation matrix of the node.

cheers
Could anyone explain how to do this?

How do you
1. Define away/forward vector. I presume it's something like vector3df.set(0,0, -100) (assuming away is -100 units away on the z axis)?

2. transform it with the transformation matrix of the node? This I have no clue to how to do. Add the vector to the object's GetPosition ()?

Thanks
Gorgon Zola
Posts: 118
Joined: Thu Sep 18, 2003 10:05 pm
Location: switzerland

Post by Gorgon Zola »

t wrote: Could anyone explain how to do this?
Sorry if I was a little vague, well, here's a more detailed explenation:
t wrote: 1. Define away/forward vector. I presume it's something like vector3df.set(0,0, -100) (assuming away is -100 units away on the z axis)?
defining the forward vector for your model is independent of irrlicht. you'll have to remember it some where in your game's Entity class:

Code: Select all

class IEntity {
public: 
 ISceneNode theNode;

 vector3df forward;

// forward points to the direction where Your game character is looking
// I presume that the mesh of the character is initially facing towards 
// position (0,0,-1) , 'looking into the camera'

 vector3df getForwardDirection();

};
t wrote: 2. transform it with the transformation matrix of the node? This I have no clue to how to do. Add the vector to the object's GetPosition ()?
We want to know the direction where the character is facing to. To optain this, we'll have to transform the vector 'forward' the same way the scenenode that represents the character was transformed.

Code: Select all

vector3df IEntity::getForwardDirection(){
  core::matrix4 mat=theNode->AbsoluteTransformation();
  return mat.translateVect(forward);
}
If all your models are facing initially towards the same direction you could forgeet about the forward vector definition in the class and just replase it in the method 'getForwardDirection' with the hard coded vector.

hope this is clearer :wink:

cheers
gorgon zola
t

Post by t »

Thanks for the response!

What is the very final step?

Also, in

Code: Select all

vector3df IEntity::getForwardDirection(){ 
  core::matrix4 mat=theNode->AbsoluteTransformation(); 
  return mat.translateVect(forward); 
} 
it is returning a matrix but the function is defined to return a vector3df. Which should it return?

And finally, how do I apply this returned value to the entity?

Something like thiS: ?

Code: Select all

pos = pos + getForwardDirection(shipnode); 		
shipnode->setPosition(pos);
[/code]
Gorgon Zola
Posts: 118
Joined: Thu Sep 18, 2003 10:05 pm
Location: switzerland

Post by Gorgon Zola »

Sorry, some time I should really get some sleep :cry:

the function should return a vector, and the matrix should transform it and not 'translate' , ok the code should look something like this....

Code: Select all

vector3df IEntity::getForwardDirection(){ 
  matrix4 mat=theNode->getAbsoluteTransformation();
  vector3df out;
  mat.transformVect(pos,out);
  return out;
} 
of course this is not optimal, because generating a new vector every time
you want to update the node position slows things down.

your last step look good.
Post Reply