Proper gun positioning

Posted By: 3run

Proper gun positioning - 12/08/17 19:46

I'm trying to position weapon correctly on the screen, so changing resolution won't actually affect it's appearance. I remember MasterQ32 recommended me to use rel_for_screen, but after I've struggled for a while (I couldn't get it working) he helped me out and made an example, and it worked like a charm! But back then I didn't really get/find any differences to my original code that didn't work grin Unfortunately I lost some data from my laptop which I was using back then, so I have to make things from scratch.

Idea (if I got it correctly) is to find center of the X coordinates on the screen (screen_size.x / 2) and the lowest Y coordinate of the screen (screen_size.y) and then convert it into the world XYZ coordinates (that's what rel_for_screen does, right? same as vec_for_screen but without views angle and position, for my purpose it's probably even better to use vec_for_screen, cause it's not a view entity but a world one).

Here is how I do that:
Code:
VECTOR pos;
vec_set(&pos, vector(screen_size.x / 2, screen_size.y, 4)); // 4
vec_for_screen(&pos, camera);


But the tricky part here is to position the weapon sprite (yes, I use sprites) correctly. The one I use has it's size 256x256. How do I place it correctly, can any one give me a hint, please?


Best regards.
Posted By: MasterQ32

Re: Proper gun positioning - 12/08/17 20:57

scale it down and displace it like a normal weapon model. also you need to set DECAL and disable auto-rotation, then set the model rotation to camera.pan
Posted By: 3run

Re: Proper gun positioning - 12/08/17 21:04

Originally Posted By: MasterQ32
scale it down and displace it like a normal weapon model.
That's what I did, but it doesn't work, take a look:



Code:
Code:
{
	my = ent_create("v_supershotgun+12.tga", nullvector, NULL);
	set(my, PASSABLE | NOFILTER | ZNEAR);
	vec_fill(&my->scale_x, 0.0625); // 0.0625
	my->ambient = 100;
	
	while(my){
		
		camera->arc = 90;		
		camera->pan = cycle(camera->pan - mickey.x / 6.5 * 1, 0, 360);
		camera->tilt = clamp(camera->tilt - mickey.y / 6.5 * 1, -90, 90);		
		camera->roll = 0;		
		
		VECTOR pos;
		vec_set(&pos, vector(screen_size.x / 2, screen_size.y, 4));
		vec_for_screen(&pos, camera);
		
		draw_point3d(&pos, COLOR_RED, 100, 0.1);
		
		VECTOR off;
		vec_set(&off, vector(4, 0, 4.5));
		vec_rotate(&off, &camera->pan);
		vec_add(&off, &pos);
		
		vec_set(&my->x, &off);
		vec_set(&my->pan, &camera->pan);
		
		wait(1);
	}
	
}



Greets!
Posted By: Kartoffel

Re: Proper gun positioning - 12/08/17 22:05

Shouldn't positioning the gun based on the camera's fov & aspect ratio work for all resolutions?
Posted By: 3run

Re: Proper gun positioning - 12/08/17 22:17

Originally Posted By: Kartoffel
Shouldn't positioning the gun based on the camera's fov & aspect ratio work for all resolutions?
Well, I used to use something like this before:
Code:
VECTOR pos;
vec_set(&pos, vector(16, 0, 8));
vec_rotate(&pos, &camera->pan);
vec_add(&pos, &camera->x);

vec_set(&weapon->x, &pos);

But this won't work correctly, it will give same results as in pictures above, and I don't really know how to position weapon based on camera's fov and aspect ratio. Any ideas are welcome.

Greets.
Posted By: Kartoffel

Re: Proper gun positioning - 12/08/17 23:25

well for example use

vec_set(&pos, vector(16, 0, some_value * (screen_height / screen_width)));
Posted By: txesmi

Re: Proper gun positioning - 12/09/17 01:47

Hi,
the bottom of the screen has a trigonometric relation with the camera arc. It may be computed only once when view arc or screen resolution changes.

Code:
_ent->skill20 = fcos(0.5 * camera->arc, _distance);
_ent->skill21 = 0;
_ent->skill22 = _sizeY - fsin(0.5 * camera->arc, _distance) * screen_size.y / screen_size.x;
...
vec_set(&_ent->x, &_ent->skill20);
vec_rotate(&_ent->x, &camera->pan);
vec_add(&_ent->x, &camera->x);
_ent->pan = ang(camera->pan - 180);
_ent->tilt = -camera->tilt;
_ent->roll = -camera->roll;



_distance is the distance to the camera and _sizeY is the sprites max_y value (0.5 * bmap_height * scale_y).

Salud!
Posted By: 3run

Re: Proper gun positioning - 12/09/17 09:21

Wow! Thank you very much for your time and help guys! laugh

Kartoffel@ thank you man! but unfortunately couldn't get it working, still when resolution changes weapon is not placed at the same spot.

txesmi@ thank you man, it works like a charm! yet the math part is a little bit above of my head, I'm not good at trigonometric.


Here is current code (with weapon bobbing effect) for those who might need it:
Code:
my = ent_create("v_supershotgun+12.tga", nullvector, NULL);
set(my, PASSABLE | NOFILTER | ZNEAR);
vec_fill(&my->scale_x, 0.0625); // cause original size was 256x256, I scale it down to 16x16
my->ambient = 100;

var weapon_bob_move = 0, weapon_bob = 0, weapon_bob_y = 0, weapon_bob_z = 0;

while(my){
	
	camera->arc = 90;		
	camera->pan = cycle(camera->pan - mickey.x / 6.5 * 1, 0, 360);
	camera->tilt = clamp(camera->tilt - mickey.y / 6.5 * 1, -90, 90);		
	camera->roll = 0;
	
	if(key_w){ weapon_bob_move = (weapon_bob_move + 30 * time_step) % 360; }
	weapon_bob = clamp(weapon_bob + 4 * time_step * 0.1 - weapon_bob / 3 * time_step, 0, 2);
	weapon_bob_y = sin(weapon_bob_move) * weapon_bob * 0.5;						
	weapon_bob_z = cos(weapon_bob_move * 2) * weapon_bob * 0.25;
	
	var x_distance = 16, z_distance = 17;		
	my->skill20 = fcos(0.5 * camera->arc, x_distance);
	my->skill21 = -weapon_bob_y;
	my->skill22 = 8 - fsin(0.5 * camera->arc, z_distance) * screen_size.y / screen_size.x + weapon_bob_z;
	
	vec_set(&my->x, &my->skill20);
	vec_rotate(&my->x, &camera->pan);
	vec_add(&my->x, &camera->x);
	
	ANGLE temp_angle;
	vec_set(&temp_angle, vector(180, 0, 0));
	ang_add(&temp_angle, &camera->pan);
	vec_set(&my->pan, &temp_angle);
	
	wait(1);
}




Best regards!
Posted By: txesmi

Re: Proper gun positioning - 12/09/17 17:10

Glad of been of help laugh

Here are the concepts behind the solution, if insterested.

Posted By: 3run

Re: Proper gun positioning - 12/09/17 17:19

Thank you for your time for making that concept! laugh

Best regards!
Posted By: txesmi

Re: Proper gun positioning - 12/11/17 12:35

You are welcome laugh

I realized/remembered there is a better trigonometric relation between cathetus and angles... the tangent! blush cry



Salud!
Posted By: 3run

Re: Proper gun positioning - 12/11/17 12:48

Thank you very much bro! laugh
Posted By: 3run

Re: Proper gun positioning - 04/16/18 00:01

Hi!

Faced some troubles with your solution, txesmi!
Fullscreen mode messes everything up..

Take a look at this code
Code:
void main(){
	
	fps_max = 60;
	level_load("");
	wait(3);
	
	camera->arc = 90;
	
	my = ent_create("test.png", nullvector, NULL);
	set(my, PASSABLE | NOFILTER | ZNEAR);
	vec_fill(&my->scale_x, 1);
	my->ambient = 100;

	var distance = 16, size_z = 8;

	while(my){
		
		my->skill20 = fcos(0.5 * camera->arc, distance);
		my->skill21 = 0;
		my->skill22 = size_z - fsin(0.5 * camera->arc, distance) * screen_size.y / screen_size.x;
		
		vec_set(&my->x, &my->skill20);
		vec_rotate(&my->x, &camera->pan);
		vec_add(&my->x, &camera->x);
		
		my->pan = ang(camera->pan - 180);
		my->tilt = -camera->tilt;
		my->roll = -camera->roll;
		
		wait(1);
	}
	
}


And a small bmap to test it, here it goes (right click and save, it's 16x16):


As far as I understood it should work correctly, but it does not :<

Screen examples (notice that resolution is the same!)
Quote:
Windowed mode

Fullscreen mode


Greets!
Posted By: Kartoffel

Re: Proper gun positioning - 04/16/18 08:45

looks like the image is scaled along the screen y axis (which is weird)

maybe check if screen_size really contains the correct resolution?

Edit: another thing that might mess things up is the billboard rotation that the engine does for sprites (I think this is done when the sprite rotation is (0,0,0)).
Posted By: 3run

Re: Proper gun positioning - 04/16/18 10:28

Originally Posted By: Kartoffel
maybe check if screen_size really contains the correct resolution?
Checked that, and it's correct (720x480 in both modes).

As for rotation, as far as I know, when you manually change rotation of the sprite, engine won't rotate it by itself.
Posted By: txesmi

Re: Proper gun positioning - 04/16/18 12:38

Hi,
I noticed that the proportion of the projection area in full screen is always real screen sized. That is strange because while it computes the projection matrix by 'screen_size', it shows a rectangle with the real proportion of the screen, so it cuts part of the projection depending on the proportion difference. And at the end it renders in 'screen_size' resolution! It sounds like a bug for me. The good thing is that it is easy to work around xP

Code:
#include <acknex.h>

var distance = 16;
var cameraProp = 1;
var screenProp = 1;

void safe_video_switch (var _mode) {
	video_mode = video_switch(_mode, 32, video_screen);
	if(!video_mode)
		video_mode = video_switch(7, 32, video_screen);
	if (video_screen == 1)
		cameraProp = distance * screenProp / camera->aspect;
	else
		cameraProp = distance * screen_size.y / (screen_size.x * camera->aspect);
}

void onQ () {
	safe_video_switch(video_mode + 1);
}

void onW () {
	video_screen = 1 + video_screen % 2;
	safe_video_switch(video_mode);
}

void weaponLocate (ENTITY *_ent) {
	var size_z = 8 * _ent->scale_z;
	_ent->x = distance;
	_ent->y = 0;
	_ent->z = size_z - tanv(0.5 * camera->arc) * cameraProp;
	vec_rotate(&_ent->x, &camera->pan);
	vec_add(&_ent->x, &camera->x);
	_ent->pan = ang(camera->pan - 180);
	_ent->tilt = -camera->tilt;
	_ent->roll = -camera->roll;
}

void main () {
	screenProp = sys_metrics(1) / sys_metrics(0);
	fps_max = 60;
	video_mode = 7;
	video_screen = 1;
	wait(1);
	
	on_q = onQ;
	on_w = onW;
	
	level_load("");
	camera->arc = 90;
	camera->aspect = 1;
	
	safe_video_switch(7);
	
	ENTITY *_ent = ent_create("sprite2.tga", nullvector, NULL);
	set(_ent, PASSABLE | NOFILTER | ZNEAR | DECAL);
	vec_fill(&_ent->scale_x, 1);
	_ent->ambient = 100;

	while (!key_esc) {
		camera->pan = ang(camera->pan - mickey.x * 0.2);
		camera->tilt = clamp(camera->tilt - mickey.y * 0.2, -90, 90);
		weaponLocate(_ent);
		wait(1);
	}
	
	sys_exit(NULL);
}



added 'camera->aspect' as it has full relevance on the computations.

Originally Posted By: 3run
As for rotation, as far as I know, when you manually change rotation of the sprite, engine won't rotate it by itself.

Remember the DECAL flag

Salud!
Posted By: 3run

Re: Proper gun positioning - 04/16/18 12:50

Thank you very much, man! I'll dive in to the code laugh

Best regards!
Posted By: 3run

Re: Proper gun positioning - 04/16/18 13:46

A little bump, what size is your "sprite2.tga" ?

Edit: got it working, size was 16x16 if I understood correctly.
Thank you one more time man! laugh
Posted By: txesmi

Re: Proper gun positioning - 04/16/18 19:35

glad of been of help wink
© 2024 lite-C Forums