GreenFox,
2. You will not find Nuclear Glory for free.
3. A-I You do not necessarily need an object for your bullets, because in reality, they cannot be seen when shot. However, if your bullet is much larger, say a cannonball, and will be going much slower than the average bullet, then an object may be needed for added effect. If you don't need the object, don't use it. It will just create more calculations for the computer to handle each loop.
Think about what needs to be coded to
fake a bullet coming from a gun. This is the point where
raytracing is needed. Raytracing is the method of
sending out a ray or line segment from the shooter to the farthest distance a specific bullet can travel. Each unit of the segment is checked for
collision between that point and any other object(any objects that can be shot). As soon as there is collision, some action is taken by the program(some lines of code are ran, depending on the object collided with).
Think of a ray simply being a single bullet that moves one unit at a time, checking if it has hit something upon arriving at each new coordinate. Likewise, a ray is 2 to 3 values, which are coordinate values, x y z. The difference is that the bullet is not accually being placed on these coordinates each loop, but we are simply checking for collision between each point(a point is the position of an object on its x y and z coordinates) and each object.
Below is a code snippet of a 2D program, in top-down view, which allows the user to send out a laser beam, upon the press of a specific key. It also allows the user to move around. When the key is pressed, in each program loop that the key is pressed, the ray checks at
every point on the laser beam for collision of either, the man walking or the box
object. In more realistic games, like a 3D FPS, the raycasting will not need to check the whole line segment each loop. Instead, for reality sake, since a bullet can only travel so far each program loop, then the raycasting would only need to check within the distance of travel for that loop, each loop.
Raycasting:
randomize 360
set display mode 800,600,32
sync on
sync rate 80
REM << pre-position characters/conjure an npc start direction
px# = rnd(800)
py# = rnd(600)
npcx# = 300
npcy# = 150
npcangle = 315
REM << preset radius value
radius = 15
REM << constant;bulletdistance(the distance the bullet will travel)
bulletmaxdistance = 400
repeat
inc tredtime,1
REM << create npc guard motion
if tredtime = 200
if npcangle = 315
npcangle = 135
else
npcangle = 315
endif
tredtime = 0
endif
REM << make npc move
npcx# = npcx# + cos(npcangle)
npcy# = npcy# + sin(npcangle)
REM << player movement
if scancode() > 0
if leftkey() = 1 then pangle = wrapvalue(pangle - 2)
if rightkey() = 1 then pangle = wrapvalue(pangle + 2)
if upkey() = 1
px# = px# + (cos(pangle) * 1.2)
py# = py# + (sin(pangle) * 1.2)
endif
if downkey() = 1
px# = px# - (cos(pangle) * 1.2)
py# = py# - (sin(pangle) * 1.2)
endif
endif
REM << cast ray
if spacekey() = 1
laser = 1
REM << store player's current position and angle for raycast calculations
lx# = px#
ly# = py#
langle = pangle
REM << reset specific variables
hit = 0
t = 0
REM complete raytrace in one frame
repeat
inc t,1
REM << cast ray in direction of character;ray check position is increased(here) by on unit each repeat
REM << the whole line, up until an collision, is checked each loop
lx# = lx# + cos(langle)
ly# = ly# + sin(langle)
REM << call function;check for ray collision;value returned
hit = check_collision(lx#,ly#,npcx#,npcy#)
until hit > 0 or t = bulletmaxdistance
else
REM turn off laser;reset t;reset hit
laser = 0
t = 0
hit = 0
endif
REM << create explosion effect when laser is hitting something
if hit > 0
REM << increase radius of explosion
if direction = 1
inc r,2
if r > radius then direction = 0
else
REM << decrease radius of explosion
dec r,2
if r < 1 then direction = 1
endif
else
REM << reset explosion radius to 0 if no object is hit
r = 0
endif
REM << call function;draw 2D graphics to screen;no return value
draw_to_screen(px#,py#,pangle,npcx#,npcy#,npcangle,hit,r,lx#,ly#,t,laser)
sync
cls
until mouseclick() > 0
end
REM << laser beam collision
function check_collision(x#,y#,x2#,y2#)
REM << reset variable to return a 0 if no object has been hit
hit = 0
REM << check for collision of both boxes
if x# => 200 and x# =< 400 and y# => 250 and y# =< 400 or x# => 550 and x# =< 650 and y# => 150 and y# =< 250 then hit = 1
REM << check for collision of guard
if sqrt((x# - x2#)^2 + (y# - y2#)^2) =< 10 then hit = 2
endfunction hit
function draw_to_screen(px#,py#,pangle,npcx#,npcy#,npcangle,hit,r,lx#,ly#,t,laser)
REM << draw npc
ink rgb(150,0,0),0
circle npcx#,npcy#,10
line npcx# + (cos(npcangle) * 10),npcy# + (sin(npcangle) * 10),npcx# + (cos(npcangle) * 20),npcy# + (sin(npcangle) * 20)
REM << draw player
ink rgb(150,150,150),0
circle px#,py#,10
line px# + (cos(pangle) * 10),py# + (sin(pangle) * 10),px# + (cos(pangle) * 20),py# + (sin(pangle) * 20)
REM << draw laser beam
if laser = 1
ink rgb(255,255,0),0
line px# + (cos(pangle) * 20),py# + (sin(pangle) * 20),px# + (cos(pangle) * t),py# + (sin(pangle) * t)
endif
REM << draw boxes
box 200,250,400,400
box 550,150,650,250
REM << draw explosion
if hit > 0
ink rgb(255,255,0),0
circle lx#,ly#,r
endif
REM << instructions
ink rgb(255,255,255),0
center text 400,560,"Press spacebar to fire laser beam"
center text 400,580,"Press a mouse button to end program"
if hit = 0 and t > 0 then print "MISS"
if hit = 1 then print "BOX"
if hit = 2 then print "NPC"
endfunction
Copy and paste this program into your DarkBasic editor and then run it. Study the multiple
REM comments which are throughout the source code. Ask if you have any specific questions. Check the forums, using the word
raycasting.

+NanoBrain+