Jump to content


Photo

Instant Bullet


  • Please log in to reply
7 replies to this topic

#1 nhmllr

nhmllr

    GMC Member

  • New Member
  • 173 posts

Posted 12 April 2011 - 10:38 PM

I know that if you want a bullet to collide when it's traveling very fast, you tell it to test collision between where it is now and where is was the last step. But how do you make it fast like a real bullet, in that it draws a line from the gun to the next thing in front of the path that the bullet would run across? (I know how to draw lines, by the way)

Thanks
  • 0

#2 PetzI

PetzI

    GMC Member

  • GMC Member
  • 1051 posts
  • Version:GM8.1

Posted 12 April 2011 - 11:04 PM

(collision_line(x1,y1,x2,y2,obj,prec,notme) This function tests whether there is a collision between the line segment from (x1,y1) to (x2,y2) and entities of object obj. This is a powerful function. You can e.g. use it to test whether an instance can see another instance by checking whether the line segment between them intersects a wall.


  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 19914 posts
  • Version:GM:Studio

Posted 12 April 2011 - 11:15 PM

That will not work... it doesnīt return the exact coords of the collision, and if there is a collision with more than one wall object along the length og the line, it may not even return the first, but rather the one with the lowest depth or the lowest instance id. No there are better ways.... Here is the easiest one to do...

In the shoot event of the object do this..

with (instance_create(x,y,obj_bullet))
{
direction=other.image_angle; //or whatever to set the angle of the bullet draw object....
move_contact_all(direction,10000);
}

Now in the bullet object you can do something like this...

//CREATE EVENT
alarm[0]=5;

//ALARM[0]
instance_destroy();

//DRAW EVENT
draw_line(xstart,ystart,x,y);

try that and see how it goes, but if itīs not good enough then I have a fantastic little script that does the same thing but pixel perfect....
  • 0

#4 Reefpirate

Reefpirate

    GMC Member

  • GMC Member
  • 335 posts

Posted 13 April 2011 - 02:49 AM

I have insta-bullets in my game... The way I handled it was with the collision_line_first() script from GMLscripts.com. I'd recommend it! I just check what the first thing hit by the line is, and that's the thing that gets hit. I can then do to-hit calculations on enemies and such after that. You can do other effects too, like ricochets around the target if you missed, or just draw really fast sprites coming out of the gun that don't do collision checks but just look cool.
  • 0

#5 nhmllr

nhmllr

    GMC Member

  • New Member
  • 173 posts

Posted 25 April 2011 - 08:11 PM

That will not work... it doesnīt return the exact coords of the collision, and if there is a collision with more than one wall object along the length og the line, it may not even return the first, but rather the one with the lowest depth or the lowest instance id. No there are better ways.... Here is the easiest one to do...

In the shoot event of the object do this..

with (instance_create(x,y,obj_bullet))
{
direction=other.image_angle; //or whatever to set the angle of the bullet draw object....
move_contact_all(direction,10000);
}

Now in the bullet object you can do something like this...

//CREATE EVENT
alarm[0]=5;

//ALARM[0]
instance_destroy();

//DRAW EVENT
draw_line(xstart,ystart,x,y);

try that and see how it goes, but if itīs not good enough then I have a fantastic little script that does the same thing but pixel perfect....


Sorry to respond so late
Thanks for the code, it does do what I envisioned.
However, about half of the time, the bullet doesn't end up at the wall: it sometimes doesn't fire, even if the gun is pointed at a wall.

Thanks for the code~
  • 0

#6 Cocopuffs

Cocopuffs

    Now with more funny!

  • New Member
  • 652 posts

Posted 25 April 2011 - 11:29 PM

Sorry to respond so late
Thanks for the code, it does do what I envisioned.
However, about half of the time, the bullet doesn't end up at the wall: it sometimes doesn't fire, even if the gun is pointed at a wall.

Thanks for the code~


That more than likely is a problem with the
move_contact_all(direction,10000);
Which means that the bullet will stop when it comes into contact with absolutely any object, including scenery or even the gun itself. If you only want solid objects to stop the bullet you can replace it with:
move_contact_solid(direction,10000);

If you want it to contact a multitude of objects both solid and nonsolid, you can whip up a script that temporarily turns all the objects you want to stop the bullet solid, then right afterwards turn them back to how they were before.

Example:
with enemy {temp_solid=solid; solid=true;}
move_contact_solid(direction,10000);
with enemy {solid=temp_solid;}

Hope that solved your issue.
  • 0

#7 nhmllr

nhmllr

    GMC Member

  • New Member
  • 173 posts

Posted 27 April 2011 - 01:44 AM


Sorry to respond so late
Thanks for the code, it does do what I envisioned.
However, about half of the time, the bullet doesn't end up at the wall: it sometimes doesn't fire, even if the gun is pointed at a wall.

Thanks for the code~


That more than likely is a problem with the
move_contact_all(direction,10000);
Which means that the bullet will stop when it comes into contact with absolutely any object, including scenery or even the gun itself. If you only want solid objects to stop the bullet you can replace it with:
move_contact_solid(direction,10000);

If you want it to contact a multitude of objects both solid and nonsolid, you can whip up a script that temporarily turns all the objects you want to stop the bullet solid, then right afterwards turn them back to how they were before.

Example:
with enemy {temp_solid=solid; solid=true;}
move_contact_solid(direction,10000);
with enemy {solid=temp_solid;}

Hope that solved your issue.


I haven't gotten my enemies totally up and running, but the bullets are working beautifully so far. Thank you!
  • 0

#8 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Retired Staff
  • 15416 posts
  • Version:GM:Studio

Posted 27 April 2011 - 02:55 AM

A faster method is range finder or collision line first on gmlscripts.com
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users