Public Group

# problem with spawning and collision

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

## Recommended Posts

ok i'm in the process of my first game, the basic overview is the player controls a plane at the bottom of the screen, it only moves back and forth along the y, and random good things and bad things spawn from the top my 2 problems spawning: i have 1 island(good thing) and 3 clouds(bad things) and i have htem spawning at random places when they exit the screen(y<screenheight), the islands move straight down at a fixed rate, the clouds move at a random speeds and with slight sideward movement as well (range of x -3 to x +3) but when the clouds spawn, then get the same vales, they always go at the same speed, and at the same sideward slant. does anyone know how to make them spawn independantly? collision: i have the collision set up for the clouds, i have the plane set up with 5 lives, with every hit taking a life away, the problem is that when it hits the plane, every frame takes away a life so the lives get to zero almost instantly , how would i fix this? thanks

##### Share on other sites
about spawning: what random number generator are you using? Many of them require you to feed
them with a "seed" every time you need _new_ random value. Assuming ur using C/C++ and standard random() fun, you would call srand( seed ); random_value = rand(), every time you need random value, using time(NULL) or any volatile value as seed.

about collision: just make ship immortal for a few secs every time it takes damage.

##### Share on other sites
We will need to see some code to answer your first question, but I'd imagine you want to do something like this

cCloud *c = new cCloud[3];for(int i = 0; i < 3; i++)  c.SpawnAt(GetRndPos(), GetRndVel());

For your second question there are numerous ways to handle what you described. Here are a few rando's not in any order.

When a collision is detected you could:
1.) Flag the object as already having contributed to the ships damage.
2.) Flag the ship as already taking damage from this object.
3.) Remove the object from the scene.
My suggestion:
4.) Initiate some flashing "safe" mode where the plane can't receive damage for a few second.

This should give you time to get out of the cloud (why are clouds causing damage anyway? shouldn't the island cause damage? but anyway), it dosn't require storage and interation of a "already collided" map, and if you should happen to hit the same object again after the "safe" time it will still cause damage.

Good luck.

edit: GoBoss double jumped me, I must have took a while to type this post.

Also as he mentioned, if you already have a random function, make sure you don't see the random numbers everytime you request one. Otherwise they will always come back the same as each other. Seed the random function one time at load time.

##### Share on other sites
thanks alot for the help, i'm gonna try those ideas,

the random num generator i'm using is

srand(time(0));
rand()

its in a function that's called everytime the cloud leaves the screen

and yeah the game presmise is really stupid, i'm wokring through a book called "the express line to game programming" and its a project they introduce in the book, the idea is that you fly over the island to drop mail and get points, and avoid trhe clouds. the book gives general ideas on how to do things, but the examples are in python, so i knwo i can do everything in the book with SDL, but some of the things i have to do, pygame has built in library functions for.

here's the random function i wrote

int random(int num)
{
int res;
int multiplier;
srand((int) time(0));
int result = rand() % num;
multiplier = rand() % 2 + 1;
if(multiplier == 1)
multiplier = -1;

res = multiplier * result;

return res;
}

just to avoid the confusion, the multiplier variable is an attempt to be able to make the clouds go left and right

##### Share on other sites
You don't need to call srand() every time you need a randomly generated number: it seeds the random generator, so what you're essentially doing is resetting the random generator each time. You only need to call srand() once, when your program starts or such, after that you should leave it as it is. :)

I wouldn't add a multiplyer to a random function, though: as you admit, it's confusing, and it doesn't really belong in a function that, by it's name, should only return a random number.

##### Share on other sites
ok i got the constant damaging to stop by making the cloud respawn on collision. thanks alot for the help, i'm still working on the randomness

another question thats probobly really simple, to make a sperate instance of an enemy, i just set up the Cloud class, the has the behavior built in, then i just create seperate instances of that class, one for each enemy

but i see alot of people use an array to get seperate enemies, is this a better way? and if it is, would you just make the object an array? ie.

Cloud cloud[3];

Cloud cloud1;
Cloud cloud2;
Cloud cloud3;

?

##### Share on other sites
I wouldn't use an array here. I wouldn't use separately named variabeles either. I'd use a std::vector, or a similar STD container:

#include <vector>std::vector<Cloud> clouds;clouds.push_back(Cloud());clouds.push_back(Cloud());clouds.push_back(Cloud());for(int i = 0; i < (int)clouds.size(); i++)	clouds.DoWhatCloudsUsuallyDo();

See what it does? You can add as much Clouds as you want, without needing to know beforehand just how much, or dangerously fiddling with pointers when you don't really need them (as honayboyz does: he creates multiple Cloud objects on the heap memory using the new [] operator, but if you don't call delete [] on that pointer when you're done with those Clouds, you'll get a memory leak).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 27
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634099
• Total Posts
3015526
×