Jump to content


Photo

Simple Projection Script


  • Please log in to reply
6 replies to this topic

#1 Rafferty97

Rafferty97

    GMC Member

  • New Member
  • 268 posts

Posted 05 January 2010 - 03:07 PM

Hi, this script is designed to make it easier to set up the projection for a 3D game.

The script is called d3d_set_projection_simple:
//d3d_set_projection_simple() made by Rafferty97//arg0 - x//arg1 - y//arg2 - z (height above ground)//arg3 - direction (looking at)//arg4 - up/down (0=looking forward, 90=looking straight up, -90=looking straight down)//arg5 - roll (0 = Normal, 90 = Leaning Right, 180 = Upside down, 270/-90 = Leaning Left)var xx, yy, zz, tx, ty, tz, ux, uy, uz, nx, ny, nz, ax, ay, az;xx = argument0yy = argument1zz = argument2tx = lengthdir_x(1,argument3)ty = lengthdir_y(1,argument3)tz = 0ax = lengthdir_x(1,argument3+90)ay = lengthdir_y(1,argument3+90)az = 0ux = 0uy = 0uz = 1//look up/downnx = vector_rotate(tx,ty,tz,ax,ay,az,argument4*(pi/180),0)ny = vector_rotate(tx,ty,tz,ax,ay,az,argument4*(pi/180),1)nz = vector_rotate(tx,ty,tz,ax,ay,az,argument4*(pi/180),2)tx = nxty = nytz = nznx = vector_rotate(ux,uy,uz,ax,ay,az,argument4*(pi/180),0)ny = vector_rotate(ux,uy,uz,ax,ay,az,argument4*(pi/180),1)nz = vector_rotate(ux,uy,uz,ax,ay,az,argument4*(pi/180),2)ux = nxuy = nyuz = nz//rollingnx = vector_rotate(ux,uy,uz,tx,ty,tz,-argument5*(pi/180),0)ny = vector_rotate(ux,uy,uz,tx,ty,tz,-argument5*(pi/180),1)nz = vector_rotate(ux,uy,uz,tx,ty,tz,-argument5*(pi/180),2)ux = nxuy = nyuz = nz//setting_projectiond3d_set_projection(xx,yy,zz,xx+tx,yy+ty,zz+tz,ux,uy,uz)

To use this, you also need this script called vector_rotate():
//vector_rotate() made by Rafferty97// x, y, z, ux, uy, uz, angle, r(x = 0, y = 1, z = 2)var xx, yy, zz, ux, uy, u, angle, r, mx, my, mz, s, c;xx = argument0yy = argument1zz = argument2ux = argument3uy = argument4uz = argument5angle = argument6s = sin(angle)c = cos(angle)r = argument7if r=0 {return (xx*(  sqr(ux)+((1-sqr(ux))*c)  ))+(yy*(  (ux*uy*(1-c))-(uz*s)  ))+(zz*(  (ux*uz*(1-c))+(uy*s)  ))}if r=1 {return (xx*(  (ux*uy*(1-c))+(uz*s)  ))+(yy*(  sqr(uy)+((1-sqr(uy))*c)  ))+(zz*(  (uy*uz*(1-c))-(ux*s)  ))}if r=2 {return (xx*(  (ux*uz*(1-c))-(uy*s)  ))+(yy*(  (uy*uz*(1-c))+(ux*s)  ))+(zz*(  sqr(uz)+((1-sqr(uz))*c)  ))}

This function rotates the vector (x, y, z) along axis (ux, uy, uz) by angle degrees.

I know this can be greatly optimized, so I will edit it later.

Here is an example of how to use it: Download advancedTest.gmk

Hope this can help people making 3D games,

-Alex

Edited by Rafferty97, 06 January 2010 - 11:50 AM.

  • 0



Want a place on the internet to put your files? Get 40mb free at Host-a.net
Gm member since 2007

#2 Lewis X

Lewis X

    Artist

  • GMC Member
  • 1050 posts
  • Version:GM:Studio

Posted 07 January 2010 - 12:50 PM

I saw this in your 3D red/blue engine, it works good
  • 0

#3 xot

xot

    GMC Dismember

  • GMC Elder
  • 4785 posts
  • Version:GM:Studio

Posted 08 January 2010 - 12:34 AM

Very helpful. Straight-forward to use and solves a common problem. It should make things a lot easier for many people. I'll probably use this myself at some point.
  • 0
GMLscripts.com, rise from your grave!

If any of my posts contain broken images or links, I can probably supply them for you. PM with a link to the post.

#4 Rafferty97

Rafferty97

    GMC Member

  • New Member
  • 268 posts

Posted 27 February 2010 - 11:55 AM

Thanks for the comments :D.
  • 0



Want a place on the internet to put your files? Get 40mb free at Host-a.net
Gm member since 2007

#5 slayer 64

slayer 64

    Slayer of gingers

  • GMC Member
  • 4301 posts
  • Version:GM:Studio

Posted 22 May 2010 - 10:20 PM

i think in this case, matrices are the way to go.
i wrote this and compared it to your code; even if you got rid of repetitive calculations, this way is more efficient.
the order of arguments is not the same as your script.
/*
	Arguments:
		0 x position
		1 y
		2 z
		3 roll
		4 pitch (up down)
		5 yaw   (left right)
*/

var 
	rotxDx,rotxDy,
	rotzDx,rotzDy,
	rotyDx,rotyDy,
	dx,dy,dz,
	ux,uy,uz,tt;

//get angle components
	rotxDx=lengthdir_x(1,argument3)
	rotxDy=lengthdir_y(1,argument3)
	
	rotyDx=lengthdir_x(1,argument4)
	rotyDy=lengthdir_y(1,argument4)
	
	rotzDx=lengthdir_x(1,argument5)
	rotzDy=lengthdir_y(1,argument5)
	
//direction vectors without any rotaions
	dx=1
	dy=0
	dz=0
	
	ux=0
	uy=0
	uz=1

//rotate up vector around x axis
	tt=uy
	uy=rotxDx*uy-rotxDy*uz
	uz=rotxDy*tt+rotxDx*uz

//rotate direction and up vector around y axis
	tt=dx
	dx=rotyDx*dx-rotyDy*dz
	dz=rotyDy*tt+rotyDx*dz
	
	tt=ux
	ux=rotyDx*ux-rotyDy*uz
	uz=rotyDy*tt+rotyDx*uz

//rotate direction and up vector around z axis
	tt=dx
	dx=rotzDx*dx-rotzDy*dy
	dy=rotzDy*tt+rotzDx*dy
	
	tt=ux
	ux=rotzDx*ux-rotzDy*uy
	uy=rotzDy*tt+rotzDx*uy

//set projection
	d3d_set_projection(
		argument0,argument1,argument2,
		argument0+dx,argument1+dy,argument2+dz,
		ux,uy,uz)

  • 0

5y5rs3d.pngfg0UQNL.png


#6 Rafferty97

Rafferty97

    GMC Member

  • New Member
  • 268 posts

Posted 23 May 2010 - 01:05 AM

My script does use matrix calculations ( rotate_vector() ). It calculates using just arithmetic and trig.

- Alex
  • 0



Want a place on the internet to put your files? Get 40mb free at Host-a.net
Gm member since 2007

#7 Agamer

Agamer

    GMC Member

  • GMC Member
  • 157 posts

Posted 05 November 2011 - 03:47 PM

Nice! Really nice! I've never actually used that kind of projection before (roll, pitch, yaw). As soon as I decided to try using full projection rotation, rather than using direction, I used vectors to make direction arbitrary, and rotation completely free. I wonder if there's any way to do this type of relative rotation in a script (of course, it would have to preserve values).
It would probably use the arbitrary vector rotation formulas:
Posted Image

<u,v,w> is the vector to rotate around (a unit vector), and (x,y,z) is a vector or point to rotate.

Edited by Agamer, 05 November 2011 - 03:51 PM.

  • 0

Package designer; resource maker; engine builder; program tool creator -- Be fully prepared to game:

My scripts My images My model designer My GUI components One of my engines Accepting texture requests