Public Group

# Beginner wishing to stop things from existing

This topic is 3923 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Still learning C++ and allegro, not very good. Wondering how to make code for "this laser will cease to exist if it hits the outer boundry of the map" =) Thank you =D

##### Share on other sites
You give the laser object a state, and tell the laser object to "die" once it reaches that boundary.

##### Share on other sites
i code in delphi and this would be a rough (and dirty) way that i would go about it just to give you the quick and dirty example. this should give you a place to start from...

if (mylaser.pos.x < world.boundary.minx) or (mylaser.pos.x > world.boundary.maxx) or (mylaser.pos.y < world.boundary.miny) or (mylaser.pos.y > world.boundary.maxy) or (mylaser.pos.z < world.boundary.minz) or (mylaser.pos.z > world.boundary.maxz)  then mylaser.alive := false;

##### Share on other sites
I don't think I understand =(

##### Share on other sites
He's testing all of your map boundaries. If the laser hits any of those boundaries (X,Y, or Z) it "dies" and no longer exists.

##### Share on other sites
Correct me if i'm wrong, but just to give the OP some additional info: The mylaser object would have a method called 'alive' which takes a boolean parameter. If the parameter is false, then the object instance is destroyed. If this is not correct, then please say so as i'll learn something myself (i'm a beginner too).

##### Share on other sites
Quote:
 Original post by yurianI don't think I understand =(
Nor do we, because you've given no context for your question.

How did you create your laser in the first place?
What do you mean by "cease to exist"?
Just what is a "laser" anyway and how is it represented in your program?
What have you tried so far?
etc...

Most of us aren't able to read minds y'know. [wink]

##### Share on other sites
hell guys, i was just giving an example. i didn't think it was going to be necessary to say that "in my example i'm assuming you have a variable with the properties..." etc. i didn't expect to need to be so explicit on an imaginary set of variables :/

anyways, to try to further help (and possibly hinder) go and get something that is square or rectangular like a book or a dvd case, or even something only roughly rectangular but close enough that you can just imagine it's rectangular.

Now let's imagine that everything within this box is the play-area of your "world". So if everything within the box is the play-area then it goes without saying that everything OUTSIDE the box is OUTSIDE the play-area / world.

Now pick a point outside of the box (use your finger or the end of a pen if need be, otherwise just imagine it with your mind... whatever works). Now you begin the tests, however, being the smart little creature you are you already KNOW that the point that you have chosen is OUTSIDE of your world and should at some point in your tests FAIL to be considered as being inside the play-area.

So now keeping in mind the point you have chosen you firstly say to yourself "ok, i'm going to start by assuming that the point i have chosen is INSIDE my play-area and valid (even though i actually know it not to be just by looking at it with my own eyes)!". Now you ask yourself "is the point to the left of the left-most wall of the box (play-area)? if it is then it is clearly outside of the entire play-area and i should make a note of this so i can remove the point when i'm past the test block of code, and if it isn't then perhaps the point is still within the play-area and so i don't need to do anything further at this time". Then you ask yourself a similar question with "is the point to the RIGHT of the RIGHT-MOST wall of the box? if it is then it is clearly outside of the play-area and i should make a note of that"... You continue this repeating process with the front wall and back wall, and then the top of the box and the bottom of the box.

So let's recap what has happened. You chose a point, started by assuming it was within the play-area (within the box), tested if it was sitting somewhere outside of the box and noted this. Now that you have tested against each of the walls you now need to see if you still maintain the original assumption that the point was valid or if somewhere within the testing code you discovered that it was actually outside... and if this is the case you can now get rid of it because you have decided you (as in YOU the original poster on this forum) don't want anything that has gone outside of the play-area to continue to exist.

if you then go back and look at the delphi code i posted above you should see that what i wrote in (far too many) words is the same as what i did in the example code. I assumed that the "laser" was within the world, tested to see if it actually sat somewhere outside of the boundaries of your world and if that is the case I get rid of the "laser" in whatever manner is required in how you have coded this project of yours.

I challenge you to find a point outside of the box that doesn't trip one, two, or even THREE (if it's outwards from a corner) of the tests in my original code example. It's like asking someone to count to infinity and when they get there to write down the number..... CAN'T BE DONE! In fact actually try a few different points and test them. Pick random points and then run through the tests to see how the simple if-statement always trips and will always detect a point outside of the world in it's tests. You won't find a point that isn't correctly identified as being inside or outside of the box. YOUR KUNG-FU IS WEAK!

i'm VERY tired and drowsy so sorry if this is gibberish and long-winded (or *cough* wrong)... enjoy.

[Edited by - nb on September 24, 2007 7:17:21 PM]

##### Share on other sites
Ahhh =D Thank you nb!!

But when I set alive to false, then what should I do next?
I'm sorry my question was not so well written.

##### Share on other sites
yer, um, i edited what i wrote. just read the last 3 paragraphs that's where i added and did stuff.

in the time it took me to edit it you already came back and posted a reply :P

in terms of "what to do next" that all depends on how you are doing things in your game or whatever you are creating. are these "lasers" in a dynamic array that you have created that only holds laser entities... is there a static array that holds all of your entities (the "things" in your game) and you give them all an "alive" tag or perhaps a "health" value?

to tell you what to do with unwanted "lasers" you first need to tell us how you create them in the first place!

• 11
• 20
• 12
• 10
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999853
×