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.

Newcomers DBPro Corner / Simple AI Help

Author
Message
Zyronagon
16
Years of Service
User Offline
Joined: 6th Mar 2008
Location: USA
Posted: 15th Jun 2010 05:05
I'm creating a very simple game, you play as a cube that dodges enemies (spheres). My code probably seems unorganized to most, but I've included it just in case. I'm trying to get the spheres to constantly move forward while rotating towards the player, sorta robotic, zombie-esque. How would I go about doing this? I've started something, yes, there's some errors in my code, but I don't think it's going to work and I think there's a better way to do it.

I was going to make this a top-down game, but I decided that this look pretty cool. It's all done with the basic 3D commands, so just copy the code if you want to see it.

On the bright side, I finally realized how to apply arrays to my game! lol.

Full code:


Specifically, the part where I set up AI:
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 15th Jun 2010 06:22

good idea with this statement!
couple flaws in your code tho.

The problem with this is that AtanFull returns a float, 99.9% of the time it'll be some long decimal like 30.245236, and not *exactly* 30 (and since EnemyAngle# is just incremented by 1, it's approximately a whole number). One usually avoids equal or doesn't equal signs when dealing with floats. So, witha check like this for floats, one does this:
in stead of:
if a#=b# then ...

this:
if absolute_value(a#-b#)<tolerance# then ...

where absolute value returns the positive value of whatever it gets.


Along with that, you also have to remember that angles are circular, in other words ...-1080=-720=-360=0=360=720=1080=...
so, what you really want is this:
if wrapvalue(absolute_value(a#-b#))<tolerance# then ...
wrapvalue returns the angle, but bounded between 0 and 360. (wrapvalue(360)=0, wrapvalue(359)=359, wrapvalue(361)=1, wrapvalue(-1)=359, etc.)

Your third problem is that you never update enemyPosX# and enemyPosZ#().

If you're not familiar with functions, try this for an absolute value function:



Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 15th Jun 2010 07:39 Edited at: 15th Jun 2010 07:50
I'm from the camp that likes to simply point objects and move them toward other objects rather than use atanfull, but it can work either way.

I edited your code and installed some TYPE variables. My hope is that you can learn from it, even if you don't end up using it. I also tried to organize it so that repetitive code was placed into subroutines. I declared some variables as global in case you want to change the subroutines into functions at some point (this is not necessary when using subroutines, it's just a force of habit on my part I guess).

The enemy spheres will look at where the player is and move towards it. Some move faster than others. If they collide with the player, the player will turn red and fade out. The player and enemies are then re-spawned at their original locations.



I hope this is helpful.

LB
Zyronagon
16
Years of Service
User Offline
Joined: 6th Mar 2008
Location: USA
Posted: 15th Jun 2010 15:20
Neuro Fuzzy:
Wrapvalue: I've lightly thought about this before and wondered if you would have to set it up manually, thanks for showing me this command.
I assume that in the statement
that tolerance# should be a number slightly greater than the number I'm using to increase the angle, correct?
Also, what are a# and b#?


LBFN:
Wow, great code! I've never used types before, but I'm glad I can refer this as an example for when I learn them. That's one of the reasons it took me so long to understand arrays, I knew what they were, but not how to apply them.

There's only one thing that strikes me as odd in your code though. Why did you declare certain numbers (q#, x#, etc.) as a float? I thought it wasn't necessary to declare the type of variable in DBP.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 15th Jun 2010 16:13
Quote: "Wow, great code!"

Thanks

Quote: "I've never used types before, but I'm glad I can refer this as an example for when I learn them."

At first, TYPE variables seem a little daunting, but if you take the time to learn how to use them, they can help you keep your code more organized and understandable. I pretty much don't write anything without using TYPE variables anymore. It is definitely worth the time to learn them.

Quote: "Why did you declare certain numbers (q#, x#, etc.) as a float? I thought it wasn't necessary to declare the type of variable in DBP."


TYPE variables are assumed to be integers, so if I need float precision (which I do with world coordinates), I need to declare them as such. If you needed it to be a string variable, you would also have to declare it as such. Technically, I did not need to list the integer TYPE variables as such, but I always do. You don't have to have a # after the TYPE variables that are floats, but I do this to be consistent.
Zyronagon
16
Years of Service
User Offline
Joined: 6th Mar 2008
Location: USA
Posted: 17th Jun 2010 01:09
LBFN:
A little unrelated, but I'm new to collision systems. In your code, you saved the old position of the object and made that its new position. While this works, the object sticks, so is there any way to make the object slide on the wall? I mean, it's okay for a game like this, but what if I were making an FPS or something where the object rotates?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 17th Jun 2010 20:37
Sliding collision could be installed in this code easily using DBP's commands. However, I would suggest you download sparky's collision dll and use it. It can be found here:
http://forum.thegamecreators.com/?m=forum_view&t=74762&b=5

If you search for 'Sparky' on the forum, you can find out a lot of info on it and I believe there are some example programs that come with it. I am pressed for time now, but if you need anything further, please let me know.

LB


Login to post a reply

Server time is: 2024-11-24 14:46:56
Your offset time is: 2024-11-24 14:46:56