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.

DarkBASIC Professional Discussion / Scale affecting Offset

Author
Message
bodmcn
15
Years of Service
User Offline
Joined: 6th Oct 2010
Location:
Posted: 14th Oct 2010 11:59
I am utilising the SCALE SPRITE and it appears to be affecting the offset with the value of the scale.

For example:
If I position a sprite with offset to centre I can:

offset sprite spr, ( SPRITE WIDTH( spr ) / 2 ), ( SPRITE HEIGHT( spr ) / 2 )


If the scaling value is set to 50, then to centre it is:

offset sprite spr, SPRITE WIDTH( spr ), SPRITE HEIGHT( spr )


So in order to centre once scaling is in use I will have to do something like:

offset sprite spr, ( SPRITE WIDTH( spr ) / 2 ) * ( 100 / scale ), ( SPRITE HEIGHT( spr ) / 2 ) * ( 100 / scale )


It seems awfully cumbersome; am I missing something??

Thanks,
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 14th Oct 2010 13:14
I have no idea, but I can give you some advice in optimizing that process.

SPRITE WIDTH( spr ) / 2 is always constant, so save that into a variable : spr_width = SPRITE WIDTH( spr ) / 2

Same with the height : spr_height = SPRITE HEIGHT( spr ) / 2

So you'd have:



Aaand that's about all there is to optimize...

Hope someone else finds a better solution for you

TheComet

bodmcn
15
Years of Service
User Offline
Joined: 6th Oct 2010
Location:
Posted: 14th Oct 2010 13:36
I am hoping so too. I have created the following function, which unfortunately doesn't allow your efficiency (which is a good thought)
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 14th Oct 2010 16:32
Rather than passing the scale to that function, why not pass the size. Often my sprites are square, so I'd just need 1 sprite size passed, but anyway it would let you size the sprite exactly, and set the offset exactly - because your using literal parameters...



Health, Ammo, and bacon and eggs!
bodmcn
15
Years of Service
User Offline
Joined: 6th Oct 2010
Location:
Posted: 15th Oct 2010 05:50
Thanks for the response

While the centering is taken care of in the code snippet you provided, the scaling of the positional offset is not, in the example it is the radius. This is the crux of the problem as this requires the same scaling as the sprite itself.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Oct 2010 19:22 Edited at: 15th Oct 2010 19:23
Quote: "So in order to centre once scaling is in use I will have to do something like:"


If your goal is to just center the sprite... when you use OFFSET SPRITE before any scaling it doesn't matter what the scale changes to later.

In the following code snip OFFSET SPRITE is used only once on the sprite before scaling. Use the mouse wheel to change the sprites scale.



bodmcn
15
Years of Service
User Offline
Joined: 6th Oct 2010
Location:
Posted: 18th Oct 2010 04:51
Thanks again for the response, but it is not centering the sprite that is the issue but the offset thereafter; this offset is scaled with the sprite. The upshot being that if the offset is given as x=10 and y=10 when the sprite has been scaled to 50%, the screen offset of the sprite displayed is x=5 and y=5.

It entails a lot of re-calculating to ensure the sprite is displayed at the co-ords given as opposed to the scaled values. I want to make sure I am not missing something as I am very new to DB Pro.

Thanks,
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Oct 2010 12:14
I guess I'm not getting what you're trying to achieve.

When you place a sprite to the screen at specific coordinates and rescale to any size or change the offset in any direction the sprite moves accordingly to the offset but alway keeps the same coordinates where you placed the sprite. When the sprites scale is changed it doesn't change the offset either. The offset always stays the same no matter how big or small the sprite becomes until you decide to change the offset.

The following code snip is a copy of the first one I posted but with an added background screen to show the center of the screen. It pastes a sprite to the center of the screen to show what I described above and still has a sprite at the mouse coordinates. Just like in the first one the sprite rotates around the mouse coordinates based on the sprites offset.

Again use the mouse wheel to scale the sprite but also use the arrow keys to change the offset of the sprite on the fly. Notice that the sprite in the center changes position based on what the current offset is. If you move the mouse to the center of the screen the sprite that rotates around the mouse will be directly over the sprite in the center of the screen to show that the pasted coordinates (in the center of the screen) did not change at all during offsetting and/or scaling of the sprite. There shouldn't be a need to recalculate coordinates based on scale or offset since it stays exactly where you want it no matter what the scale or offset.



bodmcn
15
Years of Service
User Offline
Joined: 6th Oct 2010
Location:
Posted: 19th Oct 2010 12:52
Many thanks for putting the code snippet together. It does seem to demonstrate what I was explaining above.

If you run the code and then press the Up key to get an offset on the Y of say -75; then when you decrease the scale of the box using the mouse wheel, the radius decreases towards the centre. Likewise, if you increase the scale then the radius of the box increases.

The offset is altered by the scale of the box; which it really (I believe) should not be to allow for consistency in positioning.

I hope this makes sense...
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 20th Oct 2010 12:03 Edited at: 20th Oct 2010 12:33
Quote: "The offset is altered by the scale of the box; which it really (I believe) should not be to allow for consistency in positioning.

I hope this makes sense..."


Ah, now I understand. Unfortunately it's just the nature of the beast. When the sprites offset is that far off (anywhere really) and the scale changes it does its best to maintain the same offset distance relative to the sprite. When the sprite is made smaller it moves the sprite closer to the offset (without changing the offset numbers) to appear to be the same distance.

The same is true with anything 3D as an object goes away from the camera it appears to be smaller and any gaps in the object that are huge when close to the camera appear to be smaller and smaller the farther away the object moves.

The only thing I suggest is using pre-made sprite scales rather than using SCALE SPRITE. You can more easily predict where a sprite will be when the sprite is any size you've predetermined. This code snip now changes the image number used for the sprite when you use the mouse wheel rather than SCALE SPRITE like before.



bodmcn
15
Years of Service
User Offline
Joined: 6th Oct 2010
Location:
Posted: 20th Oct 2010 13:46
Thanks for your explanation - it now makes more sense.

The idea behind this game was to make a simple 2d game, then move into 3d to get the idea behind the engine before having to muck around with meshes and modeling; not sure if it was the best idea really.

Ah, well. Thanks again for your persistence in helping me understand.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 20th Oct 2010 20:37
Quote: "Ah, well. Thanks again for your persistence in helping me understand."


Np. I look forward to seeing your game.

Login to post a reply

Server time is: 2026-07-22 03:51:33
Your offset time is: 2026-07-22 03:51:33