Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411515 Posts in 69376 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 04:57:33 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Bullet collision question (video inside)
Pages: [1]
Print
Author Topic: Bullet collision question (video inside)  (Read 1187 times)
hagel
Level 0
**



View Profile
« on: May 04, 2009, 06:36:16 AM »

Hi,
So I was surfing around my old links and found a video by Radnom. It shows a game he made where the bullets are instantshots without a projectile. I sent him a message on youtube but got no response. Anyway, here's the question:
Does anyone here know how to make similar bullets as shown in

video.

Pseudo code is appreciated

Thank you and goodbye
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #1 on: May 04, 2009, 06:55:47 AM »

These are called raycasts. They are quite common in games. I mean, it's pretty obvious what to do, just determine what shape in the world is the first to hit a given line, then draw that line. How you do that efficiently depends on how objects are stored in your world.
Logged
JLJac
Level 10
*****



View Profile
« Reply #2 on: May 04, 2009, 07:04:00 AM »

I'm gonna show you a pretty slow and stupid way of doing this, but it's simple, and once you get it to work you can propably optimize it by yourself.

It's about calculating where two straight lines intersect. Can be done with rounded shapes too, but that's another deal. If we have just vertical and horizontal terrain it's even easier. The way I do it is that I check collision between the projectile's trajectory and all the surfaces. You can propably create some kind of system that only checks those surfaces that are relevant, but we want to keep it as simple as possible here so just say all of them.

If A is the point where the bullet is created and B is the point where it has traveled as long as you want your weapon's range to be, you could calculate a straight line A to B that's defined y=kx+m. You can get k and m out of the positions of the two points.

Now if we want to check where this line intersects with a vertical surface of the terrain, we do like this:




We want hitpoint. We know that hitpoint's x-value is 4, since that is the position of the vertical surface. Thing is, we also know that hitpoint is on the line AB. So, hitpoint is on AB, and it's x-value is 4.

y = k*4 + m

We have k and m since earlier, so now it's easy to get the y-value of hitpoint. What we know do is that we check if this y-position is below the top of the vertical surface and above its bottom. If so, we have an intersection.

Then we do this with all surfaces. With the horizontals you do the same thing, exept y is the known, like this:

4 = kx+m

Save those hitpoints that are intersections, and later check wich one of them is closest to A. That's the one where the bullet impacts.

If you want tilted lines in the collision geometry you put two lines equal to each other to get their intersection point.

 Gentleman
« Last Edit: May 04, 2009, 07:08:47 AM by JLJac » Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic