Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Centering sprite on ladder (using physics)?

Author
Message
jhanson
6
Years of Service
User Offline
Joined: 19th Jun 2017
Location: USA
Posted: 10th Jul 2017 17:39
I am using the built in physics engine to make a Platformer. Using impulses to move the player's sprite around and all that. It all works fine - the player can walk, run, jump, climb up/down ladders, etc.

The one issue I can't seem to wrap my head around is centering the sprite on the ladder. I don't care if the sprite is exactly centered, but I at least want it to look like it is so that the climbing animation looks right. I tried using SetSpriteX with a calculated offset, but when I use it it kills whatever velocity and forces are already at work on the sprite. It centers him properly but the player ends up having to push Up a second time.

Is there some way to either fake the sprite being centered on the ladder, like shifting the image around in relation to the actual location of the sprite, or is there a way to calculate the exact amount of impulse to apply to move the sprite the desired distance in so little time that it isn't obvious to the player?
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 16th Jul 2017 04:19 Edited at: 16th Jul 2017 04:22
Could you apply two impulses from left and right of ladder towards the center of the ladder using the distance from player to the sides of the ladder respectively as an impulse strength IF player is x distance from center of ladder while upkey state = 1?

Now this snippet is a hipfire, but I hope it helps

jhanson
6
Years of Service
User Offline
Joined: 19th Jun 2017
Location: USA
Posted: 16th Jul 2017 13:03
Ooh. Something like that might work out well. I'll give it a shot. Thanks!
bszaronos
6
Years of Service
User Offline
Joined: 7th Jul 2017
Location:
Posted: 17th Jul 2017 03:49
Jhanson, How are you using the physics engine to move your player around in your platform ?
I am using the joystick to move the player around in my game, I have invisible sprites that I use to detect the edges to stop him from falling off. My trick for solving the problem of jumping is to have two invisible blocks 1 to stop him from walking into the space, the 2nd to stop him from landing in the space.
If he does touch the invisible sprites I just put him back to the old x position he started from. I don't really care about the y position because when I do hit a ladder I am just checking for the up joystick level and the button being pressed.

I am thinking that the 2d physics might be a solution for having my player fall off an edge. My thinking is that I can have something like a blower that pushes the player down. if he goes off the edge.

jhanson
6
Years of Service
User Offline
Joined: 19th Jun 2017
Location: USA
Posted: 17th Jul 2017 13:08
@bszaronos - I am using SetSpritePhysicsImpulse to push the character around. I only set his position directly when starting the level, and let the various impulses and forces (like gravity) handle all the motion. I don't have any invisible walls in mine (aside from the very ends of the levels) because I want him to be able to fall off the platforms if he gets too close to the edge.

I'm not sure how efficient this is, but below is how I check if he's on a ladder. I have all of my ladders set to Sprite Group 2. I have a type called game that contains all of the variables that get reused in the game. Inside that is a type called player that contains everything player-related, including the sprite ID of the player. When you press up, one of the things it does is run this function.

If the center of the player sprite is within 4 pixels of the center of the ladder sprite, I consider it close enough and return a 1. Otherwise return a 0. The resolution on this game is very low (320x240) so 4 pixels is a fair amount of leeway.

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 17th Jul 2017 13:40
Hi bszaronos,

When I first started using agk I thought that turning on sprite physics would be the easiest way to control characters (because you see results on the run screen instantly). When I ran into problems I couldn't solve I did some reading, asked on the forum, and on steam. After talking to some experienced programmers who were nice enough to help me, I found their advice true. That advice was: For most situations its easier to manipulate your character without physics, or to create your own simple physics elements.

On that note, jhanson, I have thought of another way to center your character without using the impulse.



If you have multiple floors with ladders you will need to add a check so your player doesnt levitate when below a floor with a ladder
bszaronos
6
Years of Service
User Offline
Joined: 7th Jul 2017
Location:
Posted: 17th Jul 2017 14:47
I'm new to AppGameKit only bought it on steam on July 6th, I'm 64 hours in use so far, so I have no clue about 2d physics yet. Was looking for a simple example, but couldn't find one.
I thought about using invisible sprites to create a box that was slightly away from the platform. that way the player would have to walk off the platform to touch it. Then I would just code him down to the ground level. I would add a second sprite that would be placed further away from the platform and that would be used to detect if he jumped from the edge.

This is the code I use to move my player up the ladder, not the best but it does work.

Login to post a reply

Server time is: 2024-03-29 07:27:10
Your offset time is: 2024-03-29 07:27:10