by Alloysius on February 21st, 2008

Alloysius

Question

Help answer this question below.

In C++ format, what is a code to make object 'A' avoid object 'B', while moving to face object 'C'

eg: On the classic game "Asteroids" I want to get it so that the ai will move away from my bullets, while facing me and shooting me.

  • Like
  • Report

Answers. 2 helpful answers below.

  • by Takei-Shihan on February 23rd, 2008

    Takei-Shihan

    Make a function for when the player fires ... everytime the fire button is pressed, have the command that actually fires, and then have the function check for intersecting trajectories of both the movement of your target and the fired bullets to see if "player_fired_on_target" ... if true, have it call another function that alters either the speed or direction of your target (or both), if false, it just exits, does nothing, and lets your bullets miss, and returns to the main game loop that waits for the player to aim & fire again.

    • Like
    • Report

    6 comments | Post one | Permalink

  • by yeroco on February 23rd, 2008

    yeroco

    In general, what you are trying to do is known as "pathing", and is a fairly complicated problem.

    If the game is asteroids, you have the trajectories of all of the objects on the screen, so you can predict where they will be in the future. You also know where object C is. Your programming task is find a location where you can fire a shot at A (probably assuming A won't move till then), And make a
    path to that location, not hitting anything else on the way, then turn and shoot. Rinse and repeat.

    The tricky part is finding a point to shoot from.

    One simple starting point would be to try the location you're at now, if there isn't a path to shoot A, try picking a random location not too far away from your current location, predict where the objects will be when you get there, and see if you will have a shot at that time. If you do, check to see if you will run into any object on your way to that spot. If either of those checks fail, try another random location nearby.

    I don't think anyone can give you C++ code to that that task here. It's just too complicated, plus you'd have to show us what code you are starting from.

    Good luck. You might want to buy a book on computer game programming. You might find it interesting.

    • Like
    • Report

    1 comment | Post one | Permalink

Want to attach an image to your answer? Click here.

Did this answer your question? If not, then ask a new question or create a poll.

You're reading In C++ format, what is a code to make object 'A' avoid object 'B', while moving to face object 'C' eg: On the classic game "Asteroids" I want to get it so that the ai will move away from my bullets, while facing me and shooting me.

Follow us on Facebook!

Related Ads

ANSWERBAG BUZZ

Make object move one direction at time c
How to make object moving c
C format games
How to make object in c
C an arrow hitting a target