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 / [SOLVED] Bug? Or inconsistency with GetWorldXFromSprite and GetWorldYFromSprite

Author
Message
JonEnstrom
4
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 18th Oct 2020 21:11 Edited at: 18th Oct 2020 23:48
I am experiencing a consistency issue with using GetWorldXFromSprite (and GetWorldYFromSprite) to position a particle trail from the "rear" of a spaceship sprite..

When using a typical virtual resolution of 1920, 1080 these commands are working perfectly as expected. (see first video)

The problem is when I modified the code to use the percentage based coordinate system (by getting rid of the virtual resolution and setting displayaspect(-1) these commands are now operating differently. (see second video)

EDIT: I have discovered that the issue is with SetDisplayAspect(-1). I am still a little bit confused because I am using the percentage coordinate system specifically to avoid having to define an aspect ratio. A little stretch here and there will be fine but I want the game to fill the screen of whatever devices it's running on.

I made the ship bigger in the second video to more clearly see the issue, the reported coordinates returned by GetWorldFromSprite are moving away from sprite when the sprite is turned, not to mention off center when it's facing diagonally...

Is there some way I can fix this issue so I can stick with the percentage based coordinate system?

WORKING NORMALLY:


NOT WORKING:

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

Go to answer

JonEnstrom
4
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 18th Oct 2020 22:46 Edited at: 19th Oct 2020 16:50
One step forward, two steps back!

As I mentioned in my edit above I did figure out that SetDisplayAspect(-1) was the problem.

So in my actual game having this issue (which is meant to run in portrait mode) I changed the line to say SetDisplayAspect(1080/1920) and while the particles now follow ships correctly, for some reason now the game now displays in a perfectly square shape window on both PC and mobile device!
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 19th Oct 2020 12:30
Hi,

could be a rounding issue.

1080/1920 equals 0 as long as you're using integers.

So SetDisplayAspect(1080/1920) equals SetDisplayAspect(0), which will create an aspect ratio according to the resolution you've set.


PSY


PSY LABS Games
Coders don't die, they just gosub without return
JonEnstrom
4
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 19th Oct 2020 15:56 Edited at: 19th Oct 2020 16:54
Ok I suspected that was a brain-fart situation! So that solves the display aspect issue, thank you!

However the original problem still persists in that the GetWorldXFromSprite and GetWorldYFromSprite don't work properly unless you are using a virtual resolution or your display aspect is square.

Without a solution to the issue I will be forced to abandon the percentage based system and go back to a fixed virtual resolution.
Lupo4mica37
4
Years of Service
User Offline
Joined: 1st Jun 2020
Location:
Posted: 26th Oct 2020 21:12
What calculations are you using for the particle to revolve around the space ship at radius x?
????????
JonEnstrom
4
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 27th Oct 2020 18:40
I first set up each ship in the game to have a "thruster offset" which is basically how many pixels from the center of the sprite the thruster should appear. The offset for the Y axis is calculated by simply using half of the sprite height to get basically the "rear" of the ship. Then for a "single thruster" the X axis is simply 0 to make it centered (some ships have "dual thrusters" so for those I manually setup the X offsets to something -40 and +40 or whatever looks good for the particular sprite.

That offset is based on the ship facing the top of the screen (which is the orientation of the sprite images). To adjust for the ship facing different angles I am using:

SetParticlesPosition(thrust_particles_1, GetWorldXFromSprite(sprite, thrust_offset_1_x, thrust_offset_1_y), GetWorldYFromSprite(sprite, thrust_offset_1_x, thrust_offset_1_y))

That positions the particles at the "center rear" of the ship no matter which way it's angled in the world. Unfortunately this doesn't work right when using SetDisplayAspect (unless you set to 1:1)

There is probably a way to make the values correct after using SetDisplayAspect but I never bothered to figure it out, and so I switched my code back to using a 1920x1080 virtual resolution and carried on. I originally was trying to use percentage based code to make the game fill the screen on any device at any resolution, but since this was giving me trouble I aborted the plan and went back to traditional.
JonEnstrom
4
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 27th Oct 2020 20:19 Edited at: 27th Oct 2020 20:37
This post has been marked by the post author as the answer.
Interesting development:

I revisited the problem and tried to solve it by doing my own manual calculation instead of using the GetWorldFromSprite commands and unfortunately it produces the same results!

So first I calculate the current X,Y "heading direction" of the ship (x = cosine of the sprite angle, and y = sin of the sprite angle) then attempt to place the particles at the sprite center "minus" the offset amount multiplied by the heading direction. The result is exactly the same as using GetWorldFromSprite, just like the second video in the original post.. looking at the resulting behavior it seems like the Y value is pretty well correct, hence the correct thruster position at top dead center and bottom dead center, so it seems like it's the X value that needs some adjustment..

SOLVED: Finally got this one figured out. I had to divide the X component of the "heading" by the display aspect ratio (1.77 in this case) and it works!



Note: If you port this example code to tier 1 you'll have to make one minor change. The cos function in C++ takes radians while the cos function in AppGameKit takes degrees. So change the two occurences of GetSpriteAngleRad to just GetSpriteAngle
Lupo4mica37
4
Years of Service
User Offline
Joined: 1st Jun 2020
Location:
Posted: 28th Oct 2020 20:20
Glad you solved it and thank you for sharing the code. Take care.
????????

Login to post a reply

Server time is: 2024-11-21 23:20:39
Your offset time is: 2024-11-21 23:20:39