Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

@James is wording my question better than I am, so I figured I'd put this here:

I believe what you are asking is that you have a bullet moving in a game with a fixed timestep and that when the bullet collides with something, the time step usually makes the bullet go Past where it would have actually hit making it look like the bullets hit some where beyond where it actually hit.


I'm shooting a projectile in a 2D topdown game. The unit can shoot in 360 direction. There's no gravity, there's no arc and it moves at a constant speed (although this last point may change in the future). It's easy to find the point of the unit and the destination of the bullet, but how do I know when the bullet has hit it's destination?

I'll be more specific. The bullet probably won't land directly on the point it was intended to because it moves a certain distance based on the delta of time. The bullet will often be in a state where it will overshoot the destination on the next tick.

How do games usually handle this? I can imagine pros and cons to "always overshoot" and "always undershoot".

share|improve this question

marked as duplicate by Byte56, Sean Middleditch, Nicol Bolas, Maik Semder, msell Jun 11 '13 at 9:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
When the bullet intersects with a solid target it has hit. You can use ray casting between bullet update positions to see if it passed through anything. –  Byte56 Jun 10 '13 at 22:26
    
@Byte56 There isn't necessarily a solid target. The target may have moved by the time the projectile hit its destination. Also this is a 2D game, so I don't think raycasting will/can be used. Especially because it's in a HTML Canvas using 2D graphics. –  Daniel Kaplan Jun 10 '13 at 22:28
    
@Byte56 That being the case, I suppose I could put a non-rendered wall behind the projectile's destination and just wait until I collide with it. Over projectiles would be unaffected by this wall. I'm not sure if this is what you were recommending, but it could work. –  Daniel Kaplan Jun 10 '13 at 22:31
3  
@IvanKuckir OK. Saying something like "What is a "bullet"? there are only zeros and ones in the computer, no bullets." doesn't get that point across very well. I think we all know a "bullet" is in this context. Abstraction is what programming is all about, so we don't have to deal with ones and zeros. And I think bullet is a reasonable abstraction level. I agree that "hit the destination" is vague, but I would just ask the OP to explain it again in a different way. Not change abstraction levels. –  Byte56 Jun 10 '13 at 23:12
1  
This is a question that has been asked MANY times here and in many sites. There are even full articles due to the popularity of this question. Please use Google. –  stas Jun 11 '13 at 3:23

2 Answers 2

up vote 2 down vote accepted

Correct me if I am wrong, but I believe what you are asking is that you have a bullet moving in a game with a fixed timestep and that when the bullet collides with something, the time step usually makes the bullet go Past where it would have actually hit making it look like the bullets hit some where beyond where it actually hit.

Assuming that is correct, this is quite easy to fix then. All you have to do is move the bullet back to where it actually hit instead of leaving where the timestep would have placed it if the path had been unobstructed. There might be easier ways to accomplish this but basically what you are looking for is where the line the bullet is traveling down intersects the 'face' of whatever it hit.. IE: You want to find out where your ray intersects the line of the object it collided with.

Note that this is a calculation that should only be done after the impact has been confirmed.

share|improve this answer
1  
Yes, this is exactly what I'm trying to ask. I don't think I worded my question correctly because most people don't seem to be directly replying like you are. To me it seems simpler than you're saying. It's a given in my requirements that I know where the destination is. I can just confirm a hit, then render it on the destination, no? –  Daniel Kaplan Jun 10 '13 at 23:37
    
Ah, but what I don't understand is how do I confirm a miss and then follow the same algorithm? –  Daniel Kaplan Jun 10 '13 at 23:39
    
@tieTYT If your bullets travel Instantly, then yes, whatever you are aiming at will be hit immediately and you can just show the impact there. However, it sounds as though the bullets take time to travel. If that is the case then you will not know for sure you hit something until the bullet travels and you have then collide with an object. –  James Jun 10 '13 at 23:45
    
So should I just put a non-rendered object in the location of the destination called missLocation? If the projectile collides with missLocation but not target then I know to stop moving the projectile? –  Daniel Kaplan Jun 10 '13 at 23:56
    
You can give that a try, there is certainly not a single answer for this. I just fear that would give odd side effects down the road. I would not want my bullets just vanishing mid flight at odd intervals.. Would make me start to wonder what was going on. –  James Jun 11 '13 at 0:01

There is several ways to choose from when designing Collission detection/response in a game. (The simplest way would be to use a physics-engine which someone else has already developed, there are some free and som which cost but they all should be pretty easy to find on google) Since the question is not specific as to which types of collissions it is about i will provide "The big picture" of a solution.

The problem you are refering to is often referred to as "Bullet through paper" and is probably the most annoying thing when developing games and gets exponentially more difficult to solve relative to the complexity of a physics engine if you are going for precision.

However...

There are some simple solutions which are not precise but still look real since the artifacts are to small or quick to notice.

You can either do so called "Swept-testing" which means that you interpolate between start and destination of all the objects involved in the potential collission but this will get more and more complicated depending on if both objects are dynamic and/or rotating.

A simpler solution is to simply cut the movement into several smaller steps and check each step for collissions.

Lets say you have a circle which is about to collide with a line-segment but as you said the circle moves into and even beyond the line within a single frame. Then you just create find the direction-vector of the movement and multiply with the circles diameter and check for a new collission on each interval until you have reached the final position.

I think this should be enough information for you to get started on finding further information on the subject.

share|improve this answer
    
See answers that already exist here: gamedev.stackexchange.com/questions/22765/… –  Byte56 Jun 10 '13 at 23:16
    
Probably the most annoying thing when developing games?? Wish that were my experience... –  stephelton Jun 10 '13 at 23:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.