How to make a rocket fly on a spiral trajectory?

Discuss programming in the QuakeC language.
Post Reply
Orion
Posts: 476
Joined: Fri Jan 12, 2007 6:32 pm
Location: Brazil

How to make a rocket fly on a spiral trajectory?

Post by Orion »

I think that should be easy, but I have no clue how to code this. I want to make 2 rockets launched simultaneously from the rocket launcher fly on a spiral pattern, but also towards to where I aimed. Maybe I should spawn an invisible entity to determine the center point which both rockets will fly around?
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Re: How to make a rocket fly on a spiral trajectory?

Post by frag.machine »

Since both rockets will hit the same point, I'd suggest you to go the easy way: make a new missile model, using frame groups to animate the rockets spiralling.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
Orion
Posts: 476
Joined: Fri Jan 12, 2007 6:32 pm
Location: Brazil

Re: How to make a rocket fly on a spiral trajectory?

Post by Orion »

I don't want the rockets to hit *exactly* the same point, depending on the position they will hit a wall, it'll be more likely they will hit a wall side by side or vertically, etc. And I prefer the hard way, 2 individual rockets spiralling, so it's more 'realistic' and there will be 2 trails making a cool corkscrew as they fly. :wink:
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: How to make a rocket fly on a spiral trajectory?

Post by Spike »

makevectors(self.enemy.angles);
setorigin(self, self.enemy.origin + (v_right * sin(time)) + (v_up * cos(time)));

'self' will then orbit 'self.enemy'. update its angles_z at the same time and you can make the rocket revolve also.
really though you probably want to update the rocket's velocity such that it moves towards the specified point instead (although you may find aiming short of it is smoother due to greater reliability). then you don't have the rocket suddenly appearing inside a wall and thus either damaging things through it or damaging nothing.
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: How to make a rocket fly on a spiral trajectory?

Post by mankrip »

Spike wrote:really though you probably want to update the rocket's velocity such that it moves towards the specified point instead
Yeah, changing the velocity should be the appropriate solution. It should look something like the following...

Add this to weapons.qc, right above the W_FireRocket function:

Code: Select all

void () W_UpdateRocket =
{
	local   entity missile;
	local   vector f, r, u;

	missile = self;
	makevectors (missile.angles);
	f = v_forward * 1000;
	// spirals 32 units per second
	r = v_right * 32 * sin (time);
	u = v_up * 32 * cos (time);

	missile.velocity = normalize (f + r + u) * 1000;
//	missile.angles = vectoangles (missile.velocity); // not sure about this line
	missile.nextthink = time + 0.05;

	if (missile.attack_finished < time)
		remove (missile);
}
And replace this:

Code: Select all

void() W_FireRocket =
{
[...]
// set missile duration
	missile.nextthink = time + 5;
	missile.think = SUB_Remove;
[...]
}
With this:

Code: Select all

void() W_FireRocket =
{
[...]
// set missile duration
	missile.attack_finished = time + 5;
	missile.nextthink = time + 0.05; // 20 updates per second
	missile.think = W_UpdateRocket;
[...]
}
I haven't tested this out, though.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Orion
Posts: 476
Joined: Fri Jan 12, 2007 6:32 pm
Location: Brazil

Re: How to make a rocket fly on a spiral trajectory?

Post by Orion »

Got it to work, using this code:

Code: Select all

void() SpiralThink =
{
	local vector ang;
	
	self.nextthink = time + 0.05;
	if (self.health < time)
	{
		if (self.owner.rl_detonation == 1)
			RocketExplode ();
		else
			remove(self);
		return;
	}
	
	if (self.t_width)
	{
		if (self.frags == 0)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 1)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 2)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 3)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 4)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 5)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 6)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 7)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 8)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 9)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 10)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 11)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 12)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 13)
			self.angles_x = self.angles_x - 10;
	}
	else
	{
		if (self.frags == 0)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 1)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 2)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 3)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 4)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 5)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 6)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 7)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 8)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 9)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 10)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 11)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 12)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 13)
			self.angles_x = self.angles_x + 10;
	}
	
	self.frags = self.frags + 1;
	if (self.frags >= 13)
		self.frags = 5;
	
	ang = self.angles;
	ang_x = ang_x * -1;
	makevectors (ang);
	self.velocity = v_forward * 1000;
	
	if (self.owner.rl_detonation == 2)
		RocketCheckExplode2 ();
};
t_width will determine if rocket will go clockwise or counter-clockwise. Two rockets are launched spiralling at opposed directions, doing twice the damage one rocket would do, also using 2 rockets a shot.

This was the result:
http://www.youtube.com/watch?v=cVdQGMon ... e=youtu.be
ratbert
Posts: 37
Joined: Thu Nov 19, 2009 3:47 pm

Re: How to make a rocket fly on a spiral trajectory?

Post by ratbert »

Orion can you post the whole code for this one?

Orion wrote:Got it to work, using this code:

Code: Select all

void() SpiralThink =
{
	local vector ang;
	
	self.nextthink = time + 0.05;
	if (self.health < time)
	{
		if (self.owner.rl_detonation == 1)
			RocketExplode ();
		else
			remove(self);
		return;
	}
	
	if (self.t_width)
	{
		if (self.frags == 0)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 1)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 2)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 3)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 4)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 5)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 6)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 7)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 8)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 9)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 10)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 11)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 12)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 13)
			self.angles_x = self.angles_x - 10;
	}
	else
	{
		if (self.frags == 0)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 1)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 2)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 3)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 4)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 5)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 6)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 7)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 8)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 9)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 10)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 11)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 12)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 13)
			self.angles_x = self.angles_x + 10;
	}
	
	self.frags = self.frags + 1;
	if (self.frags >= 13)
		self.frags = 5;
	
	ang = self.angles;
	ang_x = ang_x * -1;
	makevectors (ang);
	self.velocity = v_forward * 1000;
	
	if (self.owner.rl_detonation == 2)
		RocketCheckExplode2 ();
};
t_width will determine if rocket will go clockwise or counter-clockwise. Two rockets are launched spiralling at opposed directions, doing twice the damage one rocket would do, also using 2 rockets a shot.

This was the result:
http://www.youtube.com/watch?v=cVdQGMon ... e=youtu.be
r00k
Posts: 1111
Joined: Sat Nov 13, 2004 10:39 pm

Re: How to make a rocket fly on a spiral trajectory?

Post by r00k »

Orion that's cool!
Orion
Posts: 476
Joined: Fri Jan 12, 2007 6:32 pm
Location: Brazil

Re: How to make a rocket fly on a spiral trajectory?

Post by Orion »

I'll post the clean code for you, because this one I used there is some mod-specific parameters.

These are the clean SpiralThink() and W_FireSecondRocket(), paste them just above W_FireRocket():

Code: Select all

void() SpiralThink =
{
	local vector ang;
	
	self.nextthink = time + 0.05;
	if (self.health < time)
	{
		remove(self);
		return;
	}
	
	if (self.t_width)
	{
		if (self.frags == 0)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 1)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 2)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 3)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 4)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 5)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 6)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 7)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 8)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 9)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 10)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 11)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 12)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 13)
			self.angles_x = self.angles_x - 10;
	}
	else
	{
		if (self.frags == 0)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 1)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 2)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 3)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 4)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 5)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 6)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 7)
			self.angles_y = self.angles_y + 10;
		else if (self.frags == 8)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 9)
			self.angles_x = self.angles_x - 10;
		else if (self.frags == 10)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 11)
			self.angles_y = self.angles_y - 10;
		else if (self.frags == 12)
			self.angles_x = self.angles_x + 10;
		else if (self.frags == 13)
			self.angles_x = self.angles_x + 10;
	}
	
	self.frags = self.frags + 1;
	if (self.frags >= 13)
		self.frags = 5;
	
	ang = self.angles;
	ang_x = ang_x * -1;
	makevectors (ang);
	self.velocity = v_forward * 1000;
};

void() W_FireSecondRocket =
{
	local	entity missile;

	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_FLYMISSILE;
	missile.solid = SOLID_BBOX;
	missile.classname = "missile";
		
// set missile speed	

	makevectors (self.v_angle);
	missile.velocity = v_forward;
	missile.velocity = missile.velocity * 1000;
	missile.angles = vectoangles(missile.velocity);
	
	missile.touch = T_MissileTouch;
	
// set missile duration
	missile.health = time + 5;
	missile.t_width = 1;
	missile.nextthink = time + 0.05;
	missile.think = SpiralThink;

	setmodel (missile, "progs/missile.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin + v_forward*8 + '0 0 16');
};
And this is the clean W_FireRocket(), replace the whole function with this:

Code: Select all

void() W_FireRocket =
{
	local	entity missile;

	if (self.ammo_rockets < 2)
	{
		sprint (self, "Not enough rockets!\n");
		player_run ();
		return;
	}
	
	self.currentammo = self.ammo_rockets = self.ammo_rockets - 2;
	
	sound (self, CHAN_WEAPON, "weapons/sgun1.wav", 1, ATTN_NORM);

	self.punchangle_x = -2;
	
	W_FireSecondRocket ();

	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_FLYMISSILE;
	missile.solid = SOLID_BBOX;
	missile.classname = "missile";
		
// set missile speed	

	makevectors (self.v_angle);
	missile.velocity = v_forward;
	missile.velocity = missile.velocity * 1000;
	missile.angles = vectoangles(missile.velocity);
	
	missile.touch = T_MissileTouch;
	
// set missile duration
	missile.health = time + 5;
	missile.nextthink = time + 0.05;
	missile.think = SpiralThink;

	setmodel (missile, "progs/missile.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin + v_forward*8 + '0 0 16');
};
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: How to make a rocket fly on a spiral trajectory?

Post by ceriux »

some sexy stuff here =)
qbism
Posts: 1236
Joined: Thu Nov 04, 2004 5:51 am
Contact:

Re: How to make a rocket fly on a spiral trajectory?

Post by qbism »

Fraking awesome, especially that it is two separate rockets.
ratbert
Posts: 37
Joined: Thu Nov 19, 2009 3:47 pm

Re: How to make a rocket fly on a spiral trajectory?

Post by ratbert »

Thanks for the code.


Little tweaking of the routine nice to have three rockets going at once.
As in first rocket just fires in a straight line with two other rockets doing a spiral around first rocket.

Lots of crazy ideas are possible.
ceriux
Posts: 2230
Joined: Sat Sep 06, 2008 3:30 pm
Location: Indiana, USA

Re: How to make a rocket fly on a spiral trajectory?

Post by ceriux »

would the rockets still spiral if they slow dropped altitude ?
Post Reply