Author Topic: Help a n00b out? Spare some code?  (Read 5920 times)

0 Members and 1 Guest are viewing this topic.

Offline n00bstar

  • ZX 81
  • *
  • Posts: 22
  • Karma: 8
    • View Profile
Help a n00b out? Spare some code?
« on: June 01, 2012 »
Alright, I'm not a very good coder, but I'm a somewhat smart-ish individual and I understand very quickly when things are explained for a very long time with short monosyllabic words.

Forever now I've been trying get my head around doing 3D in 2D. Things like filled vectors, or starfields into which you can move in all directions and whatnot. I know a lot of 3d-looking effects are 'faked' and when they are, I generally can figure them out quickly enough. However, some are clearly based around math formulas calculating an actual Z value somewhere in there..... and that's what I want to understand.

Please then, O' wizurdz of code, help this lowly coder?




Offline Kirl

  • Senior Member
  • Pentium
  • ********
  • Posts: 1217
  • Karma: 230
    • View Profile
    • Homepage
Re: Help a n00b out? Spare some code?
« Reply #1 on: June 01, 2012 »
There were 2 functions posted recently near the end of this thread that convert your virtual 3d xyz coordinates (that exist only in memory) to the eventual 2d xy screen coords. You can do a good 3d starfield with those. :)
www.kirl.nl
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 644
  • Karma: 24
    • View Profile
    • J2K's blog
Re: Help a n00b out? Spare some code?
« Reply #2 on: June 01, 2012 »
There's one of my old GDI based filled vector engines in the C forum, its fairly well commented and does the 3d in 2d transform. I wouldnt say its the best method of drawing the vector shapes but the transformation code is fairly simple to follow.  I used a version of it in Blitz for years :)

http://www.dbfinteractive.com/forum/index.php?topic=4291.0

The attachment doesnt work, but the codes in the thread should, just needs an object I think.

Offline n00bstar

  • ZX 81
  • *
  • Posts: 22
  • Karma: 8
    • View Profile
Re: Help a n00b out? Spare some code?
« Reply #3 on: June 01, 2012 »
Cool! That's perfect I'll take a look-see. I'm pretty sure I can get my lazy head around it if I can get a clear and well commented piece of code which I can play with. I'm usually pretty good at 'figuring stuff out' and I know I have like half the logic worked out already, I just need to see it in action and be able to eff around with the variables and see what does what.

Imma have a look right now :)

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Help a n00b out? Spare some code?
« Reply #4 on: June 01, 2012 »
As you said in your opening post, a 3d stars routine is a perfect way of learning how to represent 3d on a 2d screen, it would be a good idea to have a try at it.  All you need to remember is each point needs an x, y, z.  Define your x and y around 0,0 and have z as the depth, each time you plot a star, create co-ordinate by dividing x by z and y by z and then adding offsets to bring it to the centre of the screen.  These days its safe to use doubles for your x,y,z co ordinates but when you transform them into 2d, remember to convert to integers to save weird things happening!  Also bear in mind that in comparison to x and y, z will be a relatively small number otherwise all your stars will seem condensed into a small point.

Apologies for any spelling errors or duplication of other replies, I'm on my phone at the moment.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline n00bstar

  • ZX 81
  • *
  • Posts: 22
  • Karma: 8
    • View Profile
Re: Help a n00b out? Spare some code?
« Reply #5 on: June 04, 2012 »
Allllright, I think I got some (most?) of it down when it comes to showing a 3d space with 2d coords. Basically I made each dot have an x, y, z coordinate to make a basic cube, then I displayed each dot dividing its x and y by their z. Then I wobbled it around the X axis with a sine to confirm the dots would move together in a cube like fashion and after much reducing of numbers (you were right heh) I managed to contain it on screen.

Now. I've been doing some light reading on the matter and realize fairly quickly though experimentation that if I'm going to have this cube rotate without looking like an Escher wetdream, I'm going to have to figure out vectors. This is where I'm at with my thinking:

I understand 2d vectors fairly well. The vertices are all in relation to a 0,0 origin and to rotate them all you need is to apply the salt and pepper of demomaking, sin and cos, and poof they rotate around the origin like so many rotating things rotating around origins. Now I know this is kind of half of what I need for 3d rotation. I know you've linked a thread on 3d angles but the finer points are still escaping me. Boo :(

After some light reading on the matter, it would seem I have various mathemata..rati..ti...cal options available to me:
1- Axis and Angles (calculated via a series of long board game matches)
2- Quaternions (returns the angle based on your highscore on the classic shooter Quaternion)
3- Matrix (where you plug into a machine and wake up going 'I know kung fu, also 3d angles')
4- Euler (I have no jokes left sorry)

There seems to be extensive documentation available for most of this and I'm willing to read whatever is necessary, but I'm wondering if there's any path that's more oriented towards what we geeks are looking for: easy and fast (emphasis on grade school levels of 'easy'). There's lots of scary words in there.. like Scalar (whom I assume is the end boss in "Quaternion 2: Euler's Journey, an Axis and Angle Expansion(tm)").

So far matrices look to be the easiest way and likely to produce the fastest results, am I right? Or is there a demoscene way of doing this that involves only half a line a code that everybody but me knows about? :)

 

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Help a n00b out? Spare some code?
« Reply #6 on: June 06, 2012 »
That was quite amusing to read, n00bstar :)

I don't think you have to understand matrices or even quaternions to rotate a point.
So let's start gently and imagine a 3d point (x,y,z) rotating around the x-axis:
- the x coordinate doesn't change
- y,z move along a circle
- the circle's radius is the point's distance from the axis

So you can figure out the current angle of y,z on the circle,
add the desired rotation to the angle and calculate a new y,z using sin and cos:
Code: [Select]
y= radius * cos( curAngle );
z= radius * sin( curAngle );

newX= x
newY= radius * cos( curAngle + angle )
newZ= radius * sin( curAngle + angle )

And with some optimization this ends up in the typical formular for a 2d rotation matrix:
Code: [Select]
newY = radius * cos( curAngle ) * cos( angle ) - radius * sin( curAngle ) * sin( angle )
newZ = radius * cos( curAngle ) * sin( angle ) + radius * sin( curAngle ) * cos( angle )

newY= y * cos( angle ) - z * sin( angle )
newZ= y * sin( angle ) + z * cos( angle )
« Last Edit: June 06, 2012 by hellfire »
Challenge Trophies Won:

Offline n00bstar

  • ZX 81
  • *
  • Posts: 22
  • Karma: 8
    • View Profile
Re: Help a n00b out? Spare some code?
« Reply #7 on: June 07, 2012 »
OOookay we're getting somewhere. We'll I am anyways.. you guys are already all there having a party and playing games and being smart while I'm still grinding the low level mobs of newb island.. but this is much clearer!

So this takes care of rotating on one axis, which involves calculating a simple 2d angle on a unit circle for the coordinates of the OTHER TWO axis. Then to calculate all three axis, I'd pull this trick three times....so all in all, what I'm working towards is getting a pitch/yaw/roll thing going on? Interesting. For some reason I had always assumed there was some magical SuperSine math wizardry that would calculate this more...er..."one shot". But then again, I believed in Santa until last year....and I'm 34 :(

Thanks for the help! Off I go to make more dots do more things I don't fully comprehend!

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Help a n00b out? Spare some code?
« Reply #8 on: June 07, 2012 »
Then to calculate all three axis, I'd pull this trick three times....
For some reason I had always assumed there was some magical SuperSine math wizardry that would calculate this more...er..."one shot".
You can combine multiple transformations into a single one (and we're back into matrices already).
That's useful if you want to apply the same set of rotations to many points.
Just write your 2d-rotations in 3x3 matrix form and multiply the matrices:
Code: [Select]
( 1  0   0  )   ( cy  0 -sy )
( 0  cx -sx ) * ( 0   1  0  )
( 0  sx  cx )   ( sy  0  cy )
(cx,sx and cy,sy are sin/cos of the rotation-angle for the according axis)

Now you can apply the single resulting matrix to all your points.
« Last Edit: June 07, 2012 by hellfire »
Challenge Trophies Won: