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

Non-platform specific questions.

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

Postby krenisis » Wed Jul 07, 2010 11:19 pm

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.
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

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

Postby DST » Thu Jul 08, 2010 12:28 am

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.
Attachments
carrydemo.zip
(24.48 KiB) Downloaded 76 times
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: How can I make my player pick up and carry an object?

Postby krenisis » Thu Jul 08, 2010 5:11 am

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.
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

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

Postby DST » Thu Jul 08, 2010 5:31 am

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.
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: How can I make my player pick up and carry an object?

Postby jimmynewguy » Thu Jul 08, 2010 12:22 pm

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.
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

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

Postby DilloDude » Thu Jul 08, 2010 12:36 pm

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...
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

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

Postby Hblade » Thu Jul 08, 2010 12:40 pm

wow thats... awesome DST :D Thanks for showing us ^.^
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

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

Postby Bee-Ant » Fri Jul 09, 2010 6:19 am

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;
}
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron