Posted By: BastovBros
bullets and collisions - 04/04/10 22:09
Hi all, I have a problem... I am using a code of a bullet that should hit the target (a guy in my case). The bullet moves with the speed +100*time_step, but I cannot manage to code the collision properly. When the guy stands in the way of the flying bullet, no blood sprite appears. It appears only when the guy is moving along the bullet trail. It seems that the collision works only when the guy is in certain coordinates, say at 0, then 100, then 200, 300 and so on, since the speed of the bullet is +100. And if he is standing say at 120 or 350, no effect occurs. I saw how people just make the trace from the gun and calculate the collision right away at the moment of shooting the bullet, so the collision occurs instantaneously. However, I want it to occur exactly when the bullet hits the guy, even though the bullet is moving extremely fast. Any ideas how to do this? As I noticed the code which I am suing was mostly used for slowly moving bullets...
here is also a video to show you what I mean. As you can see the guy stands in the way of the bullets, but no collision occurs. When the guy is moving, some collisions do occur, but randomly, not always (you can see an explosion sprite, I don't have blood sprite yet). Furthermore, the event sprite (explosion sprite) appears in the next frame, when the bullet is already 100 quants ahead and not at the guy....
http://www.youtube.com/watch?v=pLileH6LBBM
thank you for any help
Code:
function fire_bullets() // includes the code that animates the weapon as well { VECTOR temp; VECTOR temp1; proc_kill(4); // don't allow more than 1 copy of this function to run while (mouse_left) // this loop runs for as long as the left mouse button is pressed { vec_set(temp.x,gun_fire_ent.x); // get the coordinates for the bullets' origin vec_set(temp1.x, gun_fire2_ent.x); // create the bullet at camera's position and attach it the "move_bullets" function ent_create ("bullet.mdl", temp1.x, move_bullets); ent_create ("gun_fire.tga", temp1.x, display_muzzle); ent_create ("bullet.mdl", temp.x, move_bullets); ent_create ("gun_fire.tga", temp.x, display_muzzle); // create the gun muzzle // snd_play (bullet_wav, 100, 0); // play the bullet sound at a volume of 100 wait (-0.2); // number of bullets, 6 bullets per second, if 0.1 - 10 bullets per second } } function p_alphafade(PARTICLE *p) { p.alpha -= 1*time_step; if (p.alpha <= 0) p.lifespan = 0; } function p_trace(PARTICLE *p) { set(p, BRIGHT|TRANSLUCENT|STREAK); p.alpha = 10; p.size = 10; //p.skill_a = 1; // fade factor p.event = p_alphafade; } ENTITY* bullet_ent; function move_bullets() { VECTOR last_pos; VECTOR bullet_speed; // stores the speed of the bullet bullet_ent = me; my.scale_x = 0.2; my.scale_y = my.scale_x; my.scale_z = my.scale_x; my.skill30 = 1; // I'm a bullet my.emask |= (ENABLE_IMPACT | ENABLE_ENTITY | ENABLE_BLOCK); set(me,BRIGHT|LIGHT); set(my,PASSABLE); my.event = remove_bullets; // when it collides with something, its event function (remove_bullets) will run my.pan = player.pan; // the bullet has the same pan my.tilt = player.tilt; // and tilt with the camera bullet_speed.x = 100 * time_step; // adjust the speed of the bullet here bullet_speed.y = 0; // the bullet doesn't move sideways bullet_speed.z = 0; // then don't allow the gravity to have its ways with the bullet while (my) // this loop will run for as long as the bullet exists (it isn't "null") { // move the bullet ignoring the passable entities and store the result in distance_covered vec_set(last_pos.x,my.x); c_move (my, bullet_speed, nullvector, IGNORE_PASSABLE); effect(p_trace,1,my.x,vec_sub(last_pos.x,my.x)); wait (1); } } function remove_bullets() // this function runs when the bullet collides with something { wait (1); // wait a frame to be sure (don't trigger engine warnings) ent_create ("gun_fire.tga", my.x, explosion_sprite); // create the explosion sprite set (my, INVISIBLE | PASSABLE); wait (-2); // wait until the explosion_sprite() function is over ent_remove (my); // and then remove the bullet } function display_muzzle() { set (my, PASSABLE | LIGHT); my.roll = 0.01; my.ambient = 100; // and this line makes it even brighter my.pan = player.pan; // has the same pan and tilt my.tilt = player.tilt; // with the weapon my.roll = random(360); // and a random roll angle (looks nicer) my.scale_x = 0.05; // we scale it down my.scale_y = my.scale_x; // on all the axis my.scale_z = my.scale_z; // this would only be needed for a 3D (model-based) muzzle player.ambient = 100; // highlight the weapon (makes it look more realistic) wait (2); // show the muzzle sprite for 2 frames player.ambient = 0; // restore the normal weapon ambient value ent_remove (my); // and then remove it } function explosion_sprite() { set (my, PASSABLE | BRIGHT | TRANSLUCENT); my.scale_x = 0.15; // we scale it down to 0.15 my.scale_y = my.scale_x; // on both axis my.ambient = 100; // and we give it an ambient of 100 my.roll = random(360); // we set a random roll angle my.alpha = 100; // but we set it to be completely opaque for now // while (my.frame < 5) // go through all the animation frames // { // my.frame += 2 * time_step; // animation speed // wait (1); // } while (my.alpha > 0) // now fade the last frame quickly { my.alpha -= 50 * time_step; // 50 = fading speed wait (1); } ent_remove (my); }
here is also a video to show you what I mean. As you can see the guy stands in the way of the bullets, but no collision occurs. When the guy is moving, some collisions do occur, but randomly, not always (you can see an explosion sprite, I don't have blood sprite yet). Furthermore, the event sprite (explosion sprite) appears in the next frame, when the bullet is already 100 quants ahead and not at the guy....
http://www.youtube.com/watch?v=pLileH6LBBM
thank you for any help