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 / How to "fix" out of center?

Author
Message
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 15th Apr 2018 21:46
Hello

I have a simple question, been thinking a lot but can't really come to a solution.
Let's say you have a 3D app, but with some GUI elements, an element of 100px on top and an element of 200px on the left.

I have a simple image here:

The red star is the 'real' center, the green star is the spot the user expects to be the center when loading an object, panning around it etc.

How would one 'fix' this problem of the center being 'wrong' because of GUI elements on the sides?

Any insights would be very helpful, thanks a lot!
Westa
13
Years of Service
User Offline
Joined: 28th Oct 2011
Location:
Posted: 16th Apr 2018 05:31
When you first load a 3D object - if its off center then you should first reposition it so that is CENTER is at 0,0,0

SetObjectPosition(2,0,-15,0)

Then call fixobjectpivot - this will change the center point of the object.
FixObjectPivot(2)

Now it will pivot as expected and position as expected

Westa
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 16th Apr 2018 06:09
you will need to use these commands

fubar
Blendman
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location: Arkeos
Posted: 16th Apr 2018 07:27 Edited at: 16th Apr 2018 07:59
Hi

Your gui :
- left : spriteLeft width = 200px
- top : spriteTop height = 100px

That's ok ?

So to get the redcenter :
X = virtualWidth *0.5
Y = virtualHeight * 0.5


Green center :
X = (virtualWidth + getspriteWidth(spriteLeft )) * 0.5
Y = (virtualHeight + getspriteHeight(spriteLeft )) * 0.5

This should work, unless you resize the window.

So you need to use GetScreenBoundsLeft(), GetScreenBoundsTop() to place your center correctly.

I hope this help

EDIT :
oup's in 3D.

You need to convert the screen coordinate from screen to 3D, with :
u = 5000
worldX# = Get3DVectorXFromScreen( X, Y ) * u
worldY# = Get3DVectorYFromScreen( X, Y ) * u
worldZ# = Get3DVectorZFromScreen( X, Y ) * u
worldX# = worldX# + GetCameraX(1)
worldY# = worldY# + GetCameraY(1)
worldZ# = worldZ# + GetCameraZ(1)


obj = ObjectRayCast(Grid,getcamerax(1),getcameray(1),getcameraz(1),worldx#,worldy#,worldz#)

Grid is a 3Dplane to put your 3D object

A test (I'm sur not but it seems to works) :
AGK2 tier1 - http://www.dracaena-studio.com
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 16th Apr 2018 13:22 Edited at: 16th Apr 2018 15:44
To correctly set the 3D render to your view area....
1) You can render your 3D view to an image (SetRenderToImage()) then place a sprite which fills just your view area which uses your rendered image. Then the 3D view is correctly centered in the view area.

2) Alternatively you can use SetCameraOffCenter( 1, 1 ) and SetCameraBounds( cameraID, left, right, top, bottom ) to render your 3D view only to the view area on the screen.
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 16th Apr 2018 20:30 Edited at: 16th Apr 2018 21:25
Thanks a bunch everybody for answering!

Blendman wrote: "Clever code with raycasting."


This works beautiful. Only thing I can imagine is the fact that panning or zooming will still use the whole window. I would never have thought about raycasting.

Bengismo wrote: "1) You can render your 3D view to an image (SetRenderToImage()) then place a sprite which fills just your view area which uses your rendered image. Then the 3D view is correctly centered in the view area. "

Clever, I tried it, but my sprite is always empty, I'm now just getting the screen, an putting it on the same place, so it should show the sphere, but it doesn't. Commenting the sprite just shows a sphere as expected.
Also, if this would work, how would you scale the sprite appropriately?

I really don't get why the sphere is not showing, I'm calling Sync() so it should render, shouldn't it?



Bengismo wrote: "2) Alternatively you can use SetCameraOffCenter( 1, 1 ) and SetCameraBounds( cameraID, left, right, top, bottom ) to render your 3D view only to the view area on the screen."

This sounds like exactly what I need, but how does it work? I now have this:




This code doesn't seem to do anything, fiddeling with values doesn't seem to change things!

Thanks for any help guys, you give me so much insights!
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 16th Apr 2018 21:23 Edited at: 16th Apr 2018 21:27
CarHartGuy wrote: "Clever, I tried it, but when I SetRenderToImage() I cannot seem to render the sprite to the screen (as that is also rendered to the image I guess. Is there a way around this? Also, how should I scale the the image?
"


You have to render a scene to the image... then call render again to make your final screen which has a sprite on it (which is using the image you just rendered) as well as your GUI elements etc. You have to render twice so that your render is displayed on the sprite.

1) Render to an Image First
2) Then render your final screen using the image made in step 1 on your sprite and also draw your GUI elements etc...

The example in SetRenderToImage() shows how to do this and there have been examples posted on the forum on how to do it too. Also see the RendertoImage example that comes with AppGameKit in the "others" folder in examples. This shows how you can render to an image then use that image on another screen or in 3D itself.

CarHartGuy wrote: "This sounds like exactly what I need, but how does it work? I now have this:"


SetCameraBounds( 1,l,t,b,r ) ONLY affects the 3D rendering as it only affects the camera projection.
The numbers you pass to it are not screen pixel coordinates. They are actual values to go into the projection matrix. Typically = Fnear*Tan(FOV/2)

You can have multipleviewports too
See.....https://forum.thegamecreators.com/thread/212232?page=21#msg2615964

So you can render 3D to a section of your screen easily using this and the scissor function if you wanted (with the centre of your render where the camera is pointing). You can have camera viewports like in a 3D editing program if wanted and render the same scene from multiple angles.
The maths is quite difficult though so if your not familiar with projection matrices or open GL then this probably isnt for you really.
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 16th Apr 2018 21:32
Thanks a bunch Bengismo!

I already altered my previous answer because the documentation pointed me in the right direction with rendering to an image and then render to the screen again. I got it working now!
I used Sync() instead of Render(), now it's working!

Thanks again for your help.

I will forget about that SetCameraBounds() function, as it's way above me. I never even heard about projection matrices.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 16th Apr 2018 23:29
Quote: "I will forget about that SetCameraBounds() function, as it's way above me. I never even heard about projection matrices."


I would like to know how its done this way as any attempts I have made have failed.
Blendman
10
Years of Service
User Offline
Joined: 17th Feb 2014
Location: Arkeos
Posted: 17th Apr 2018 08:17
Quote: "Only thing I can imagine is the fact that panning or zooming will still use the whole window."


hi

If you want to pan and zoom : use the camera (for zoom use movecameralocalZ() for example), not the view and it will works fine

For your GUI, I have forgotten to add : FixSpriteToScreen(sprite, 1).
AGK2 tier1 - http://www.dracaena-studio.com
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Apr 2018 21:32 Edited at: 19th Apr 2018 08:47
PartTimeCoder wrote: "I would like to know how its done this way as any attempts I have made have failed."


The maths can be a bit complicated but if you wrap it up into simple functions it becomes fairly easy. The SetCameraBounds function isnt too well documented but it does work fine!

A quickly put together, simple, dirty example using 4 3D viewports is shown below.



EDITED: 19/4/2018 - Corrected aspect ratio error
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 19th Apr 2018 05:06
That works fantastic

split-screen and viewports based on a division of the screen size seem to work very but an oddly shaped viewport like the client area of an app messes with the aspect ratio, I wonder in this case would it be best to render to image?
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 19th Apr 2018 08:37 Edited at: 19th Apr 2018 08:38
Actually, you spotted bug in my code! I put that together fairly quick with no real testing.

I was using the aspect ratio of the whole screen for the viewport which is incorrect if the viewport is tall and thin or wide but short

if you replace the RenderTo3DArea function with this one it corrects the aspect ratio of the viewport


So it should work now without squashing any of the views

You can set the aspect ratio of any viewport to be different from others if you wanted to....you could also have one viewport zoomed in and one with a fisheye lens etc... (Its just the maths is a pain though)

So ...well done for spotting the bug! Rendering to a texture is a nice option too but its possible to get the aspect wrong with that too.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 19th Apr 2018 21:10
That's perfect, thank you very much I would have never figured this out by myself based on the information in the help file, its given my 3D editor a real professional feel when expanding/collapsing the sidebars to have the view center shift by the same amount.

There is a small camera Y issue though but I guess its a expected result of the camera shift, I got around this by adding/subtracting half the width of the desired change in x position to/from the camera y position, there is a tiny noticeable change in the object size (or camera zoom) but I can live with it, aspect ratio is perfect no matter what size I set.



Login to post a reply

Server time is: 2024-11-23 21:29:56
Your offset time is: 2024-11-23 21:29:56