[Code/ Idea] bullets without entities

Posted By: Superku

[Code/ Idea] bullets without entities - 01/15/12 23:49

When you're creating a shooter (side-scroller, FPS, space shooter), you'd probably most of the time use models for flying bullets. As the (ent_)creation of all those bullets, their removal, rendering and esp. c_move (that includes a USE_BOX trace) gets quite slow, I suggest you don't use models at all and simply run a function that emulates a bullet as follows:

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

void p_bullet(PARTICLE* p)
{
	vec_set(p.blue,vector(random(128),128+random(128),255));
	set(p, STREAK | BRIGHT);
	p.alpha = 100;
	p.size = 10;
	p.lifespan = 0.01;
	p.event = NULL;
}

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);
	}
}


void main()
{
	fps_max = 60;
	video_mode = 9;
	level_load(NULL);
	ent_create(CUBE_MDL,vector(500,-10,-5),NULL);
	def_move();
	while(1)
	{
		if(mouse_left)
		{
			VECTOR temp;
			vec_set(temp,vector(5,-20,-10));
			vec_rotate(temp,camera.pan);
			vec_add(temp,camera.x);
			bullet_create(temp,camera.pan,500);
			wait(-0.1);
		}
		wait(1);
	}
}



You can still use c_ignore and most of the events. Additionally, if you really need a lot of bullets, you could handle all of them with just one wait (save the bullets in some array/ linked list).
Posted By: alibaba

Re: [Code/ Idea] bullets without entities - 01/16/12 09:36

Why should i use entities? I use c_trace =/
Posted By: Damocles_

Re: [Code/ Idea] bullets without entities - 01/16/12 09:55

Quote:
Why should i use entities? I use c_trace =/


I wonder how you make a blue c_trace ?

a c_trace is for gaining information
and ent_create is for making something "3d" to get rendered.

Posted By: alibaba

Re: [Code/ Idea] bullets without entities - 01/16/12 10:16

Ah, you mean to see the bullet flying? For that i use particles
Posted By: Superku

Re: [Code/ Idea] bullets without entities - 01/16/12 17:28

Of course you normally use c_trace for FPS, but have a look at the following video (or any space shooter) and tell me how you'd do that with a regular c_trace and particles:
http://www.youtube.com/watch?v=vmWDRlbS6aQ&t=10s
Posted By: alibaba

Re: [Code/ Idea] bullets without entities - 01/16/12 19:53

Ahhhh! Understood laugh thanks!
Posted By: darkinferno

Re: [Code/ Idea] bullets without entities - 01/17/12 13:44

i could xD
anyway, question, why do you think this is more efficient than using c_move ?
because we're still tracing per frame per bullet correct? even though its short traces, you think it'll be faster than the c_move approach?
Posted By: MasterQ32

Re: [Code/ Idea] bullets without entities - 01/17/12 15:10

yes it will be
because we have only a single trace colliding with geometry, not geometry colliding with geometry. also a model has to be clipped, drawn, depth-sorted and so on
so i think it will be much faster then c_move
Posted By: Superku

Re: [Code/ Idea] bullets without entities - 01/17/12 16:42

MasterQ32 is right, esp. because c_move uses USE_BOX and the entity creation and deletion takes a lot of time, too.
Posted By: 3run

Re: [Code/ Idea] bullets without entities - 02/05/12 16:17

Is there anyway to make this work with isometric camera? Streak and beam does't work.
Posted By: Superku

Re: [Code/ Idea] bullets without entities - 02/05/12 17:02

I've requested this feature multiple times before and JCL stated this some time ago:

Quote:
BEAM und STREAK für isometrische Views stehen auf der Liste für das nächste Update, d.h. Version 8.40.

That means BEAM and STREAK with isometric views will probably be implemented in the next Update 8.40.
Posted By: 3run

Re: [Code/ Idea] bullets without entities - 02/05/12 19:32

great, thank you laugh
Posted By: Joozey

Re: [Code/ Idea] bullets without entities - 02/05/12 20:00

The fastest way I suppose would be checking if any bullet hits any "hittable" object by coming close enough. If you need a lot of bullets and don't care too much about accuracity. Also, to save speed don't use wait(1). Handle everything in one looping function. You can create 5.000 entities without problems. But if you give all of them their own function (with wait(1)) that is what influences the framerate dramatically.
© 2024 lite-C Forums