Camera Help

Discuss all aspects related to modding Zandronum here.
Post Reply
User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

Camera Help

#1

Post by Zeberpal » Tue Aug 20, 2013 2:36 pm

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?
Last edited by Zeberpal on Tue Aug 20, 2013 4:11 pm, edited 1 time in total.
Image ImageImage

Klofkac
Forum Regular
Posts: 481
Joined: Sat Jun 09, 2012 1:31 pm
Location: Ask Grandvoid servers

RE: Camera Help

#2

Post by Klofkac » Tue Aug 20, 2013 2:43 pm

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.
Last edited by Klofkac on Tue Aug 20, 2013 3:37 pm, edited 1 time in total.
𝕂𝕝𝕠𝕗𝕜𝕒𝕔

User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

RE: Camera Help

#3

Post by Zeberpal » Tue Aug 20, 2013 4:04 pm

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!
Image ImageImage

Klofkac
Forum Regular
Posts: 481
Joined: Sat Jun 09, 2012 1:31 pm
Location: Ask Grandvoid servers

RE: Camera Help

#4

Post by Klofkac » Tue Aug 20, 2013 4:14 pm

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.
Last edited by Klofkac on Tue Aug 20, 2013 4:18 pm, edited 1 time in total.
𝕂𝕝𝕠𝕗𝕜𝕒𝕔

User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

RE: Camera Help

#5

Post by Zeberpal » Tue Aug 20, 2013 4:41 pm

Square roots might not work in zan? I define FixedSqrt, but even so he says missing semicolon.
Image ImageImage

Klofkac
Forum Regular
Posts: 481
Joined: Sat Jun 09, 2012 1:31 pm
Location: Ask Grandvoid servers

RE: Camera Help

#6

Post by Klofkac » Tue Aug 20, 2013 4:45 pm

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; 
}
𝕂𝕝𝕠𝕗𝕜𝕒𝕔

User avatar
ibm5155
Addicted to Zandronum
Posts: 1641
Joined: Tue Jun 05, 2012 9:32 pm
Location: Somewhere, over the rainbow

RE: Camera Help

#7

Post by ibm5155 » Tue Aug 20, 2013 4:52 pm

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?
Projects
Cursed Maze: DONE, V2.0
Zombie Horde - ZM09 map update: [3/15/13]
Need help with English? Then you've come to the right place!

<this post is proof of "Decline">

User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

RE: Camera Help

#8

Post by Zeberpal » Tue Aug 20, 2013 5:24 pm

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.
Last edited by Zeberpal on Tue Aug 20, 2013 5:26 pm, edited 1 time in total.
Image ImageImage

Klofkac
Forum Regular
Posts: 481
Joined: Sat Jun 09, 2012 1:31 pm
Location: Ask Grandvoid servers

RE: Camera Help

#9

Post by Klofkac » Tue Aug 20, 2013 5:28 pm

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.
Last edited by Klofkac on Tue Aug 20, 2013 5:40 pm, edited 1 time in total.
𝕂𝕝𝕠𝕗𝕜𝕒𝕔

User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

RE: Camera Help

#10

Post by Zeberpal » Tue Aug 20, 2013 5:40 pm

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 ?
Image ImageImage

Klofkac
Forum Regular
Posts: 481
Joined: Sat Jun 09, 2012 1:31 pm
Location: Ask Grandvoid servers

RE: Camera Help

#11

Post by Klofkac » Tue Aug 20, 2013 5:48 pm

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...
Last edited by Klofkac on Tue Aug 20, 2013 7:59 pm, edited 1 time in total.
𝕂𝕝𝕠𝕗𝕜𝕒𝕔

User avatar
ibm5155
Addicted to Zandronum
Posts: 1641
Joined: Tue Jun 05, 2012 9:32 pm
Location: Somewhere, over the rainbow

RE: Camera Help

#12

Post by ibm5155 » Tue Aug 20, 2013 5:53 pm

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/)
Projects
Cursed Maze: DONE, V2.0
Zombie Horde - ZM09 map update: [3/15/13]
Need help with English? Then you've come to the right place!

<this post is proof of "Decline">

User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

RE: Camera Help

#13

Post by Zeberpal » Tue Aug 20, 2013 5:59 pm

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
Last edited by Zeberpal on Tue Aug 20, 2013 6:03 pm, edited 1 time in total.
Image ImageImage

Klofkac
Forum Regular
Posts: 481
Joined: Sat Jun 09, 2012 1:31 pm
Location: Ask Grandvoid servers

RE: Camera Help

#14

Post by Klofkac » Tue Aug 20, 2013 6:00 pm

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]);
Last edited by Klofkac on Tue Aug 20, 2013 6:32 pm, edited 1 time in total.
𝕂𝕝𝕠𝕗𝕜𝕒𝕔

User avatar
Zeberpal
Forum Regular
Posts: 477
Joined: Mon Jun 04, 2012 6:55 am

RE: Camera Help

#15

Post by Zeberpal » Tue Aug 20, 2013 7:02 pm

Thank you!
Image ImageImage

Post Reply