Random Spray (Angle)

Posted By: 82RJZAE

Random Spray (Angle) - 08/22/12 09:16

Hi, how might I add randomness to the angle of a bullet (c_trace bullet) originating from camera.x to simulate bullet spread? I am using the below method to create bullets:

bullet_create(camera.x,camera.pan,500);

Originally Posted By: Superku
Code:
void bullet_create(VECTOR* vpos,VECTOR* vang,var speed)
{
	var alive = 16;
	VECTOR pos,temp,vel;
	
	vec_set(pos,vpos);
	vec_set(vel,vector(speed,0,0));
	vec_rotate(vel,vang);

	while(alive > 0)
	{
		// calculate new position
		vec_set(temp,vel);
		vec_scale(temp,time_step);
		vec_add(temp,pos);
		c_trace(pos,temp,IGNORE_PASSABLE | USE_POLYGON); // optional: SCAN_TEXTURE
		if(trace_hit)
		{
			/*if(you) ... // reduce health, blood effect
			else ... // spark effect*/
			alive = 0;
		}
		effect(p_bullet,1,pos,vel); // you may want to shorten vel on impact
		vec_set(pos,temp); // move to new position

		alive -= time_step;
		wait(1);
	}
}


My apologies I am not too familiar with vector mathematics.
Posted By: MasterQ32

Re: Random Spray (Angle) - 08/22/12 09:50

just randomize your shooting direction a little bit:
vang.x += random(spray) - spray / 2;
vang.y += random(spray) - spray / 2;

spray is the total spraying angle
Posted By: 3run

Re: Random Spray (Angle) - 08/22/12 11:30

Here is how I've edited that function for my old project:
Code:
// accuracy:
var wpn_aim_x = 1; // play with this value
var wpn_aim_y = 1; // play with this value

function bullet_create(VECTOR* vpos, VECTOR* vang, var speed){
	var alive = 16;
	VECTOR pos;
	VECTOR temp;
	VECTOR vel;	
	ANGLE accur;
	vec_set(pos, vpos);
	vec_set(vel, vector(speed, 0, -3));
	vec_set(accur, vector(wpn_aim_x - random(wpn_aim_x * 2), wpn_aim_y - random(wpn_aim_y * 2), 0));
	ang_add(accur, vang);
	vec_rotate(vel, accur);
	while(alive > 0){
		// calculate new position
		vec_set(temp, vel);
		vec_scale(temp, time_step);
		vec_add(temp, pos);
		trace_mode = IGNORE_PASSABLE | USE_POLYGON | SCAN_TEXTURE;
		c_trace(pos, temp, trace_mode); 
		if(trace_hit){
			// check if hit the target:
			if(vec_dist(target, nullvector) > 0){
				// effect for blocks:
				if(hit.entity == NULL){
					
				}	
				else{
					// entity was hit:
				}			
			}
			alive = 0;
		}
		// you may want to shorten vel on impact:
		effect(bullet_effect, 1, pos,vel); 
		// move to new position:
		vec_set(pos, temp); 
		alive -= time_step / 16;
		wait(1);
	}
}

I use it like this:
Code:
bullet_create(muzzle_pos.x, weapon.pan, 400);
// muzzle_pos is the vector where I create muzzle flash effect

Change accuracy of your weapons depending on the player's movement state to make it more realistic laugh
© 2023 lite-C Forums