Page 1 of 1

Camera Help

Posted: Tue Aug 20, 2013 2:36 pm
by Zeberpal
Hello zandronum, I need your help/ I have a map idea which I can’t stop think about, because I don’t know how to implement it. I’m a bad coder. But still I tried to solve it myself and now I really give up.
It’s not a tradition to ask for some advanced/qualified help on the forums , but if you mind please help :3
.
The problem is camera. I was trying to make two types of cameras but didn’t succeed in any of those.
.
CASE 1. Chase Camera. A camera which would follow the player from a certain angle and pitch. Now I only know how to make simple chasecam (behind player's back)
Spoiler: with this code. (Open)

Code: Select all

#include "zcommon.acs"

#define C_TID		1000	//Default camera tid
#define MAX_R		128	//Maximum radius (or distance from the player)
#define ADJUST_R	8	//Amount to adjust the camera by
#define VIEW_HEIGHT	41.0	//The approximate hight of the player's view

bool cam_mode[8];		//Variable for turning the camera on or off.
	
Script 1 ENTER
{
	cam_mode[PlayerNumber ()] = ON;
	ACS_ExecuteAlways (3, 0, PlayerNumber ());
}

Script 2 RESPAWN
{
	cam_mode[PlayerNumber ()] = ON;
	ACS_ExecuteAlways (3, 0, PlayerNumber ());
}

Script 3 (int p_num)
{
	int r = MAX_R;
	
	while (cam_mode[p_num] == ON)
	{	
		int a = GetActorAngle (0);
		int p = GetActorPitch (0);
		int x = GetActorX (0);
		int y = GetActorY (0);
		int z = GetActorZ (0) + VIEW_HEIGHT;
		int xyr = r * cos (p) >> 16;
		
		if (!ThingCountName ("ChaseCam", C_TID+p_num))
		{
			while (!Spawn ("ChaseCam", x-cos(a)*xyr, y-sin(a)*xyr, z+sin(p)*r, C_TID+p_num, a >> 8) && r > 0)
			{
				r -= ADJUST_R;
				xyr = cos (p) * r >> 16;
			}
			
			if (ThingCountName ("ChaseCam", C_TID + p_num))
				ChangeCamera (C_TID + p_num, 0, 0);
			else
			{
				cam_mode[p_num] = OFF;
				print (s:"Camera script failed to initialize.");
			}
		}
    else
		{
			while (!SetActorPosition (C_TID+p_num, x-cos(a)*xyr, y-sin(a)*xyr, z+sin(p)*r, 0) && r > 0)
			{
				r -= ADJUST_R;
				xyr = cos (p) * r >> 16;
			}
			
			SetActorAngle (C_TID + p_num, a);
			SetActorPitch (C_TID + p_num, p);
			
			if (r < MAX_R) 
                              r += ADJUST_R;
		}
		
		delay (1);
	}
}     
       
Howether, If I change int a = GetActorAngle (0); to int a = GetActorAngle (“any number here”);
Camera will stick to Player under angle 0° only, which is nearly what I need. But I always need to follow Player under this cinematic angle and pitch for example. I really don’t know how to code/change it. How can I set angle and pitch that I want?
.
I have a
wad file if you feel like trying to help.
.
CASE 2. Aiming camera. A security camera which would aim the player. It’s item-side workable only, so there is no way to provide camera to Player’s TID with ACS :[ Or is there any?

RE: Camera Help

Posted: Tue Aug 20, 2013 2:43 pm
by Klofkac
Case 1: Here could work GetActorAngle(0)+whatever angle you need. 90° is 0.25 .

Case 2:
There is aiming camera actor, but if you want to use this on something different, like monster...
Image
Basically, you need pitch and angle of the vector u.
For that you got already the function VectorAngle. To calculate a,b,c you need to substract player's and camera's x,y,z respectively. u1 is sqrt(a*a+b*b).
Then use VectorAngle(a,b) for angle and VectorAngle(u1,c) for pitch.

EDIT: I have noticed several issues regarding aiming cameras and things with dynamic TID: The camera looks for TID when it is activated. But if nothing has that TID during activation, camera has nothing to aim for.
So, make the camera dormant and activate it after player has their TID.

RE: Camera Help

Posted: Tue Aug 20, 2013 4:04 pm
by Zeberpal
Case 1 worked like a charm! Thanks!

Case 2 is like I got back to high school, where I was lame at maths. It's hard for me to figure out why should I calculate these.
My current understanding is on pic. Image I'm not too deep in this problem for now, but
for now I dont' understand what to start with.
1) Do you mean I need to: Player's |X|Y|Z| - Camera's |X|Y|Z| for ABC?
2) Camera's Y = Cam's height; X = is probably Cam's angle, but what is Z here? Same question about Player's

Thank you very much for your post!

RE: Camera Help

Posted: Tue Aug 20, 2013 4:14 pm
by Klofkac
Zeberpal wrote: Camera's Y = Cam's height; X = is probably Cam's angle, but what is Z here? Same question about Player's
Well, X and Y are coordinates in 2D plane, while Z is the height like in 3D plane (Doom engine is 2.5D)
A, B and C are relative X, Y and Z distances of player from camera.

Here is the code:

Code: Select all

script 1 ( int player_tid, int camera_tid )
{
int x,y,z,a,b,c,u1;
//Assuming camera has fixed position
x = GetActorX(camera_tid);
y = GetActorY(camera_tid);
z = GetActorZ(camera_tid);
While(1){
a=GetActorX(player_tid)-x;
b=GetActorY(player_tid)-y;
c=GetActorZ(player_tid)-z;
u1=FixedSqrt(FixedMul(a,a)+FixedMul(b,b));
SetActorAngle(camera_tid,VectorAngle(a,b));
SetActorPitch(camera_tid,VectorAngle(u,c));
Delay(1);
}}
I have written this without testing so I am not sure if it will work.

There might be other (and simpler) solutions too.

RE: Camera Help

Posted: Tue Aug 20, 2013 4:41 pm
by Zeberpal
Square roots might not work in zan? I define FixedSqrt, but even so he says missing semicolon.

RE: Camera Help

Posted: Tue Aug 20, 2013 4:45 pm
by Klofkac
Zeberpal wrote: Square roots might not work in zan? I define FixedSqrt, but even so he says missing semicolon.
Oh right, that's new in ZDoom 2.7.1
Use this function:

Code: Select all

function int sqrt(int number) 
{
  if (number == 1.0) return 1.0; 
  if (number <= 0) return 0;
  int val = 150.0;
  for (int i=0; i<15; i++) 
    val = (val + FixedDiv(number, val)) >> 1;

  return val; 
}

RE: Camera Help

Posted: Tue Aug 20, 2013 4:52 pm
by ibm5155
for the camera that aim you, wouldn´t be easy to force a target by acs to an invisible monster, than do it enter on a chase loop and finaly set him as a camera?

RE: Camera Help

Posted: Tue Aug 20, 2013 5:24 pm
by Zeberpal
Klofkac, here is what I've done:
1)I placed a Camera/Aiming Camera thing on a map. (and tagged them 1000 just in case)
2) Inserted this script

Code: Select all


#include "zcommon.acs"

function int FixedSqrt(int number)
{
  if (number == 1.0) return 1.0;
  if (number <= 0) return 0;
  int val = 150.0;
  for (int i=0; i<15; i++)
    val = (val + FixedDiv(number, val)) >> 1;

  return val;
}
       
script 1 ( int player_tid, int camera_tid )
{
int x,y,z,a,b,c,u1,u;
//Assuming camera has fixed position
x = GetActorX(camera_tid); //I tried 1000 here too
y = GetActorY(camera_tid);//I tried 1000 here too
z = GetActorZ(camera_tid);//I tried 1000 here too
While(1){
a=GetActorX(player_tid)-x;
b=GetActorY(player_tid)-y;
c=GetActorZ(player_tid)-z;
u1=FixedSqrt(FixedMul(a,a)+FixedMul(b,b));
SetActorAngle(camera_tid,VectorAngle(a,b));
SetActorPitch(camera_tid,VectorAngle(u,c));
Delay(1);
}}
It's executable now, but nothing ingame happens. Is it just because I haven't gave Player a TID? (I didnt because I barelly remember how, gotta check it later) Or there is more of a core reason?
ibm5155 wrote: for the camera that aim you, wouldn´t be easy to force a target by acs to an invisible monster, than do it enter on a chase loop and finaly set him as a camera?
Could be, but that map is for multiplayer..That method means that monster could pick a different players to chase.

RE: Camera Help

Posted: Tue Aug 20, 2013 5:28 pm
by Klofkac
Zeberpal wrote: It's executable now, but nothing ingame happens. Is it just because I haven't gave Player a TID? (I didnt because I barelly remember how, gotta check it later) Or there is more of a core reason?
Well, I haven't tested it myself, as I am on laptop without zan. Maybe I worte it badly. But if you activate it with puke, 0 (activator) should do a trick.
There isn't ChangeCamera, anyway.

EDIT: I have noticed that pitch isn't working like it should.

RE: Camera Help

Posted: Tue Aug 20, 2013 5:40 pm
by Zeberpal
Image totally forgot about it.

But oh my! It's like a Player is chasing himself. But it's strange. I can see his hud weapon(fists)... and it's actually changed his movement lol. It's like he is driving a truck..or better yet Ice skating. And when I jump I immediatelly watch on my feet. I can't look up and down though. This makes me think that the whole Zdoom's player movement code is.. something camera movement code lol ?

RE: Camera Help

Posted: Tue Aug 20, 2013 5:48 pm
by Klofkac
Erm, looks like you used camera's TID as player...
EDIT: Why power of 2 gives me negative number ???
EDIT 2: Oh god, looks like the pythagorean theorem causes integer overflows...
EDIT 3: Yay, no VectorLength...

RE: Camera Help

Posted: Tue Aug 20, 2013 5:53 pm
by ibm5155
Thing_Hate (hater, hatee, 6);
This would make the monster chase the player while it´s not dead
then when it dies, it could enter on a states that call an acs script to chase other guy... (weird idea wroted \o/)

RE: Camera Help

Posted: Tue Aug 20, 2013 5:59 pm
by Zeberpal
Klofkac wrote: Erm, looks like you used camera's TID as player...
Well I tried to puke once i was in Camera view. Camera's view was standing still but Player was still a truck.
I cant believe it's a dead end D: ?

Ibm5155, hmm that's true, I will definetelly try this method If hardcoding fails

RE: Camera Help

Posted: Tue Aug 20, 2013 6:00 pm
by Klofkac
OK when I phased out the pitch it worked perfectly for me.
Made pitch working. Although it works only with integer coords, there will be un-noticeable accuracy loss:

Code: Select all

#include "zcommon.acs"
      
function int sqrt (int n)
{
	int a;
	for (a=0;n>=(2*a)+1;n-=(2*a++)+1);
	return a;
}      

script 1 ( int player_tid, int camera_tid )
{
int x,y,z,a,b,c,u;
//Assuming camera has fixed position
x = GetActorX(camera_tid); 
y = GetActorY(camera_tid);
z = GetActorZ(camera_tid);
ChangeCamera(camera_tid,0,0);
While(1){
a=GetActorX(player_tid)-x;
b=GetActorY(player_tid)-y;
c=GetActorZ(player_tid)-z;
u=sqrt((a>>16)*(a>>16)+(b>>16)*(b>>16));
SetActorAngle(camera_tid,VectorAngle(a,b));
SetActorPitch(camera_tid,-VectorAngle(u<<16,c+48.0));
Delay(1);
}}
For puking, use puke 1 0 [camTID].
For use in ACS, use ACS_Execute(1,0,[playerID or 0 if activator is that player],[camID]);

RE: Camera Help

Posted: Tue Aug 20, 2013 7:02 pm
by Zeberpal
Thank you!