Page 1 of 1

Water Physics (attempt) and faster collision free

PostPosted: Fri Sep 16, 2011 1:49 am
by jimmynewguy
Hello, was messing with water physics just to see what would happen and a sorta nice looking thing came out of it as well as a useful thing. CollisionFree usually takes up a lot of memory and made things lag if used often. I replaced it using this code.
Code: Select all
int check(int posX, int posY)
{
     int temp;
     Actor * coll;
     coll = getactor(posX, posY);
    temp = (strcmp(coll->name, "") == 0);
    return temp;
}

Does the same thing, but instead of "Event Actor" just use x,y for posX and posY. Changed collsionfree to this and it ran at least twice as fast.

Anyway, this is sorta cool looking water here. It flows from an unknown source and fills the holes. Inevitably, after the first is filled and it moves on to the second, it starts to lag. :( Still neat, though! Check it out please! I'm still trying to optimize it fyi.

Re: Water Physics (attempt) and faster collision free

PostPosted: Fri Sep 16, 2011 2:57 am
by skydereign
Just as an optimization to your check function, you can do this instead.
Code: Select all
int
check(int posX, int posY)
{
    return (getactor(posX, posY)->cloneindex! =- 1);
}

This way you don't have to do a strcmp each time, and don't declare any variables. In case you didn't know, getactor returns an invalid actor (with a name of "" and a cloneindex of -1). Also I notice you haven't been uploading Linux versions as much anymore...

Re: Water Physics (attempt) and faster collision free

PostPosted: Fri Sep 16, 2011 4:55 pm
by jimmynewguy
skydereign wrote:Just as an optimization to your check function, you can do this instead.

*Slaps forehead*
Thanks sky, I'm not so sure how well this works anymore though. It doesn't like to flow both directions anymore... Ah oh well!

Re: Water Physics (attempt) and faster collision free

PostPosted: Mon Sep 19, 2011 12:35 am
by jimmynewguy
Optimized it a little more, here's bigger sprites acting as lava. 100 actors, little bit of lag while being created is all. With linux this time! (Sorry sky for the first time)

Re: Water Physics (attempt) and faster collision free

PostPosted: Mon Sep 19, 2011 1:07 am
by Jagmaster
Pretty nice effect! change the color to green and you have slime!
I think my cpu would go kaput if I implemented it in a game. But maybe mix with tiled animation and you would have a less realistic but cool effect!

Re: Water Physics (attempt) and faster collision free

PostPosted: Mon Sep 19, 2011 1:24 am
by jimmynewguy
Thanks! I like it a little, but it's really not practical. It's just fun to mess with and see what comes of it, tried messing with objects to float in the water, but it ended up bobbing a bunch.

All the lava one does is use a for loop and checks the distance between one particle and all the others (excluding itself) and moves away if they're to close. (and sticks together a tiny bit)

EDIT: I should wait before I post, I tweaked it a little and made it better and faster. Oh well, I'm not sure if I should share the source code, it all really messy and sloppy.. Plus it's still not too practical! :)

WARING! What you are about to see is bad. Check only checks for the ground in this one so don't use the original check function.
Code: Select all
int i;

if(!off)
{
for(i=0;i<nwater;i++)
{
if(i != cloneindex)
{
Actor * col = getclone2("water", i);
if(abs(x-col->x)+abs(y-col->y) < 12)
{
angle = direction(x,y,col->x,col->y)+180;
directional_velocity = 2;
break;
}
}
}
}

if(check(x,y))
{
yvelocity = 0;
angle += 180;
directional_velocity *= 0.8;
off = 1;
}

yvelocity = max(yvelocity + 1, 6);

Re: Water Physics (attempt) and faster collision free

PostPosted: Tue Sep 20, 2011 2:26 pm
by SuperSonic
Wow, this is amazing and I don't get any lag! +1 for that dood :wink:

Re: Water Physics (attempt) and faster collision free

PostPosted: Thu Sep 22, 2011 12:49 pm
by Game A Gogo
I'm curious, how did you make your water physic in your first post? I've tried doing one but I kept failing horribly!

Re: Water Physics (attempt) and faster collision free

PostPosted: Thu Sep 22, 2011 4:04 pm
by jimmynewguy
Lots of if's! :D you can see it in here, made the water longer since when they moved towards each other the went through sometimes...

Re: Water Physics (attempt) and faster collision free

PostPosted: Sun Sep 25, 2011 10:26 am
by JamesLeonardo32
Nice physics!

I made some experiments with it. (Updated with better water effect)