User Tag List

Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 24

Thread: Trick to make object turn slowly toward a target

  1. #1
    No Products Registered

    Join Date
    Jul 2006
    Location
    Madison, WI, USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trick to make object turn slowly toward a target

    Here's a little trick I tried today that works great and replaces tons of math-heavy events. Some of you probably have done this yourselves and might say "well, duh", but I thought I'd share it since it had baffled me up until now.

    I wanted to make my AI-controlled ship be able to track me, but not turn instantly. A similar functionality was to make tracking missiles. I also use this to let the AI know when it's facing its target so it knows when to fire, etc.


    Originally I did all this crap with 8 events per object checking its direction and the relationship between X & Y coordinates of the target, etc. But then I discovered "look at" recently and had started using that, but the problem was that it was an instant change in direction.

    So here's what I've done instead. I just made an invisible active object called "AIAimBot" and anything that wants to aim at a target does the following steps:

    1) Set Position of the AIAimBot to the shooter's/projectile's current position.
    2) Make the AIAimBot look at the target (could be an object or a position, whatever you want)
    3) Set AIAimBot's Alterable Value D to AIAimBot's current direction minus the shooter's projectile's current direction. This gives you the difference between the desired direction and your current direction.
    4) Because of the way that direction numbers go from 0 to 31, you can end up with odd numbers here (like -17 or 28) that would make you turn in the wrong direction. So we need to "normalize" it - that is, make sure the difference in direction (the "delta") is never more than 16 or less than -16 (in other words, no more than a 180 degree turn). To do this, all you have to do is add 32 to AIAimBot's "D" value if it's less than -16 or *add* -32 to it if it's over 16. This will make sure your value is between -16 and 16.
    5) Now you know how far off your current direction the direction to your target is. You can use this for multiple things. For my AI and tracking missiles, I use it to check "FOV" (field of view). It the number is < -8 or > 8, then I know the target is more than 45 degrees off to one side. At this angle, my tracking missiles lose their lock and stop tracking. I also use this, though, to determine which direction to turn in if I *do* want to turn toward the target. It's as simple as this: if AIAimBot's "D" value is 0, I'm right on, but if it's > 0, I need to Set Direction to my current direction plus some number (I add 1 to make slow, smooth turns), and if it's < 0, then I need to subtract from my current direction. Usually I do this once every 10/100ths of a second so that the turning speed is at a constant rate but not to jerky.

    Another nice optional thing you can add on top of this is some random direction tweaks to make "drunken" tracking missiles. All you have to do is, once you've done the above, set up a timed interval event to add Random(3)-2 to the current direction of your projectile. This will still track but have some randomness to the tracking - kinda makes a sort of anime-ish drunken tracking missile behavior.

    One nice thing about this is that, since events are processed in linear order, you only need one AIAimBot for all of your objects - just make sure that when and object is going to use it, it repositions it, makes it look and calculates and normalizes the "D" value first.

    Hope this is helpful!

  2. #2
    No Products Registered

    Join Date
    Jul 2006
    Location
    Madison, WI, USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trick to make object turn slowly toward a target

    Here's a little trick I tried today that works great and replaces tons of math-heavy events. Some of you probably have done this yourselves and might say "well, duh", but I thought I'd share it since it had baffled me up until now.

    I wanted to make my AI-controlled ship be able to track me, but not turn instantly. A similar functionality was to make tracking missiles. I also use this to let the AI know when it's facing its target so it knows when to fire, etc.


    Originally I did all this crap with 8 events per object checking its direction and the relationship between X & Y coordinates of the target, etc. But then I discovered "look at" recently and had started using that, but the problem was that it was an instant change in direction.

    So here's what I've done instead. I just made an invisible active object called "AIAimBot" and anything that wants to aim at a target does the following steps:

    1) Set Position of the AIAimBot to the shooter's/projectile's current position.
    2) Make the AIAimBot look at the target (could be an object or a position, whatever you want)
    3) Set AIAimBot's Alterable Value D to AIAimBot's current direction minus the shooter's projectile's current direction. This gives you the difference between the desired direction and your current direction.
    4) Because of the way that direction numbers go from 0 to 31, you can end up with odd numbers here (like -17 or 28) that would make you turn in the wrong direction. So we need to "normalize" it - that is, make sure the difference in direction (the "delta") is never more than 16 or less than -16 (in other words, no more than a 180 degree turn). To do this, all you have to do is add 32 to AIAimBot's "D" value if it's less than -16 or *add* -32 to it if it's over 16. This will make sure your value is between -16 and 16.
    5) Now you know how far off your current direction the direction to your target is. You can use this for multiple things. For my AI and tracking missiles, I use it to check "FOV" (field of view). It the number is < -8 or > 8, then I know the target is more than 45 degrees off to one side. At this angle, my tracking missiles lose their lock and stop tracking. I also use this, though, to determine which direction to turn in if I *do* want to turn toward the target. It's as simple as this: if AIAimBot's "D" value is 0, I'm right on, but if it's > 0, I need to Set Direction to my current direction plus some number (I add 1 to make slow, smooth turns), and if it's < 0, then I need to subtract from my current direction. Usually I do this once every 10/100ths of a second so that the turning speed is at a constant rate but not to jerky.

    Another nice optional thing you can add on top of this is some random direction tweaks to make "drunken" tracking missiles. All you have to do is, once you've done the above, set up a timed interval event to add Random(3)-2 to the current direction of your projectile. This will still track but have some randomness to the tracking - kinda makes a sort of anime-ish drunken tracking missile behavior.

    One nice thing about this is that, since events are processed in linear order, you only need one AIAimBot for all of your objects - just make sure that when and object is going to use it, it repositions it, makes it look and calculates and normalizes the "D" value first.

    Hope this is helpful!

  3. #3
    Clicker Fusion 2.5 DeveloperSWF Export Module

    Join Date
    Jun 2006
    Location
    Turku, Finland
    Posts
    1,023
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a targ

    It works really nice! This is what I wanted for my game <img src="/center/images/graemlins/wink.gif" alt="" /> Thanks!

  4. #4
    Clicker Fusion 2.5 DeveloperSWF Export Module

    Join Date
    Jun 2006
    Location
    Turku, Finland
    Posts
    1,023
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a targ

    It works really nice! This is what I wanted for my game <img src="/center/images/graemlins/wink.gif" alt="" /> Thanks!

  5. #5
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a target

    That looks like it'll work fine - a decent way to do it without extensions.

    (By the way, were you responsible for the Temple of Nefertum puzzle in Hexen 2? It took me fully a year to get past that.)

  6. #6
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a target

    That looks like it'll work fine - a decent way to do it without extensions.

    (By the way, were you responsible for the Temple of Nefertum puzzle in Hexen 2? It took me fully a year to get past that.)

  7. #7
    No Products Registered

    Join Date
    Jul 2006
    Location
    Madison, WI, USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a target

    []That looks like it'll work fine - a decent way to do it without extensions.

    (By the way, were you responsible for the Temple of Nefertum puzzle in Hexen 2? It took me fully a year to get past that.) [/]

    Yeah, I still have the demo, so I've been trying to find a way to do it without any...

    Oh, man, Hexen 2! Hah! That was my first project at Raven. Yeah, that puzzle is something Brian Raffel (co-founder of Raven) and I toiled on for a while to get it right (especially the 9-button puzzle whose solution was different depending on which path you took to get there - I think the solution was engraved somewhere in the level, too). I think we tried too hard because a lot of people just stopped playing the game at that part. We should have realized we're making Hexen II, not Myst II... <img src="/center/images/graemlins/wink.gif" alt="" />

  8. #8
    No Products Registered

    Join Date
    Jul 2006
    Location
    Madison, WI, USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a target

    []That looks like it'll work fine - a decent way to do it without extensions.

    (By the way, were you responsible for the Temple of Nefertum puzzle in Hexen 2? It took me fully a year to get past that.) [/]

    Yeah, I still have the demo, so I've been trying to find a way to do it without any...

    Oh, man, Hexen 2! Hah! That was my first project at Raven. Yeah, that puzzle is something Brian Raffel (co-founder of Raven) and I toiled on for a while to get it right (especially the 9-button puzzle whose solution was different depending on which path you took to get there - I think the solution was engraved somewhere in the level, too). I think we tried too hard because a lot of people just stopped playing the game at that part. We should have realized we're making Hexen II, not Myst II... <img src="/center/images/graemlins/wink.gif" alt="" />

  9. #9
    No Products Registered

    Join Date
    Jul 2006
    Location
    Madison, WI, USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a targ

    []It works really nice! This is what I wanted for my game <img src="/center/images/graemlins/wink.gif" alt="" /> Thanks! [/]

    One quick word of warning - as described above, it doesn't work so well with multiple instances (copies) of a single type of object at once (like firing multiple drunken homing missiles). What I had to do was modify the first step to make the event use the "Pick one &lt;object&gt; randomly" condition, then store the resultant direction change (Alterable Value D) on the projectile, itself. Then, later, I just check every projectile to see if it has a "D" value and add it to their current direction (I use the same "D" value detection &amp; application to make shots come out at any relative angle to my ship - so my ships can shoot backwards, sideways, spreads of shots, etc.)

  10. #10
    No Products Registered

    Join Date
    Jul 2006
    Location
    Madison, WI, USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Trick to make object turn slowly toward a targ

    []It works really nice! This is what I wanted for my game <img src="/center/images/graemlins/wink.gif" alt="" /> Thanks! [/]

    One quick word of warning - as described above, it doesn't work so well with multiple instances (copies) of a single type of object at once (like firing multiple drunken homing missiles). What I had to do was modify the first step to make the event use the "Pick one &lt;object&gt; randomly" condition, then store the resultant direction change (Alterable Value D) on the projectile, itself. Then, later, I just check every projectile to see if it has a "D" value and add it to their current direction (I use the same "D" value detection &amp; application to make shots come out at any relative angle to my ship - so my ships can shoot backwards, sideways, spreads of shots, etc.)

Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. Making a object slowly wobble up and down?
    By Outcast in forum Multimedia Fusion 2 - Technical Support
    Replies: 10
    Last Post: 10th March 2012, 03:35 PM
  2. How do i make my car be able to drift (180 turn)?
    By GaryFreeman in forum The Games Factory 2 - Technical Support
    Replies: 1
    Last Post: 25th August 2011, 02:33 PM
  3. Replies: 0
    Last Post: 2nd January 2009, 09:50 PM
  4. Drop object on target
    By RCL in forum The Games Factory 2 - Technical Support
    Replies: 8
    Last Post: 2nd December 2008, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •