Jump to content

Why a bullet object?


Chiblue
 Share

Recommended Posts

Interesting thing I see is that most of the code I have seen in LE for firing a gun involves creating a sphere and moving it and then checking any collisions...

 

In the other engines I have used this was done primarily using raycast, just out of curiosity why the difference, as you really cannot see a bullet in real life?

If it's not Tactical realism then you are just playing..

Link to comment
Share on other sites

The only advantage I see is the fact that realistic ballistics come into play, so you can have parabolic movement as gravity pulls the bullet down over distance, realistic transmission of momentum into hit objects and also true rickochet (which would be quite tricky to simulate without using a real object and the physics engine). Personally ... I'm just using raycasts.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

considering most of the engine is done on GPU I find I have a huge amount of CPU headroom especially considering 90% of the time bullets aren't colliding with anything.

 

For a generic FPS you'd probably just use raycasts though, only mil-sims really need bullet drop and ricochets.

AMD Phenom 9850 (X4 2.5GHz) | 1066MHz CL5 GEIL Black Dragon (800MHz) | Powercolor ATI 4870 1GB | DFI 790FXB-M2RSH | 24" 1920x1200 | Windows Vista 64-bit Ultimate

Link to comment
Share on other sites

Actually i think it depends on what you want to do.

 

For example, speaking as a FPS-lover, i hate when my bullet doesn't

exactly hit where i shot. So it maybe would be best to do this via

an absolutely straight raycast.

 

On the other hand, when making a space simulation you maybe want to shoot

ships to pieces and assign the right velocity, ect. to it. The "best" (maybe not fastest)

would be to create a entity as a bullet/missile/laser and let the collision callback

deliver you the impact force and so on...

 

It really depends on the game and the CPU/GPU time you have left to waste. ;)

(Win7 64bit) && (i7 3770K @ 3,5ghz) && (16gb DDR3 @ 1600mhz) && (Geforce660TI)

Link to comment
Share on other sites

I do raycasts with my stuff. When I find a hit I apply a body force to the object hit.

 

For bigger things like missles etc I might use bodies but I'm not onto that bit yet.

 

My C++ code: -

 

#include "gun/include/bullet.h"
#include "gun/include/bulletHit.h"

#define DEBUG_BULLET

Bullet::Bullet( TVec3 pos, TVec3 dir, TScene *scene, float speed, int life ) : m_force( 100 )
{
m_scene = scene;
m_position = pos;
m_origin = pos;
m_life = life;
m_direction = dir;
m_speed = speed;

if (!g_bulletEmitterMaterial)
{
	g_bulletEmitterMaterial = LoadMaterial("abstract::dust.mat");
}

HookUpdateEvent(m_scene->updateEvent);
#ifdef DEBUG_BULLET
HookRenderEvent(m_scene->renderEvent);
#endif
}

Bullet::~Bullet()
{
UnhookUpdateEvent( m_scene->updateEvent );
UnhookRenderEvent( m_scene->renderEvent );
}

void Bullet::moveBullet()
{
m_position.X += m_direction.X*m_speed;
m_position.Y += m_direction.Y*m_speed;
m_position.Z += m_direction.Z*m_speed;
}

void Bullet::Render( float gameLoopTime)
{
#ifdef DEBUG_BULLET
if (m_life)
	SetColor( Vec4(1,0,0,1) ); // red
else
	SetColor( Vec4(1,1,1,1) ); // red
TVec3 nextPos = Vec3( m_position.X+m_direction.X*m_speed, m_position.Y+m_direction.Y*m_speed, m_position.Z+m_direction.Z*m_speed);
tdDrawText( m_scene->cam, m_position, "1");
tdDraw( m_scene->cam, m_position, nextPos );
tdDrawText( m_scene->cam, nextPos, "2");
#endif
}

void Bullet::Update(float gameLoopTime)
{
TPick pick;

m_life--;
if (m_life<=0)
{
	//delete (this);
	return;
}

moveBullet();

// Look speed metres ahead
TVec3 nextPos = Vec3( m_position.X+m_direction.X*m_speed, m_position.Y+m_direction.Y*m_speed, m_position.Z+m_direction.Z*m_speed);
if(LinePick(&pick, m_position, nextPos,0.0,0))
{
	builtHitForce( pick );		
	bulletHitEffect( pick );

	// Create a decal
	if(pick.surface)
	{
		// Add decal if less than 20 metres away
		/* if(PointDistance(Vec3(pick.X,pick.Y,pick.Z), m_origin) < 20)
			createBulletDecal( pick, decal ) */
	}
	//delete this;
	m_life = 0;
	return;
}

}

void Bullet::builtHitForce( TPick& pick )
{
TBody body;

body = GetMeshModel( pick.entity );
if(body)
{
	AddBodyForceAtPoint( body, Vec3(m_direction.X * m_force, m_direction.Y*m_force, m_direction.Z*m_force), Vec3(pick.X,pick.Y,pick.Z) );
//		SendEntityMessage( topParent(pick.entity), "bullethit", body, 0 );
	TEntity e = pick.entity;
	BulletHit *hit = new BulletHit;
	hit->m_direction = m_direction;
	hit->m_force = m_force;
	hit->m_origin = m_origin;
	hit->m_speed = m_speed;
	hit->m_pick = pick;
	while (e)
	{
		SendEntityMessage( e, "bullethit", (byte*)hit, 0 );
		e = GetParent(e);
	}
}
}

void Bullet::bulletHitEffect( TPick &pick )
{
TEmitter emitter;
TWorld world = CurrentWorld();

//loadBulletSounds			
//If Not emittermaterial emittermaterial=LoadMaterial("abstract::dust.mat")
SetWorld(m_scene->foregroundworld);

//Richochet emitter
emitter = CreateEmitter(20, 500, Vec3(0,0,1), 1);
PositionEntity( emitter, Vec3(pick.X,pick.Y,pick.Z),1 );
AlignToVector( emitter, Vec3(pick.NX, pick.NY, pick.NZ) );
PaintEntity( emitter, g_bulletEmitterMaterial );
SetEmitterVelocity( emitter, Vec3(0,0,3), Vec3(1,1,0) );
SetEmitterAcceleration( emitter, Vec3(0,-9.8,0) );

//Dust emitter
emitter = CreateEmitter(10,2000,Vec3(0,0,1),1);
PositionEntity( emitter, Vec3(pick.X,pick.Y,pick.Z),1 );
AlignToVector( emitter, Vec3(pick.NX, pick.NY, pick.NZ) );
PaintEntity( emitter, g_bulletEmitterMaterial );
SetEmitterVelocity( emitter,Vec3(0,0,0.2),Vec3(0.1,0.1,0) );
SetEmitterRadius( emitter, 0.25, 0.25 );
EntityColor( emitter,Vec4(1,1,1,0.1) );
SetEmitterRotationSpeed( emitter,0.1 );

//Impact sound
//bulletimpactsound.EmitNPCSound( "bullet", "bullet1", emitter )

SetWorld( world );
}

Link to comment
Share on other sites

Chris, yes I saw that in your code download... thats for the code...

 

I did a similar thing in GDK so if you fired a missile say, you could see it fly, I also atteched a camera to the missle and put a B/W filter on it, so it when you fired a missle you had a popup window that showed the flight and hit of the missle... hopefully someday I will be upto the same technique in LE....

If it's not Tactical realism then you are just playing..

Link to comment
Share on other sites

Just got to try some of your bullet hit effect emitters, very nice effects. Maybe you could set a material type in editor and the load the sound and emitter effects based on that. Sorry not criticizing very nice effect though.

If it's not Tactical realism then you are just playing..

Link to comment
Share on other sites

Just got to try some of your bullet hit effect emitters, very nice effects. Maybe you could set a material type in editor and the load the sound and emitter effects based on that. Sorry not criticizing very nice effect though.

 

If that was at me, I've already done this so sounds etc are stored against the entity key and are used when hit.

Link to comment
Share on other sites

I am actually planning to use a combination of the two methods.

 

First do a raycast to see if the bullet would hit. Then if it would, create a sphere and give it a force.

 

This eliminates a significant number of spheres being created that are complete missed shots and still maintains the realistic feel of bullets being affected by physics.

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...