Jump to content


Photo

Creating Illusion of a model using sprites?


  • Please log in to reply
3 replies to this topic

#1 Zarthas

Zarthas

    GMC Member

  • GMC Member
  • 845 posts
  • Version:GM8

Posted 31 August 2011 - 08:51 PM

Hello all,

You haven't seen me in the forums, probably ever for most of you, I've been gone for a long, long time. But I'm messing with GM off and on again, and I'm having trouble wrapping my head around some math right now. It has nothing to do with my intoxication, I assure you.

I'm attempting to create the same effect that you see in games like Doom or Hexen, where an object uses different sprites for different angles the camera is viewing it. Here's my stuff :
var camdir,newdir;
camdir=point_direction(Camera.x,Camera.y,x,y)
newdir=facing-camdir //camdir-facing=no
if newdir<0{newdir+=360}
if newdir>=360 {newdir-=360}
//
newdir=floor(newdir/4)*90
if newdir=0{sprite_index=argument0 image_xscale=1}
if newdir=90{sprite_index=argument1}
if newdir=180{sprite_index=argument2 image_xscale=-1}
if newdir=270{sprite_index=argument3}

This is of course, not producing the desired effect. The last half of the code isn't really a concern, but I'm having a bit of trouble figuring how I'd get the correct angle to use. Can anyone point me the right direction? There were a pile of doom-sprite-like examples years ago, and I was able to do it once upon a time, but I've lost it all since :(
  • 0

#2 Pie Person!

Pie Person!

    GM 6+ Lover

  • GMC Member
  • 1973 posts

Posted 31 August 2011 - 10:34 PM

Try this:

sprite_index = floor(((((facing - point_direction(Camera.x, Camera.y, x, y)) + 540) mod 360) - 180) * 0.25);

I haven't tested it, but it should work just fine.
  • 0

#3 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 3024 posts
  • Version:GM:Studio

Posted 02 September 2011 - 12:50 PM

Pie Person!, you're onto something, but that piece of code doesn't make much sense on its own. Why are you multiplying by 0.25? Why should the sprite_index (?) be the floor of the difference in direction?
I'd rather do something like this:
diff=(direction-point_direction(x, y,camera_x,camera_y)+360) mod 360 //Get the difference in the interval <0,360]
n=sprite_get_number(sprite_index) //Get the total number of images in the sprite
image_index=floor(n*diff/360) //Set the image index to the correct number
Or, compressed to one line:
image_index=floor(sprite_get_number(sprite_index)*((direction-point_direction(x, y,camera_x,camera_y)+360) mod 360)/360)
I haven't tested this either, I might be completely wrong.

Edited by TheSnidr, 02 September 2011 - 12:51 PM.

  • 0

#4 Zarthas

Zarthas

    GMC Member

  • GMC Member
  • 845 posts
  • Version:GM8

Posted 06 September 2011 - 06:03 PM

Hey Gents,

I appreciate your help! After tweaking your code, TheSnidr, I was able to get the effect I wanted :D It snaps to a direction a little strange, but I think I'll just play with rounding the image_index rather than using a floor or something of that nature. Thanks again guys.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users