1 registered members (AndrewAMD),
1,213
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Writing custom face-object function, need help
#215382
07/10/08 21:28
07/10/08 21:28
|
Joined: Mar 2007
Posts: 677 0x00000USA
MrCode
OP
User
|
OP
User
Joined: Mar 2007
Posts: 677
0x00000USA
|
Hi, all. I've decided to write a custom utility function used for facing one entity to another (for instance the camera to another object). I had thought I had all the math worked out (as for using trigonometry to find the angles and whatnot), but apparently I haven't got it all figured out as far as the code is concerned. Here's the function:
void FaceObject(ENTITY* ent1,ENTITY* ent2)
{
distz= ent2.z - ent1.z; //these are the distance variables.
disty= ent2.y - ent1.y; //as you can see,these find
distx= ent2.x - ent1.x; //the distance between the x,y,and z components.
distxy= sqrt(pow(distx,2) + pow(disty,2)); //finding general distance to object
/*I prefer not to use vec_dist() here because
*I have already found the z distance above,
*and I don't need more than the x and y in
*this case.*/
ent1.pan= atan(disty/distx); //using arc tangent to find pan
ent1.tilt= atan(distz/distxy); //and tilt angles.
}
Can anyone tell me what I'm doing wrong here? Maybe I should be using atanv()? Well, I suppose it would help if I explained a bit more in detail, . I run it, and the camera is just sitting there, doing nothing, facing straight. I have the object that I want the camera to face at an obscure position from the camera, so I know it's not just that the effect isn't visible.
Last edited by MrCode; 07/11/08 23:50.
void main()
{
cout << "I am MrCode,";
cout << "hear me roar!";
system("PAUSE");
}
|
|
|
Re: Writing custom face-object function, need help
[Re: MrCode]
#215697
07/12/08 20:57
07/12/08 20:57
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
why you dont use vec_to_angle? vec_to_angle(camera.pan,TARGET.x);
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Writing custom face-object function, need help
[Re: WretchedSid]
#215704
07/12/08 21:27
07/12/08 21:27
|
Joined: Oct 2007
Posts: 5,210 İstanbul, Turkey
Quad
Senior Expert
|
Senior Expert
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
|
this faces first one to second: void ent_face_first(ENTITY* first,ENTITY* second){
if(first == NULL || second == NULL){
printf("Null object in ent_face_first.");
}
VECTOR vec;
vec_set(vec.x,second.x);
ANGLE angl;
vec_set(vec.x,vec_sub(vec.x,first.x));
vec.z = 0;
vec_to_angle(angl.pan,vec.x);
first.pan = angl.pan;
} this faces two objects to each other:
void ent_face_both(ENTITY* first,ENTITY* second){
if(first == NULL || second == NULL){
printf("Null object in ent_face_both.");
}
VECTOR vec;
ANGLE angl;
vec_set(vec.x,second.x);
vec_set(vec.x,vec_sub(vec.x,first.x));
vec.z = 0;
vec_to_angle(angl.pan,vec.x);
first.pan = angl.pan;
vec_set(vec.x,first.x);
vec_set(vec.x,vec_sub(vec.x,second.x));
vec_to_angle(angl.pan,vec.x);
second.pan = angl.pan;
} a lil exp. i did when i saw the thread: http://rapidshare.de/files/39985587/face.zip.html-move the first object with mouse, uncomment ent_face_first and comment ent_face_both to see the other function- edit: vec.z = 0; lines are NOT needed since i used first.pan = angl.pan;
Last edited by Quadraxas; 07/12/08 22:24.
3333333333
|
|
|
Re: Writing custom face-object function, need help
[Re: MrCode]
#216094
07/15/08 04:06
07/15/08 04:06
|
Joined: Mar 2007
Posts: 677 0x00000USA
MrCode
OP
User
|
OP
User
Joined: Mar 2007
Posts: 677
0x00000USA
|
btw, it's not because I'm forgetting to call it in a while loop. It's being called in a loop, it's just not working. Here's the whole code:
#include <acknex.h>
#include <default.c>
float distx,disty,distz,distxy;
void FaceObject(ENTITY* ent1,ENTITY* ent2)
{
distz= ent2.z - ent1.z; //these are the distance variables.
disty= ent2.y - ent1.y; //as you can see,these find
distx= ent2.x - ent1.x; //the distance between the x,y,and z components.
distxy= sqrt(pow(distx,2) + pow(disty,2)); //finding general distance to object
/*I prefer not to use vec_dist() here because
*I have already found the z distance above,
*and I don't need more than the x and y in
*this case.*/
ent1.pan= atan(disty/distx); //using arc tangent to find pan
ent1.tilt= atan(distz/distxy); //and tilt angles.
}
void main()
{
video_mode= 8;
video_screen= 1;
level_load("terr_env.wmb");
wait(3);
camera.z= 250;
ENTITY* face_me= ent_create("dummy.mdl",vector(100,100,100),NULL);
while(1)
{
FaceObject(camera,face_me);
face_me.x+= 10 * time_step;
wait(1);
}
}
void main()
{
cout << "I am MrCode,";
cout << "hear me roar!";
system("PAUSE");
}
|
|
|
Re: Writing custom face-object function, need help
[Re: LunaticX]
#216243
07/16/08 00:33
07/16/08 00:33
|
Joined: Mar 2007
Posts: 677 0x00000USA
MrCode
OP
User
|
OP
User
Joined: Mar 2007
Posts: 677
0x00000USA
|
Well, thx, but the first suggestion kinda defeats the purpose of modularity, and the second one won't work, as the compiler complains that "'pan' is not a member of VECTOR". I've tried using void*, but again, "'x' is not a member of VOID. But I think I have a solution. I suppose I could create a dummy entity, and have that be ent1, and have the camera match it's coordinates/angles. Thanks for the replies! EDIT: Success! Well, mostly. It turns in accordance to the position of the test entity, but I can't make it face the test entity exactly. I've tried multiplying the atan()s by different values, and adding angles to them as well, but nothing's working. I guess my estimates were waaaaay off.
Last edited by MrCode; 07/16/08 00:47.
void main()
{
cout << "I am MrCode,";
cout << "hear me roar!";
system("PAUSE");
}
|
|
|
|