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.

AppGameKit Classic Chat / Correctly figuring if a physics sprite is about to hit something

Author
Message
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 29th Aug 2017 13:36
I am moving a tilemap with SetViewOffset(), and trying to keep the player sprite in the middle with SetSpritePositionByOffset(). I set physics on for the player and for the sprites that he should not pass through.

When the player hits an obstacle, the physics correctly stops the movement even if I do SetSpritePositionByOffset(). The problem is, I then move the screen by the same amount with SetViewOffset(). This causes the player to drop off the center and the screen keeps scrolling while the player is staying put. I need to prevent the screen from moving while player stays put.

I tried to set the obstacles to specific category and use PhysicsRayCastCategory() to get hits on the left, right, top and bottom of the player on those obstacles. This works OK if the player moves directly left, right, up or down. But when they move diagonally, somehow it does not catch the hits with the ray casts. Is there some change in direction of the raycast if you set the sprite angle? Or should I play tricks with the player size? I try to raycase the top line and bottom line of left and right lines of the player depending on left/right or top/down dirs. As I said, works on straight angles but not diagonally.

Any examples on how to do this so it works, or some ideas on how to do this better otherwise?

tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 31st Aug 2017 09:47
I fixed the above issue by just centering the screen all the time on the player sprite, while letting the physics system bumb the sprite. Makes it look like the player is humping the wall sometimes but good enough.

Anyway. Related question.

What is a good way to check for collisions when deploying a sprite at some coordinates? I am interested in some sprite groups/categories only.

I am using the physics system, mainly for collision detection, but it seems to me that the collision maps it maintains only get updated on sync(). So I guess I would need to first deploy the sprite, wait for sync() and then remove it if a hit was registered. That would look silly.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 31st Aug 2017 13:48
can you use GetSpriteHitGroup( group, x, y ) to check the location before creating the sprite?
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 31st Aug 2017 19:38
Thanks for the idea, did not think of GetSpriteHitGroup (). That could work. Of course this only checks a single pixel position so not the whole collision shape. But maybe testing the center would be good enough..

I actually ended up dropping the sprite on screen and checking in the following frame, after physics are updated, if it hit something. Then set its sprite group and make it visible if it did not, delete it if it did.. Seems to work fine for my purposes for now.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 31st Aug 2017 20:06 Edited at: 31st Aug 2017 20:07
Quote: "Of course this only checks a single pixel position so not the whole collision shape"


you could get the sprite position and size and check if the location is inside the sprite collision area

tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 1st Sep 2017 08:07
Thanks @PartTimeCoder. That is definitely useful.

I would still need to find all tiles and their sprites, as well as other sprites on top of them to do the tests on. Was hoping to avoid all that. The physics system handles it for me for now, which is why I was preferring to use it.

Of course, I have no idea how efficient the physics system collision detection is vs writing this myself. Would not be too hard and then it would be always usable, of course. My shapes here are mostly shapes and circles so perhaps..


Login to post a reply

Server time is: 2024-11-24 09:16:15
Your offset time is: 2024-11-24 09:16:15