how do i get a camera to orbit an entity?

Started by William, December 02, 2022, 08:52:05

Previous topic - Next topic

William

im not sure how to accomplish that then i thought of child entities but could not find a childentity command reference..

can you help?
im still interested in oldschool app/gamedev

Matty

I assume you are using blitz3d.

Pseudocode:

;entity you wish the camera to orbit around:
entity = createcube();
;camera you wish to do the orbiting
camera = createcamera()
;global frame counter
global frame = 0
;main loop
repeat
     frame = frame + 1
     ;dostuff()
     orbitcamera(camera,entity,100)
     renderworld
     flip
until keydown(1)

function orbitcamera(cam,ent,dist#)
piv = createpivot()
positionentity piv,entityx(ent),entityy(ent),entityz(ent)
positionentity cam,entityx(piv),entityy(piv),entityz(piv)
rotateentity cam,0,0,0
moveentity cam,0,0,-dist
entityparent cam,piv
turnentity piv,float(frame)*0.1,float(frame)*0.2,float(frame)*0.3 ;play around with this however you like
entityparent cam,0
freeentity piv

end function

;this is off the top of my head...but it should work

Matty

Note - I had to edit the above code - which you might not have seen the change...I added a 'freentity piv' in the function otherwise you'll get a memory leak from creating and losing the reference to the pivot each frame.

RemiD

#3
the idea is to have one 'root pivot' (or the entity itself) for the movement, and one 'rotor pivot' as a child of the 'root pivot', and the camera as a child of the 'rotor pivot', and when you turn/rotate the 'rotor pivot', the camera will follow.


William

im still interested in oldschool app/gamedev

William

how to move camera in orbit of an entity with a mouse?
im still interested in oldschool app/gamedev

Matty

in the above code where the turnentity command is applied to the pivot, instead of a fixed set of angles used to turn by, pass the mousexspeed and mouseyspeed multiplied by some factor, just remember to convert the speeds to floats first.

3DzForMe

This is very interesting stuff, might crack open B3d over the break, for some 3d stuff instead of my evolving Speccy esque adventures.  ;D
BLitz3D, IDEal, AGK Studio, BMax, Java Code, Cerberus
Recent Hardware: Dell Laptop
Oldest Hardware: Commodore Amiga 1200 with 1084S Monitor & Blitz Basic 2.1

William

Can you tell me the openb3d/blitzmax implementation?

Compile Error: Unable to find overload for entityx(Int). Argument #1 is "Int" but declaration is "TEntity". 

Function orbitcamera(piv:TMesh,cam,ent,dist#)
'CreatePivot:TPivot
positionentity piv,entityx(ent),entityy(ent),entityz(ent)
positionentity cam,entityx(piv),entityy(piv),entityz(piv)
rotateentity cam,0,0,0
moveentity cam,0,0,-dist
entityparent cam,piv
turnentity piv,Float(frame)*0.1,Float(frame)*0.2,Float(frame)*0.3 ;play around with this however you like
entityparent cam,0

End Function

im going to work on it in the morning.
im still interested in oldschool app/gamedev

William

okay, i got it working.

'camera

Local camera:TCamera=CreateCamera()
PositionEntity camera,130,1,-130

Function orbitcamera(cam:tentity,ent:tentity,dist#)
Local piv:TPivot=CreatePivot()
positionentity piv,entityx(ent),entityy(ent),entityz(ent)
positionentity cam,entityx(piv),entityy(piv),entityz(piv)
rotateentity cam,0,0,0
moveentity cam,0,0,-dist
entityparent cam,piv
turnentity piv,Float(frame)*0.1,Float(frame)*0.2,Float(frame)*0.3 'play around with this however you like
'entityparent cam,0
Freeentity piv

End Function

i had to comment out entityparent cam,0 to get it to work not sure what its for.

im still relatively umm.. a beginner to blitz code even though i've been around the language for decades, i am working it out.

can you explain what each section of the commands are for, the purpose? i can figure it out on my own i imagine, though.

i posted this in the wrong section sorry thought i might need blitz3d help at the time, i am using blitzmax openb3d.
im still interested in oldschool app/gamedev