Collision & "Hurt"

Game Editor comments and discussion.

Collision & "Hurt"

Postby linkshards » Tue Aug 31, 2010 12:41 am

So I picked up where I left off from my vacation.
I was working on collision, and I was getting help from DST. Thanks dood, very much appreciated for the code, but I'm a little confused with how the math behind it works.

Here's the last responding quote:
DST wrote:The simplest way to make a controlled collision movement is using the moveto() function, like this:


Code: Select all
int i=x-collide.x;    //store the offset of this actor vs. collide actor position
int j=y-collide.y;    //using x-collide.x gives us negative offset (collide.x-x would create positive offset)
MoveTo("Event Actor", x+i, y+j, 10.000000, "Game Center", "");  //move to that negative offset


If he is to move farther away than that, just multiply the i/j values by whatever you wish before adding them to the moveto();

As for the animation and invincibility, you can 'changeanimation' in the collision script, and then add an 'animationfinish>hurt animation>script editor' where you restore to normal animation.

If he is to be invincible during the duration of the 'hurt', simply wrap this script in a variable check, like
if(cancollide==1){
//movetoscript here
cancollide=0;
}

Then return it to 1 on the animation finish. You can use a timer, or a 'state' switch in draw actor to more accurately set the length of time he is hurt, if you prefer, instead of using animationfinish.

The cancollide disables collisions while he is in hurt mode, and by NOT using that same 'if' in wall collisions, he'll always collide with walls no matter what.

I often like to use a state switch for actors, like this:

Actor>draw actor>

switch(state){
Code: Select all
case 0: //normal operation
break;
case 1://hurt mode
variable++;
if(variable==10){   //stay hurt for 10 frames
variable=0;
state=0;               //then return to normal mode
ChangeAnimation(etc etc);
}
break;
}


Then you can simply switch the state in the collision, and also use that same state instead of cancollide, by only allowing the collisions script to happen if the state!=1. You can also disable other events in the state, so you won't have to do it in the collision. Keep in mind that it's a good idea to set that 'variable' used as a timer to 0 upon collision, to prevent it being a wrong value if something else happened to interrupt it.


Most important part of all: If you have multiple enemies that are not clones, you would need this collision script for collision with each one. so make it a function!

Go to global script, and wrap it in a function name:
Code: Select all
void getHurt(int cx, int cy){
//collision script here, using cx instead of collide.x, and cy instead of collide.y
}


Then just call the getHurt(collide.x, collide.y); in the collisions with each type of enemy, instead of typing it out each time. Making changes to the script will be much easier that way, as everything you do to the collide script will automatically happen to all the collisions.

Note that you have to pass the collide.x and y variables to the script but not the event actor's x and y, because a script automatically becomes part of the actor it's called in. So you'd also want to add the collide.damage, as another int in the script, and any other variable that the enemy passes on to the player.

Using these things as functions makes a game 100x easier to create.


The math behind the collision is a bit confusing to me. But it works-somewhat.
Let's say the main hero gets hit by an enemy and takes damage, he still moving, from what it looks like, leftover momentum, so he's moving a little.
If might be doing this wrong, but the code I put in as a Collision->Draw Actor is this:

Code: Select all
i = hero.x - monster1.x;
j = hero.y - monster1.y;
MoveTo("Event Actor", hero.x+i, hero.y+j, 2, "Game Center", "");


As for the hurt animation, his "hurt" direction has to be congruent with the direction he was facing before we was hurt. ↑↓→←
How exactly would I write the code to receive the current animation (the direction he's facing)?
And then return to normal?
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score

Re: Collision & "Hurt"

Postby DST » Tue Aug 31, 2010 12:44 pm

viewtopic.php?f=4&t=8375&start=15#p58243

Remember how you find dir based off angle? angle is constant, it works as long as your actor is moving, no matter what method you used to move him.

dir=round(angle/90); //find animation direction based on angle, where dir is an int variable you made.
if(angle>315){dir=0;}

If you are using single animpos, just say animpos=dir;

else use dir to sprintf a new animation if you are using full animations. You can create, for any actor, a string variable to hold the actor's name, then use that in the animation name.

for animation list
link0, link1, link2, link3

link>createActor>
strcpy(myname, "link");

and to change animation based on dir
char anim[32];
sprintf(anim, "%s%i", myname, dir);
ChangeAnimation(Event Actor, anim, FORWARD);

sprintf is for combining strings and variables. In this case, a string named anim.

sprintf(string to write to, "%s for string %i for integer", var1, var2);
where the var is in order from what you called in the second space "%var1type%var2type" as "%s%i".
You can string as many of these together as you want. When assigning them at the end, use "" for literal strings, and no "" for variables.

char sometext[64];
char yourname[12]="linkshards";
sprintf(sometext, "%s%s%s%s%s%s", "sprintf ", "is ", "really ", "useful, ", yourname, "!");

returns

sprintf is really useful, linkshards!

It always overwrites anything contained in the string previously.

Sprintf is essential to controlling animations in GE.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Collision & "Hurt"

Postby linkshards » Wed Sep 01, 2010 3:09 am

Whoa, kinda lost me there. X3

How would I use sprintf to recreate or call on an animation after it's been played?
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score

Re: Collision & "Hurt"

Postby DST » Wed Sep 01, 2010 3:27 am

Well, you shouldn't have to. You can call the script anytime to constantly assign the proper animation.

In my examples, i used the "FORWARD" option on the animation call. But you can also use "NO_CHANGE". Not only will this preserve the direction, but also the frame. So you can easily transition between animations using this option.

So the whole dir/sprintf script can run all the time in Draw_Actor, and you can simply add other options to the sprintf to determine what's going on. You don't have to recall because, at any given moment, the script knows exactly what animation link should be on, and using no_change, it will preserve the animpos too!

For instance, when link gets hit, set variable 'hurt' to 1. Then, you can add that into the sprintf/dir thing.

char anim[32]; //for final animation name
char anim2[32]; //for extra animation option
dir=angle/90;
sprintf(anim, "%s%i", myname, dir); //link+dir
if(hurt==1){ //if link is hurt
strcpy(anim2, "hurt");} //make anim2 say "hurt"
else{strcpy(anim2, "norm");} //or if not hurt, make anim2 say "norm";
strcat(anim, anim2); //add anim2 to the end of anim (so it says "link2hurt' or 'link2norm')
ChangeAnimation("Event Actor", anim, NO_CHANGE); //Change to that animation.

Then name your animations link0norm, link1norm, etc. etc.

If link needs to change color, say he gets new armor (ring), then you'll have to add that as a new set of animations anyway (that's just how ge is) and so the most important concept here is that you make a sensible animation naming system. Because, if you want a blue link, then you can just add the word 'blue' to the end of the anim name and changeanimation to link0normblue.

What i mean is, i didn't have to say 'link1norm". i could have left off the norm part. Therefore, you can always add more 'if hurt" kind of stuff, and strcat more names onto the end of the string, to match the names in your animation list. Link0, link1, link2, link3. Link0blue, link1blue, etc. link3red, link0godzillamodskin, it doesn't matter you can add anything to the end of the animname as long as your direction system is in place. 4 directions based on dir.

And linkhurt will probably only be one animation...it doesnt' matter what color link is normally, hurt is pretty much 'rainbowlink' no matter what armor or ring he has. You can always put 'hurt' at the very end of the animnames if you wish.

strcat copies one string onto the end of another, so strcat(myname, yourname); results in
"dstlinkshards".

Sorry if i'm giving you too complex code here, but, i can't help it. Programming is fun. Understanding strcpy, sprintf, and strcat (and strncat) are infinitely fun to me, and it's part of what keeps me coming back for more. It's not clumsy at all...sprintf is actually really a smart concept.

C is still the most/2nd most used programming language on the planet.
(it's always fighting with Java for total usage).
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Collision & "Hurt"

Postby linkshards » Thu Sep 09, 2010 2:42 am

So would it be right that I use Collision→Draw Actor→ then replace what you told me with the correct corresponding animations to call?

As for the angles... lets say 'link' gets hit and needs to face the direction of the monster that hit him. How I would set that up so that 'link' detects the side he was hit by the monster and then do the hurt animation then return to normal?
The return to normal is timer based right?
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score

Re: Collision & "Hurt"

Postby linkshards » Sun Sep 12, 2010 8:34 pm

DST? :x You there mate?
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron