Mouse shooting and leading target
Blitz3D Forums/Blitz3D Programming/Mouse shooting and leading target
| ||
Hi, does anyone have any code for mouse shooting (ie shooting shots in 3d space towards where the mouse cursor is at). Also a leading target function would be a great help, ie where to shoot to hit the target ship. Thanks in advance. |
| ||
there is a demo in the code archives: http://www.blitzbasic.com/codearcs/codearcs.php?code=559 maybe this is useful. |
| ||
Mouse shooting is pretty easy using CameraPick. As for leading a target, I wrote a function to do this in 2D as a cheat for a "Missile Command" clone somebody wrote: ;---------------------------------------------------------- ;cheat to aid aiming Function Cheat() Color 255,0,0 For M.Meteor = Each Meteor distance#=M\m x#=(M\Vector\x*distance)+M\ox y#=(M\Vector\y*distance)+M\oy For i=1 To 6 distance=M\m+M\speed*Sqr((x-gunx)*(x-gunx)+(y-guny)*(y-guny))/bulletspeed x=(M\Vector\x*distance)+M\ox y=(M\Vector\y*distance)+M\oy Next Oval x,y,4,4 Next End Function ;---------------------------------------------------------- Basically I use a little trigonometry to calculate where the target will be by the time the shot crosses its path if I shoot directly at the target. Then I repeat the calculation but this time figuring out where the target will be if I fire at the location I determined in the previous calculation. I repeat this several times (6 times in the code above, but the number of times you will need to do it will vary; enough times to be accurate, but not too often that you're slowing down the program execution,) narrowing in on the correct leading, to determine where to shoot at. In my code I draw a red dot at the spot, but obviously you could do whatever you want with the information once you've figured out the coordinates. Something along these lines could be done in three dimensions for a 3D game. |