Best way to do things ( random grass generator)

Posted By: DestroyTheRunner

Best way to do things ( random grass generator) - 01/24/08 15:55

Hi
I have a function that generates 500 sprites of a grass image(32bits TGA)
in randomly positions over the level.
I found out that I needed a bit more than 1000 to cover my level decently.
So i made a function that any sprite that is X(eg. 5000) quants farther from the player, gets its invisble flag on.(for speed and rendering improvment)

But IF i make a function that maintan a max number of sprites 200(which is enough to fool the vision of the player) and as i walk, it creates and removes sprites using the same distance comparison.

Which one do you think is the best? ( speed, memory, rendering...)

ps. IŽm not using no ones particular generator code and no generator tutorial code(if there is any).

thanks
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/24/08 15:59

You should create only once, and then move them on positions around the player instead of removing and creating again. That's the fastest way I can think of.
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/24/08 16:13

but what about memory?
1000 grass sprites means:

1000 entities + 1000 functions running.

Im kind of gathering PROS and CONS about this two methods.
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/24/08 17:00

Quote:

But IF i make a function that maintan a max number of sprites 200(which is enough to fool the vision of the player)



200 grass sprites means: 200 entities + 1 function running.

you create the grass sprites, store their pointers somewhere and check the distance of all the sprites from the player each frame. This would give 200 loops per frame, that's fine.
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/24/08 17:32

ok
sorry if i couldnt be a bit clear...
200 grass sprites only apply to the vision of the player. example, the level
has to have 1000 grass sprites to cover the land decently, but i need 200 visible.
But im getting a problem with it, IŽll explain to you the real thing so you can have a more clear vision of the problem:

"Basically im throwing rocks at a castle, and when it hits the castle wall, it disappears and create 10 small rocks with c_move and c_trace running on each of them ( each small rock has 12 faces), then they fall to the ground and some seconds later they disappear and all their functions terminated."

NOW the problem, if there is more than ~300 grass sprites in the level EVEN WITH THEIR FLAG INVISIBLE ON, and i throw the rock at the castle, when it creates those 10 small rocks, the FPS drops from 60 to 35/40! until they disappear and it comes back to 60.

So the most i could get was 200 grass and the FPS still drops 8 to 10fpss(~52fps)

Did you get the Drama?

(my current pc specs is AMD Athlon 1600mhz 256mb ram Video Card GeForce4 but i get the same results in a P4 2.6 512ram video card ATI Radeon 345m 128 shared with the ram memory)
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/24/08 17:36

I think I know why, it's because you have grass sprites everywhere, but not visible. c_move detects the sprites and collides with them. Either turn of sprite collision in c_move/trace, or do as I told in my previous post.

if you only have 200 entities visible max, you really only need to make 200 grass sprites, not 1000. The level doesn't need to be filled if you dont see them anyway. If you create 200 grass sprites around the player at random locations, and you move the player forward, the grass sprites outside the visibility range should be relocated to some new location around the player that just appeared in visibility range.

That's the most effective way.
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/24/08 17:46

Quote:


If you create 200 grass sprites around the player at random locations, and you move the player forward, the grass sprites outside the visibility range should be relocated to some new location around the player that just appeared in visibility range.
That's the most effective way.




you are right, thats was what i was trying to achieve creating and removing grass entities...
but still
about the c_move stuff... the rocks has their sprite collision detection OFF already AND they rarely make it to collide with a sprite.
And still 200 makes the FPS to drop but its playable, im only affraid when the game get more stuff in its vision or level, whats goind to happen...

I will try to come up with a code that makes what you said before,
have any more thoughts or ideas about the FPS drop and anything else?

thanks
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/24/08 18:21

I don't know actually what causes the fps drop, for 10 small rocks moving with c-move, IMO there shouldn't be a drop of 20fps. You could post a new post with this specific question and give some more details.
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/24/08 18:52

well yeah..
it does if theres like 500 grass and i create this 10 small rocks i get this drop for some moments and then it gets back to 60. =/


I am in need of one more help from you, to make it like
200 entities and 1 function running, i should not put any while in their initial function.
So far I have made this:
Code:

var grass_array_x[1000];
var grass_array_y[1000];
var grass_array_index;

function define_grass_pos()
{

while(grass_array_index < 1000)
{
grass_array_x[grass_array_index] = random(10000);
grass_array_y[grass_array_index] = random(10000);

grass_array_index += 1;

}

}


So I have defined 1000 positions (x,y) inside my level in a random area of 10,000.
Now I need to create 200 grasses and move them into 200 player nearest positions of this 1000 total positions.
So how am I going to move the grasses between this positions if i cant have a while loop inside their functions? because otherwise it will make 200 running functions.

I was trying to find an Entity Array so i could directly move them. but it doesnt seems to have Entity Arrays only Var and string arrays.
So am i really going to define 200 ENTITIES MANUALLY?
eg.
entity* grass1;
entity* grass2;
...

?

ps. the reason I pre-defined 1000 postitions randomly only once is because once you loaded the game, and the positions are defined, even if you walk out from an area and the grass is removed from there to fill another position, i wanted to when you go back to that place, the grass is put back in the same place, instead of you walk in the same area 10 times and see the grass in 10 different places everytime you walk in that area. ( it may be stupid, maybe the player wont notice it but i wanted it this way unless i really need to change it)
Posted By: Ready

Re: Best way to do things ( random grass generator - 01/24/08 19:29

Had a problem somewhat related to that earlier today, check out this thread
http://www.coniserver.net/ubbthreads/showflat.php/Cat/0/Number/813157/an/0/page/0#Post813157
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/24/08 19:43

Nice thanks Ready
that article in AUM47 might solve my problem, Ill try it when i get home today, and tomorrow IŽll post the results at work.

In case anyone else have any idea, tip, opinion about the original post please post it up, i am always gathering Render, FPS , memory information as much as I can to improve my game since its aimed to average strength computers.(something like P3 1000mhz 128 or 256mb of ram, GeForce2 64mb, is it too weak?)
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/24/08 19:45

Quote:


So am i really going to define 200 ENTITIES MANUALLY?




Noo noo, of course not manually . You can store entity pointers in variable arrays. But I can not provide you the full code now, it would take time to figure out a good method, but this is how you should basicly do it (code in green is code you should add, orange is psuedo thus you yet need to define it):

Code:

var grass_array_x[1000];
var grass_array_y[1000];
var grass_pointer[200];
var grass_array_index;


function create_grass()
{
while(grass_array_index < 1000) {
grass_array_x[grass_array_index] = random(10000);
grass_array_y[grass_array_index] = random(10000);

grass_array_index += 1;
}
}


function run_grass() {
var i;
while(i < 200) {
grass_pointer[i] = ent_create ("grass.tga", nullvector, NULL);
i += 1;
}

while(1) {
while(i < grass_array_index) {
you = grass_array_index[i];

if (vec_dist (you.x, player.x) > 1000) {
you.x = closest_stored_xpoint_to_player; //that is not already taken by a grass sprite
you.y = closest_stored_ypoint_to_player;

}
}
wait(1);
}
}




Now the orange part is most certainly not the easiest part, and because you have predefined positions, it's alot more calculating and loops than just placing at random places. I do advise you to not use fixed positions for this reason, but there might be a different method I have not thought of to handle this (maybe involve an entity it's skill variables).
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/24/08 19:47

As for the pointers, their pointers are also stored in variables without using ptr_for_handle. There's use for this function, but it is slow and I think you wont need it here.
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/24/08 20:09

Alright thanks Joozey
IŽll try to do the code at home, and tomorrow IŽll post it.

thanks and good night (its 18:00 here in brazil)

Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/25/08 19:42

OK
i made it work ( not really) Im still implementing some features to make it more reallistic but the OVERALL IDEA works but with ONE BLOODY PROBLEM.
here is the code
Code:

function put_grass()
{

if(grass_index < 200)
{

teste2 =grass_index; //just for debugging purposes
you = grass_pointer[grass_index];

you.x = nearest_pos_x;
you.y = nearest_pos_y;

grass_index += 1;

}

return;

}


function define_nearest_pos()
{
grass_index = 0; //reset
grass_array_index = 0; //reset


while(grass_array_index < 1000)
{
if(vec_dist(global_player_pos.x,grass_array_x[grass_array_index] ) < 8000)&&(vec_dist (global_player_pos.y, grass_array_y[grass_array_index]) < 8000)
{

nearest_pos_x = grass_array_x[grass_array_index];
nearest_pos_y = grass_array_y[grass_array_index];
put_grass();
}

grass_array_index += 1;
}

}

function initial_grass_func()
{
my.passable = on;
my.scale_x = 0.5;
my.scale_y = 0.5;
my.scale_z = 0.5;
my.z = 64;

}


function define_grass_and_positions()
{
i_max_grass = 0;//reset
i_grass_array_index = 0; //reset


while(i_grass_array_index < 1000)
{
grass_array_x[i_grass_array_index] = random(10000);
grass_array_y[i_grass_array_index] = random(10000);
i_grass_array_index += 1;
}

while(i_max_grass < 200)
{
grass_pointer[i_max_grass] = ent_create (grass1, nullvector, initial_grass_func);
i_max_grass += 1;
}

if(i_grass_array_index >= 1000)&&(i_max_grass >= 200){define_nearest_pos();}//IF its has finished defining and creating ...

}



At first the "define_grass_and_positions()" function is called first, then down the road the function "define_nearest_pos()" is called by another function(not listed here) eveytime the variable "grass_array_index" gets to a 1000 so its always updating.


The problem is
Everytime I start the Engine sometimes its just doesnt work, the variable "grass_index" stays at 0 (zero) and sometimes it works fine.


anyone has any idea why?
Posted By: Nidhogg

Re: Best way to do things ( random grass generator - 01/25/08 23:39

Maybe you need to add the wait(1) statement after the while(something) statements.

I believe your supposed to add them otherwise it does casue problems.

best of luck..
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/28/08 11:55

yeah i thought about that. IŽve trying to test Wait(1) statements everywhere, and the same happens, sometimes work sometimes not, something that i could figure out is that when it doesnt work means that the Put_Grass() has not even been CALLED!
anymore ideas?


Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/28/08 17:55

HEELP
lol
Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/28/08 19:57



if(i_grass_array_index >= 1000)&&(i_max_grass >= 200){define_nearest_pos();}//IF its has finished defining and creating ...


Well heres a mistake, should give a compile error. you close the if but youre not done with your statement yet.

Code:

if(i_grass_array_index >= 1000&&i_max_grass >= 200){define_nearest_pos();}//IF its has finished defining and creating ...



You do not need the IF at all anyway, for you will only reach the call to function define_nearest_pos() if the while loops are completed.
I don't think it will work if I look to the rest of your code, but ;et's first see what happens now...


EDIT
Where do you fill the variable grass_index at all? Only in the function put_grass and surely nowhere else?
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/29/08 14:01

hey joozey
doing ifs like this ()&&() never gave me any problem.
and now that i found the problem yesterday i know the problem isnt there.
a friend of mine helped me figure it out.

As far as i could understand this is wrong:

Code:
if(vec_dist(global_player_pos.x,grass_array_x[grass_array_index] ) < 8000)&&(vec_dist (global_player_pos.y, grass_array_y[grass_array_index]) < 8000)



because vec_dist compares vectors with 3 values, so, global_player_pos.x is actually global_player_pos.x.y.z together comparing with grass_array_x[x x x] which is not what i want to compare, so i had to do something more arcaic.

Code:

temp.x = grass_array_x[grass_array_index];
temp.y = grass_array_y[grass_array_index];
temp.z = global_player_pos.z;

if((temp.x - global_player_pos.x ) < 8000)&&((temp.y -global_player_pos.y ) < 8000)
{
...




and for some reason it works fine!!
So IŽll begin been more careful when using Vec_Dist func.

thanks you all for the attention.

Posted By: Joozey

Re: Best way to do things ( random grass generator - 01/29/08 14:47

Yes, ofcourse ^^ I overlooked that.
So now your grass are popping up randomly at positions and move to others when they are out of players range?
Posted By: DestroyTheRunner

Re: Best way to do things ( random grass generator - 01/29/08 16:41

thats what im going to do today...
cause right now
the grass changes position in sequence, 1 to 200 then after that, grass number 1 is the next, and thats the loop.
so im going to do it with the same loop logic but if the position is inside the players vision then it doesnt move and pass the index to the next one.
shouldnt be hard.
do you want me to post the code after finished? or do you already has one?
© 2024 lite-C Forums