Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / I have a nublar question about enemy ai movement in a shooter esque project

Author
Message
Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 21st Jan 2008 15:44 Edited at: 21st Jan 2008 15:49
*phew* That was a long title, but neccesarry so you know what I'm talking about and will rid this thread of uneeded sarcasm. So to it then, I am using the 2d Tutorial Shooter setup coded and written by TDK, so if that looks familar, its his, i just am adding to it and shaping it to my own project.

Anywho, my problem as stated in the title is the enemy ai's movement, I have my box *enemy's* coords stored into variables but..here comes the nublar question, even after searching LA Forums, I can not come up with an answer.

BUT WAIT, even though I am tired and have two articles due to rami for U4 I still have code of my own, *cheers*, and so here it is in segs.

My init code


And then, I had written out a fancy movement system along the lines of if Ax< 0 then inc, 2 and so on and so forth, i'm not showing it because in my tiredness, deleted it. So, if someone could tell me if i'm on the right track by doing the following that would be great.

What i've tried so far.

If statement here obviously
Ax< 0 theninc,2
Ax>640 then dec,2
Ay<0 then inc,2
Ay>640 then dec,2

If A<0 then inc A
If A>640 then dec A

So I'm just not sure what to do, so I think this is a mature if rather snarky post for my fatigued self now, so if you can help thank you very much, if not well maybe i'll figure it out anyways. Happy coding!!

Zombie

And the whole source, thought it would be good to have, note though enemy ai code is scarce as it was deleted.




Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd Jan 2008 04:28
I don't understand A = Box
You don't want to be checking right at the edges of the screen like that or your aliens will go off screen, take the width of the alien into account e.g. if Ax > 640-Awidth then move = move*-1
use a variable like move and you can add it to the aliens position and they'll go in the correct direction.

You don't say how you want the aliens to move?
If you want them to move like space invaders you need to consider a few things.
The number of aliens dictates their speed (fewer are faster).
The position of the aliens dictates how far across the screen they travel before changing direction.
Every time they change direction then must also drop down.
I would recommend making an array that stores whether an alien is alive or dead (in a grid).

Is this the kind of movement you mean?

Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 23rd Jan 2008 13:17 Edited at: 23rd Jan 2008 17:18
Sorry for not posting for so long there obese , I was trying to get the box *enemy* to move around the outside of the screen like this

____________________


____________________


Comes down the sides too, so that's what I was wondering on that, as far as A=box, i've just no clue on what to do, I had A = box, then ay=100, ax=100, to store coords and then I was checking the screen to move it so like..

if ay=100 and a=100 inc ay



I like the idea of an array, I'll give it a shot, thanks for posting in.

although I just realised that I haven't a clue how to make a grid system .

Ian


Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Jan 2008 18:09 Edited at: 23rd Jan 2008 18:13
ah ok
here is my movement code from Vanishing Point

It looks very complicated and I'm actually going to have to take a minute the work out how it works ...

Ok, the variable move tells you which direction the ball is moving; if it equals 0 it's moving right, 1 is left, 2 is down and 3 is up.
Each time the ball hits a boundary it's movement changes by altering the move variable.
You may be wondering how I can have (move=2) in the middle of an equation, this is very useful because if move=2 this will return 1, if move<>2 this will return 0. If you're still not getting it I'll explain more.

Now I think you want the enemy to move around the edge of the screen so lets say
right=0
down=1
left=2
up=3

and every time we hit an edge we'll add 1 to move to change direction.


Here's a working example, just ask if you want me to explain anything


Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 23rd Jan 2008 19:25 Edited at: 23rd Jan 2008 20:18
Thanks obese, I owe you a hug or a handshake or a drink or some other like that . I get what you're saying in the code, and it all seems to flow nicely, I started to add to it and its going great. Run into an x-axis issue but I believe I've worked it out, this is some fast code, everything runs smoothly.


Nvm, got it, its the coords, I see now...okay I think I can do the mutiple creation code on my own, time to see if I can learn.

Although, I do have a question, since you combined (move=2) in the middle of an equation would I be able to mix it up more with a litte bit of math to throw the player off or would a randomize be better? Annnnd, how did you come up with the example, I actually wouldn't have thought of that the way you did it. Then again, i'm not in the coding frame of mind 24/7 as I'd like to be.

Code follows




still confused, maybe I didn't get it like i thought before, can you see what i've done wrong?




Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Jan 2008 23:01 Edited at: 23rd Jan 2008 23:19
Quote: "how did you come up with the example?"

I came up with it when I was making Vanishing Point; I needed the ball to move right until it hit the side of the screen and then change direction and move left, and vice versa.
My original thought was to make a simple flag that changed once the ball hit a wall.
if X<0 or X>1024 then move=1-move
I have to thank Mr. Tank for that very clever and simple bit of code that toggles a variable between 1 and 0, he used it to make a toggle switch and I applied it here.
Then the obvious thing to do next is to check the value of move.
If move=0 then inc X,speed
If move=1 then dec X,speed

Recently I've been trying to cut down on the amount of IF statements I use, I don't know if this helps performance but its fun to try and work out how to use conditions in a mathematical way. So what I've done to get the final equation is basically turn the statement inside out; instead of checking move and then altering X, I'm altering X depending on the value of move. You must have balanced equations in maths, I think of it as a similar sort of thing, this is the opposite and equal to the previous if statement.
inc X,(move=0)*speed - (move=1)*speed
I'm only just starting to use this kind of code but already it looks very powerful, it basically allows you to slot an if statement into any equation.

Quote: "still confused, maybe I didn't get it like i thought before, can you see what i've done wrong?"

The problem with your code is that you have two circles travelling at different speed but only one variable to say which direction they should travel in, so only once both circles have hit the edge will they change direction, unfortunately the faster circle is way off screen by then so you can't see it.
I don't like your variable names: A and B are coordinates just like X and Y, the only difference is they apply to a different circle. This calls for an array! It is important to be able to recognise when to use arrays, try to imagine what you would have to do if you wanted 100 circles, if the answer is write a hell of a lot of code then you need to change your approach.
Make an array that stores data for 3 circles (it is wise to leave 0 blank as you'll often need 0 to mean "nothing" instead of pointing to an entity), you'll need to store the X and Y coordinates, speed and direction for each circle.

Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 24th Jan 2008 04:29
ah so

dim circle (3)

circle (1,x,y,speed)
circle (2,x,y,speed)
circle(3,x,y,speed)

okay now I want to learn code darnit, its so aggravating to want to learn and not be able to practice it and test if you're doing it right. Now something seems off to me from writing that, I think I need a multidimensional array, is that right or is the above correct, BASIC seems to be a common sense thing with its syntax so I just wrote it out as I could imagine DB doing it.

I like that you're learning not to rely on if's so much and learn more maths, its great because I understand everything in your movement code that you wrote, I just wouldn't think to do that. Anyways, gonna stop getting down on myself.

So am i close there with the array or do I need to branch it out more like

dim circle (0,3)


Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 24th Jan 2008 18:46
Your second attempt was closer
Each circle needs 4 variables: X, Y, Speed, Direction
So the array would look like this
DIM Circle(3,3)
X=0:Y=1:Speed=2irection=3


I have to go to work now but I'll check back later

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 00:25
@Obese87
I should really stay out of this since you are doing such a good job but I believe a conditional test is faster than the equation approach, at least in the case of two multiplications. A conditional statement only proceeds if the condition it encounters is met. So If a=500 and b=20 wouldn't even go on to check b if a hadn't been met and any code relating to the condition wouldn't be checked. But in the case of


all of the calculations are performed every iteration. Speed is calculated 4 times as opposed to being a constant and the value of the increment is calculated twice as opposed to being a constant also; and multiplication by itself is quite expensive in terms of processing. Just to be sure, you might want to run a benchmark to get an idea of processing time - I could be completely wrong.

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Jan 2008 02:27 Edited at: 25th Jan 2008 02:48
@Latch
hmm I was looking at those * and wondering if they were gonna slow things down. The problem is I don't know what the computer actually does with an IF statement, do you?

haha I just noticed a stupid waste of power
inc X, (move=right)*speed - (move=left)*speed
Could be
inc X, ((move=right)-(move=left))*speed
I'm not sure if I need all those brackets but I get wary around =, there should be a different symbol to assign values in my eyes.

What do you mean by benchmark? Make a test program?
How do you test the speed of a program?

This prints the FPS (is that what you mean?)
It averages about 350fps


The original was slightly slower overall, it took much bigger drops if FPS.


Using select case didn't effect fps much but the circle moved much slower.


Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 02:35
Quote: "what the computer actually does with an IF statement, do you"

It's supposed to skip everything that follows the condition test if it doesn't match, but DBC might be quirky in that regard. But this test seems to indicate it works correctly:



Enjoy your day.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 02:38
benchmark:

Run thousands of iterations using one method and get the approximate clock time it takes to run. Then do the same test using another method and compare the times to see which takes less. In general, if the computer has to do more, it will take more time.

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Jan 2008 03:01 Edited at: 25th Jan 2008 03:04
Ok I think I know what you mean now

The equation is much faster, it takes about 75% of the time.

Now let's try with multiplication in there!

now the conditional is using 71% of the equation's power

I just thought of a good reason for using equations: even if the condition isn't true a variable will still be assigned a value but the value will be 0.

Sinani201
18
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 25th Jan 2008 05:52
Quote: "X=0:Y=1:Speed=2irection=3"

Put that in a code snippet, there was an "accidental smiley" that isn't supposed to be there!

Seriously, how do you make the little blue text come up below your message?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Jan 2008 07:25 Edited at: 25th Jan 2008 07:30
@Zombie
Sorry, highjacking your thread a little bit - although it still has to do with the movement code. If you object I'll stop after this post. It seems OBese's equation method is faster than the conditionals which has me intrigued.

@OBese87
in
inc X, ((move=right)-(move=left))*speed
This isn't accepting input from a user is it? This is just a calculation based on the position of an enemy, right? I was looking at your equation as redundant because I thought it was taking user input then doing a calculation on it again to solve for direction - this was what was behind my post. But in terms of user input, would it be faster to not use an if upkey()=1 etc and just assign it to a variable right off the bat
up=upkey()
and then use your equation method?

Enjoy your day.
Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 25th Jan 2008 16:40
Latch- NO no no, please go ahead, anything I can learn is very much appreciated, I don't get to study much and this forum teaches me a lot. I actually find your conversation very interesting.


Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Jan 2008 19:20
Quote: "But in terms of user input, would it be faster to not use an if upkey()=1 etc and just assign it to a variable right off the bat
up=upkey()
and then use your equation method?"

haha that's exactly what I do
Not sure if it's faster though it seems like it.

Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 25th Jan 2008 20:14
Right then, so I"ve literally just started working on this, so here is what I wrote, i'm going to keep on going with obese's code if its okay with him.


Dim Enemy# (3,4)
Enemy#(1,x,y,speed,left)
Enemy#(2,x,y,speed,right)
Enemy#(3,x,y,speed,down)
[\code]

I know its wrong, don't know what i'm doing wrong but I really don't want to be stuck in the pong world forever.


Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Jan 2008 01:32 Edited at: 26th Jan 2008 01:35
@Zombie
You've nearly got it but you don't understand arrays at all!
Another word for an array is a matrix; like the 3D matrices in DB, a matrix is an array (that's where the name comes from) of numbers stored in a grid-like way. You enter parameters when calling an array to point to the desired location.

Imagine you were making a game that required a map.
Here's a simple map that I made for a text RPG

The orange squares are the town, the light green squares are grassland, the dark green squares are forest, the grey square is a cave, and the blue squares are the seas.

So to make this map we first have to make an array to store the data in.

The first dimension is the X coordinate and the second dimension is the Y coordinate map(x,y).

What this means is that in the top dimension of our array there are 5 boxes (including box 0), and inside each one of those five boxes there are 5 smaller boxes (the y coordinates). Inside the Y boxes are the values we have stored.

lets store our map in our array


Do you get it now?

Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 27th Jan 2008 11:19
Obese,

Wow thanks, that was a great explination, but to make sure I've really understood it I'll write out what i'll be doing.

So using the arrays I'll be storing the positions of the screen right? And, from that I can just call individual parts of the array to make my enemies move, is that right?

Thanks for taking time to explain too, it is very appreciated.

Zomb


Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 27th Jan 2008 16:08
I'll give you a clue; going back to the example, say we wanted a second map, maybe for a different town that our player could travel to. We'd do something like this.

Now we have 3 coordinates: which map we are pointing to, x and y. This is now a 3 dimensional array, so you could say we have a cube and each cross section of the cube is a separate map.

I think I am confusing you a bit with this example, you seem to be thinking of making an array for the screen and then storing the players position in the correct box; that's not a very good way to do it.
You need to use your array differently in your game, you aren't using it as a map, you aren't using the array like a grid, you just need some boxes to store different values in.


Zombie 20
18
Years of Service
User Offline
Joined: 26th Nov 2006
Location: Etters, PA
Posted: 27th Jan 2008 16:29
Oh I see, the array just helps keep everything organized, okay that makes sense. Therefore, with it all organized, all you have to do is call object 1,2,3,etc...right? I don't want to jump off the gun without knowing, but I'll go on my own again if you're tired of explaining this to me , I can be a little thick from time to time.


Coffee coffee coffee coffee coffee coffee cappacino, JAVA!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 27th Jan 2008 17:05 Edited at: 27th Jan 2008 17:07
give it a go
I think the map example confused you a bit (my fault), but that is how arrays work, even your array.
Look at the map again, think of the X coordinates as your objects (the first dimension), and think of the Y coordinates (second dimension) as the different properties of your objects i.e. row 0 would be the X position of the object, row 1 would be the Y position, row 2 the speed and row 3 the direction.

The only difference with your array is that you aren't using X and Y as separate dimensions because you're not mapping anything.

Login to post a reply

Server time is: 2025-06-04 02:12:14
Your offset time is: 2025-06-04 02:12:14