Make an enemy 'track' the player through rotation.
BlitzMax Forums/BlitzMax Programming/Make an enemy 'track' the player through rotation.
| ||
hey folks! I need to make an enemy gun turret 'track' the player, and turn to always be facing him(Although preferably at an adjustable speed, so I can slow his turn rate if need be). So how might I do this? I've made it so the Gun turret can fire in the right direction, but not actually be pointing the right way. I'm using a variable to determine rotation, called Enemy.r, and need to use this if that is at all doable with any suggestions you might have. Thanks! |
| ||
count angle between player and turrent, you can use atan2(turrenty-playery,turrentx-playerx) function. Then interpolate missing angle :). |
| ||
count angle between player and turrent, you can use atan2(turrenty-playery,turrentx-playerx) function. Then interpolate missing angle :). The only problem with that Polan. Is I've never used atan2, I'm not sure how it works, and I'm not very good with angles ;) If you could clarify that in a code example that would be great. Sorry for my noobishness, but there are parts of Blitzmax that I don't know, and angles is one of em :) Thanks! |
| ||
atan2 returns the destination angle:destinationAngle = atan2(destinationY-sourceY,destinationX-sourceX) that will give you the direction that your bullet got to trable or the direction the turret has to be facing. To get your bullet to travel in the direction of the obtained: dx = cos(destinationAngle) dy = sin(destinationangle) all you are going to have to do from here is add that to the x and the y of the object you want o move and it's going to move in the correct direction. x = x+dx y = y+dy now to change the speed of the object all you have to do is this x = x+dx*speed y = y+dy*speed Last edited 2010 |
| ||
Lol, thanks Jesse! You always seem to be the one to give me a hand ;) destinationAngle = (destinationY-sourceY,destinationX-sourceX) This 'should' be all that I need. I already have the bullets traveling the way I want. I'll post back though letting you know the outcome. Cheers! |
| ||
sorry Werdna. I don't know How I forgot the most important part the atan2. fixed now. You always seem to be the one to give me a hand ;) I take a glance at the boards every now and then and if there is someone I can help with something I do. there are some topics that I haven't gotten into which I should and those are usually the ones stay away most of the time. If you need help with something and you think I can help you just let me know and I will. No strings attached. |
| ||
I don't know How I forgot the most important part the atan2. Np, I kind of guessed that was supposed to be in there ;) That single line of code worked out just fine, my Gun Turrets now act how I want them to, and I'll have a new test build(And youtube video) of my current game ready in 1 or 2 days. Cheers! |
| ||
There are a whole bunch of samples of this throughout the forums, just search for atan2. |
| ||
Asource is your current angle, Adest is the angle the other object is and smooth is how quickly you want it to turn.. add the return value to the current angleFunction Rotarydir:Float(Asource:Float,Adest:Float,smooth:Float) Local dir:Float=0 If Asource>Adest Local Diff1:Float=Asource-Adest Local diff2:Float=(360.0-Asource)+Adest If diff2<Diff1 dir=diff2/smooth Else dir=Diff1/smooth*-1 EndIf Else If Asource<Adest Local diff1:Float=Adest-Asource Local diff2:Float=(360.0-Adest)+Asource If diff2<diff1 dir=diff2/smooth*-1 Else dir=Diff1/smooth EndIf Else dir=0 EndIf EndIf Return dir End Function |
| ||
really handy tools to have around, thanks for the clean info all, much appreciated. |
| ||
Thanks for the example Skully :) |