Jump to content


Photo

Changing a billboard-enemy sprite


  • Please log in to reply
11 replies to this topic

#1 deathzero021

deathzero021

    GMC Member

  • GMC Member
  • 420 posts
  • Version:GM8.1

Posted 28 May 2012 - 10:34 PM

Think of Doom if you must. basically flat character sprites represent enemies in my current project. I coded a nice and simple method for drawing the billboard always facing the camera (so no need to talk about that) now onto the problem. I created a sprites for 4 different angle of the enemy. left, right, front, back. Now the tricky part, how to correctly change sprite in relation to the player and the enemy's direction?

at first i thought, simply using the enemy direction would be enough, but in a 3d game, that's a disaster. so i know that there has to be a relation between the camera's direction and the enemy's direction. However i do not have an idea on how to approach this method.

EDIT: my problem has now been solved!

Edited by deathzero021, 10 September 2012 - 06:42 AM.

  • 0

#2 mcf1lmnfs

mcf1lmnfs

    GMC Member

  • New Member
  • 14 posts
  • Version:GM8

Posted 30 May 2012 - 07:42 PM

you are right with "there has to be a relation between the camera's direction and the enemy's"

for example if he has the same dir like you he is going to be faceing away from you, so you will see his back,

the pseudocode for this is

if abs(enemy.dir-my.dir)<45 degrees
his.picture=facing_away
else if abs(enemy.dir-my.dir-90degrees)<45 degrees
his.picture=facing_left
else if abs(enemy.dir-my.dir+90degrees)<45 degrees
his.picture=facing_right
else
his.picture=facing_toyou

you should change this code a little though it is a pseudocode!!! with some commemnts arround


for proper working of this code dir shoud be in degrees and you shoud limit it between 0 and 360
if dir<0
dir+=360
if dir>360
dir-=360
  • 0

#3 deathzero021

deathzero021

    GMC Member

  • GMC Member
  • 420 posts
  • Version:GM8.1

Posted 30 May 2012 - 08:54 PM

hmm that makes a lot of sense. That's the perfect structure i was looking for. thanks a ton! of course, now i gotta test it out.

EDIT: nope not working right. the enemy just seems to stay at the sprite facing me. here's the code:
<cut>
I'll continue to play around with the idea though.

EDIT2: okay i got it working some what better now. still seems a little strange, turning the camera a few degrees can get the sprite to change for the character and it seems to favor the up/down sprites the most even if it's barely moving up/down and mostly left/right. here's the code:
Spoiler


EDIT3: i think i figured out why the code can't work. abs removes the negative from the value, which is not a good thing. -180 and +180 are the same thing, but -90 and +90 are not the same direction. these need to be considered... i came up with a different structure but it's not doing much better...

Edited by deathzero021, 30 May 2012 - 10:37 PM.

  • 0

#4 briantrlov

briantrlov

    GMC Member

  • GMC Member
  • 610 posts

Posted 11 June 2012 - 09:46 PM

I'm not a 100% on the question.. but i think this might help?
http://gmc.yoyogames...1
  • 0

#5 lump

lump

    GMC Member

  • GMC Member
  • 64 posts
  • Version:GM8

Posted 16 June 2012 - 05:43 PM

Back when i was doing soom clones i had this problem, i used somthing along the lines of:

step event:
// i got 90 by dividing 360 by the amount of directions i have sprites for (4 sprites)
image_n=(point_direction(x,y,player.x,player.y)-direction)/90

draw:

d3d_draw_wall(i,i,i,i,i,i,sprite_get_texture(sprite name,image_n),1,1)
// for the 'i's just put your code


put all of the images in a spite going from right counter clockwise, Done :biggrin: :thumbsup:
Edit: you might need to check if image_n is a minus number and invert it e.g

if image_n<0
image_n=-image_n

Edited by lump, 17 June 2012 - 09:34 AM.

  • 0

#6 deathzero021

deathzero021

    GMC Member

  • GMC Member
  • 420 posts
  • Version:GM8.1

Posted 17 June 2012 - 02:31 AM

Thanks for the reply!
I'm defiantly going to test out your method when i can. I'll post my results when i do.
  • 0

#7 lump

lump

    GMC Member

  • GMC Member
  • 64 posts
  • Version:GM8

Posted 17 June 2012 - 09:27 AM

ok but beware! you might need to tweak as i wrote it on the spot.(although it does look right)

so report any errors to me and i will try to fix. :thumbsup:

I think the image_n convertion code is wrong
this should work:

if  image_n=-1 {image_n=1}
if  image_n=-2 {image_n=4}
if  image_n=-3 {image_n=3}
if  image_n=-4 {image_n=2}



Edited by lump, 17 June 2012 - 09:33 AM.

  • 0

#8 lump

lump

    GMC Member

  • GMC Member
  • 64 posts
  • Version:GM8

Posted 17 June 2012 - 09:36 AM

also the "90" in the step event should be on the same line. :whistle:

also you will need to round image_n

Edited by lump, 17 June 2012 - 10:05 AM.

  • 0

#9 Nebuer

Nebuer

    GMC Member

  • GMC Member
  • 21 posts

Posted 17 June 2012 - 09:01 PM

if you are familiar with basic's trig you can use tan(x) to solve the angle between the two points but this will produce an angle between 0-180

you can try using the built in arctan2() function which produces and angle between 0-360 as it accounts for the quadrant, if the built in arctan2 function isnt doing it then you can try making your own (if you script your own functions you are more aware of why things do and dont work i)

http://en.wikipedia.org/wiki/Atan2
  • 0

#10 Gamer3D

Gamer3D

    Human* me = this;

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

Posted 17 June 2012 - 09:53 PM

Find the direction from the enemy to the player (or vice-versa). point_direction will work, and is assumed for the rest of the example. (You can also use trig. If you do, replace 360 with 2 * pi)
Find the difference between that direction and the direction the enemy is facing. (Subtract one angle from the other)
Map that to the range [0, 360) by wrapping if it's out of range. You can do that using this script:
angle_direction = angle_direction mod 360;
if (angle_direction < 0)
  angle_direction += 360;
Now we map the real-valued range [0, 360) onto the integer range [0, num_directions). We do this by calling:
subimg = floor(angle_direction * num_directions / 360);

You can do a few other things for animations, but this should be enough to get you started.
  • 0

#11 briantrlov

briantrlov

    GMC Member

  • GMC Member
  • 610 posts

Posted 18 June 2012 - 01:58 AM

For real is the topic i posted not what ya'll are talking about?
Change sprite by getting direction of character and object to look real 3d? Perfect open source Posted Image
  • 0

#12 deathzero021

deathzero021

    GMC Member

  • GMC Member
  • 420 posts
  • Version:GM8.1

Posted 05 September 2012 - 06:05 AM

For real is the topic i posted not what ya'll are talking about?
Change sprite by getting direction of character and object to look real 3d? Perfect open source Posted Image

lol yeah that is exactly what i wanted and i downloaded the example. thanks a lot, this will come in handy :biggrin:

EDIT: my problem has now been solved!

Edited by deathzero021, 10 September 2012 - 06:41 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users