Page 1 of 1

How can I make my player pick up and carry an object?

PostPosted: Wed Jul 07, 2010 11:19 pm
by krenisis
I was wondering how can I make my player pick up an object and carry it. I really need to know how to do this.

Re: How can I make my player pick up and carry an object?

PostPosted: Thu Jul 08, 2010 12:28 am
by DST
Here's a demo describing how to pick up and carry an object.

Note that the sin/cos lines are there to make sure the player holds the object in front of him. They may be different depending on game type (for instance, in a side scroller you wouldn't use the y=sin part, only x/cos would be used). In a 3/4 overhead, you might also use zdepth to make the object behind player whenever player is walking upwards.

Re: How can I make my player pick up and carry an object?

PostPosted: Thu Jul 08, 2010 5:11 am
by krenisis
This demo is great because you can carry the object in all directions and it looks even with no overlapping. However there some questions because Iam use to a different coding style.
1) What command actually make the object snap to the player when you press space key? I see the variable carry=1; on space key press down event. I looked in the draw actor but on the (carry==1) it reads like this
if(carry==1){
this=getclone2("object", oindex); //find object we're holding
this->x=x+(cos(degtorad(angle))*20); //object is 20 pix in front of player
this->y=y+(sin(degtorad(angle))*-20); //y is inverted

Now I never used these commands before:
cos
sin
this
degtorad
What is thier definition?

2) Also I see you vastly differ from me because insted of loading 4 animations up,down,left,or right you have all the animations in one file. How did you get the game editor to read which exact direction you player is going from that one file?

Thanks alot I need to know because when I program this I have to use onscreen buttons.

Re: How can I make my player pick up and carry an object?

PostPosted: Thu Jul 08, 2010 5:31 am
by DST
animpos=angle/90;

or instead of 90, use 360/nframes, for example, if you had 8 frames, it would be angle/45; but you can just use 360/nframes, as nframes is a builtin ge variable.

animpos=angle/(360/nframes);

As far as sin and cos, these are basic trigonometry functions which once you understand them, you're gonna love them.

In trig, we use radians for angles instead of degrees. Radians are an expression of PI (3.1459 etc). So we use
degtorad(angle) to convert to radians.

Then....cos = x and sin =y. If you were to make an angled line, you'd move over x pixels and up y pixels; a 45 degree angle is 1:1, and a 30 degree angle is 2:1. That's 2 x pixels for every y pixel, right? 90 is 0:1. x+=0, y+=1;

The last thing to know is that sin and cos return normalized values (a float less than 1) so you have to multiply them to get the distance you want, in this case, 20.

sincos.png
This isn't technically correct; as sin and cos are floats less than 1; but the ratio is what i'm trying to show



So the sin/cos functions i wrote in the demo put the object in front of the user no matter which way he's facing, @ 20 pixels out from the player. When the player moves up and has angle of 90, the cos(x) =0, so it's centered on x, and when he's facing left or right, the sin(y)=0 so it's at the same y.


Actor * holds a temporary copy of an actor; ANY ACTOR IN THE GAME. You have to give it a name to refer to it, and then use either getclone or getclone2 to define it. (In global code, load the getclone2 script, hit file>save> and save it as a .c file so you can load it on other games). Getclone2 allows us to specify a cloneindex for the object, which in this demo, we got when we collided with the object.

so Actor*this=getclone2("object", oindex); Makes a temporary copy of the object we touched; you can then access this objects variables using the -> function; if oindex==10 then it will get object.10.

this->x; this->y; this->transp;

And it's the same for assigning or reading values, like:

this->x=100;
x=this->x;

For added functionality, you can use it alongside createactor, using createactor instead of getclone when creating a new actor:

Actor*this;
this=CreateActor("shot", "redmissile", etc. etc);
this->xvelocity=10;

at the end of the current script, Actor * this will disappear from the script, but the createactor version is now in the game as a normal actor.

Re: How can I make my player pick up and carry an object?

PostPosted: Thu Jul 08, 2010 12:22 pm
by jimmynewguy
Ah. When you go through the forum and see a giant picture of a circle with a sin/cos ratio, and DST is one of the people who made the post, there's something good there. :) I hope everyone reads this and stops being afraid to use functions they aren't familiar with.

Re: How can I make my player pick up and carry an object?

PostPosted: Thu Jul 08, 2010 12:36 pm
by DilloDude
DST wrote:As far as sin and cos, these are basic trigonometry functions which once you understand them, you're gonna love them.

sin and cos are indeed awesome. I use them in pre-made functions which automatically do the degtorad and distance multiplacation. There aren't many game projects I make which don't end up using them...

Re: How can I make my player pick up and carry an object?

PostPosted: Thu Jul 08, 2010 12:40 pm
by Hblade
wow thats... awesome DST :D Thanks for showing us ^.^

Re: How can I make my player pick up and carry an object?

PostPosted: Fri Jul 09, 2010 6:19 am
by Bee-Ant
Kren, if you feel DST's code too confusing, here's the simpler method. First, make an actor variable called "pick".

Player->Collision->Object:
Code: Select all
collide.pick=1;

Object->DrawActor:
Code: Select all
if(pick==1)
{
x=Player.x;
y=Player.y;
}