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 Studio Chat / [SOLVED] How to check if a sprite is left the screen and no longer visible?

Author
Message
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 8th May 2020 16:33 Edited at: 8th May 2020 16:44
Hi.

I would like to make a warp function that moves the sprite from one side of the screen to the other side similar to the classic Asteroids game. For this I need to know when a sprite left the screen.
The obvious solution is that to compare the X and Y position of the sprite with the width and height of the window.
However, if the sprite is not a perfect square but a rectangle and it is rotated, the calculation become more complicated to know for certain if any part of the sprite is still visible on the screen and I can't figure out the Math for this one

If the Sprites are rectangular shape and rotated they get warped to the other side too early or too late, I need to find a more accurate solution but I have no idea how to do the Math.
I would appreciate any help.

Edit:// I did try the GetSpriteInScreen() command but I get the same problem with this one. As described in the docs, if the sprite is very close to the edge, it may and it is return 1 even if some part of the sprite is still visible.

I would like to do the math from scratch.

Thanks.

The author of this post has marked a post as an answer.

Go to answer

Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 8th May 2020 16:35
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 8th May 2020 16:40 Edited at: 8th May 2020 16:41
Thanks but I get the same problem with this one. As described in the docs, if the sprite is very close to the edge, it may and it is return 1 even if some part of the sprite is still visible.

I would like to do the math from scratch.

But thanks.
Kevin Cross
20
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 8th May 2020 17:19
How about having one large invisible sprite that covers the screen and then check for sprite collision being 0 with the large invisible sprite
OryUI - A WIP AGK2 UI Framework
Kevin Cross
20
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 8th May 2020 17:28
Another option would to put a sprite off the screen, far enough so that any sprite colliding with it would definitely be out of view. Once a collision is detected move the moving sprite to the other side. I think my first suggestion might be better though as it should return 0 once the moving sprite is no longer touching it and should therefore be off the screen completely
OryUI - A WIP AGK2 UI Framework
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 8th May 2020 18:51
Thanks for the tips Kevin. The problem with collision checking is that you can not do custom collision shapes in AppGameKit and so there could be empty areas in an image that cause similar problems, like detecting collision too early or too late.

I tried many things and nothing really worked so at this point I really would like to work out the math.
Basically what I would like to figure out is a solution to check if all 4 corners of an image is outside the screen but taking in to account rotation as well, the rotation part that I can not figure out. I can find the corners but as soon as I rotate it, I am in dark, I have no clue how to go about the math and take the rotation in to account to locate the corners.

But thanks again, if I could do custom collision shapes checking collision could be one way to go about this indeed.

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 8th May 2020 19:13 Edited at: 8th May 2020 19:24
Quote: "you can not do custom collision shape"

you lost me there where we can set shapes (box/rectangles, circles, polygons, chains) ?

Quote: "I would like to figure out is a solution to check if all 4 corners "

i wouldn't check if the corners/vertices of the shape are on screen but the lines between them where, say, v1 and v2 are off screen near the top-left corner but the line between them could definitely cross the screen.

if you want to do that manually, this might be a starting point: https://stackoverflow.com/questions/16203760/how-to-check-if-line-segment-intersects-a-rectangle.

but, you should also look at box vs box math as i expect it's simpler than computing rotated vectors (we have GetSpriteShapeVertexX &Y but they are only so useful/are not live world coords).

meanwhile, i don't know if GetSpriteInScreen uses something different, but we have GetSpriteInBox, as well. it doesn't mention a "if the sprite is very close to the edge"-type disclaimer while it does offer hope in "This command takes into account the rotation and scale of the sprite when checking intersection".

my (uneducated) guess is that GetSpriteInScreen uses "if its very close to the screen" logic for rendering while GetSpriteInBox might use aforementioned box vs box math or line vs box?

but, i would start by testing GetSpriteInBox before i delved into the others.

i'll add that, if you do want to find the rotated vertices, you could start with the non-rotated position, calculate the angle and distance of each from the center point (0,0) and hold that to calculate their rotated positions.

someone probably has a more elegant solution, there
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 8th May 2020 19:48 Edited at: 8th May 2020 19:49
Thanks I did not noticed we can now add polygons to a spriteshape now. That's nice.
My understanding is that GetSpriteInBox is create a box collision area anywhere you want on the screen and you can check collision with this area. I guess I could use it to create box same size as the screen and check if the sprite is no longer colliding with this box maybe?
I guess it could work but I'm not too sure how badly it is going to hit performance, constantly checking collision vs checking position on the screen.

Quote: "hold that to calculate their rotated positions."

This is exactly I have no clue how to do it how and where the rotation fit in the formula this staff is way beyond me XD
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 8th May 2020 20:03 Edited at: 8th May 2020 20:38
Quote: "I guess I could use it to create box same size as the screen and check"

yah, i think kevin had it right the first time.

some useful functions for the rest (gathered from the forum/other users):


i'm looking for previous code to position the points. bear with me

back with full code for positioning the points:


since it's a rectangle, the distance to each is the same but the code leaves it a bit open for other shapes.

you can use those points to draw your lines, etc. and, i'll leave it there for you to continue working on where i've had enough exercise for now

add: you see where i created the box as a polygon vs just using a box where, disappointingly, GetSpriteShapeNumVertices doesn't work on true box shapes added in case you were wondering.

we could still use a real box where computing the vertices manually wouldn't be too much of a chore (about the same as setting them in a polygon) but, that's another cat to skin at another time.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 9th May 2020 00:15
Quote: "Sprite/GetWorldXFromSprite/Sprite/GetWorldYFromSprite"

i'm sure that's more accurate than my attempt to achieve the same.

i don't try to re-invent wheels but need to refer to our inventory of them more often
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th May 2020 00:39
It's a big bunch of functions isn't it?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th May 2020 01:18
Bengismo method is best

but I think you just want to do the following not so sure there is a one solution formula

fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 9th May 2020 07:14
Thank you all! It is great to see so many people trying to help.

It seems, GetWorldXFromSprite what blink0k was linking is what I was looking for, I should read the docs more often I guess. With this command I can easily check if all 4 corners of a sprite left the screen.
But I admit checking collision what Bengismo was suggesting is the most accurate solution maybe but I am still not sure how is this effect performance when you have say 100+ sprite to check if they are on the screen. I would believe checking collision is more resource hungry than checking position but then I am maybe wrong depends on how GetWorldXFromSprite do the calculations. It is require some testing.

@Virtual Nomad Thanks a lot for the useful Math functions and links you posted. It is going to be very useful. I can solve it with the above techniques for now but regardless I don't give up figuring out the math and do it from scratch, after fighting this so long I just wan to know how this can be calculated.

@fubarpk, thanks for the code but your solution doesn't take the rotation in to account, as I explained in my original post, if the sprite is not a perfect square and it is rotated then simply checking the X and Y position is not efficient. But thanks.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 9th May 2020 08:32
This post has been marked by the post author as the answer.
It looks like you have your solution now but I have just spent a little time working on this so here it is:

You asked for a pure maths solution, so I have created a project with rectangular asteroids that rotate.
Firstly we check the position of the asteroid to see if it is off-screen. If it isn't then there's no point in doing the expensive maths. But if the centre is off-screen then we check each vertex in turn and if all four are off-screen we wrap the asteroid to the other side.

Zigi
14
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 9th May 2020 13:18
Thanks @Scraggle, this is looks amazing, exactly what I was looking for. I could definitely not figure this calculations out on my own. Thank you very much

Login to post a reply

Server time is: 2024-04-20 05:46:37
Your offset time is: 2024-04-20 05:46:37