Main Menu

[Resolved] Calculating sprite's on screen x and y values?

Started by cozziekuns, August 26, 2011, 03:55:44 AM

0 Members and 1 Guest are viewing this topic.

cozziekuns

Fun little problem that I encountered when working with angles and sprites, and also something that's now really bugging me .-.

Is there any way I can track the location of a sprite according to it's ox value? What I mean is, say for instance I have a sprite that is at x value 0 and y value 0, but at an ox value of -272 and an oy value of -272. The sprite would be somewhere at the bottom of the screen, and my sprites x and y values (on the screen) are 272 and 272, even though my sprite is at 0, 0 according to it's actual x and y values. But if my sprite's angle is set at 180, the sprite's corresponding x and y (on the screen) is -272 and -272, and my sprite is at 0, 0 according to it's actual x and y values.

My dilemma is trying to get the sprite's screen x and screen y co-ordinates. Is there some kind of formula RGSS2 uses for getting these values? Or are the values stored in some default variable that I don't know about?

I'm not sure if I explained this very well, and I will be happy to elaborate on my problem.

Infinate X


cozziekuns

Quote from: Infinate X on August 26, 2011, 04:36:57 AM


Not exactly what I was looking for. I was refering to a sprite and it's ox and oy values, as well as it's angle, not an events location. But thanks for the help, I guess.

pacdiggity

That's a confusing one there. Of course, there's the obvious solution of printing the sprite's ox coordinate, but that would be impractical and annoying. No.
What I would do is have a dummy sprite follow the sprite, so that the dummy's x and y are equal to the actual sprite's ox and oy, or inversely so if required. If the angle of the sprite is 0, leave them unaltered, if it is 180, multiply it by -1, if it is 90 half it(?) and for 270 half and multiply by -1.
Thinking about this is making my head hurt, and I don't even know if I'm helping you at all.
Something that would help me is seeing your problem in action. If you could recreate the problem in code and post it, you'll probably get more constructive answers.
it's like a metaphor or something i don't know

Zeriab

The rotation is anchored around the ox and oy coordinates. Try changing those. You could for example set it to half width and half height.
Is this fine or do you still want to know what I assume to be the x- and y-coordinate of the top-left corner of the  bounding box after the sprite's angle has been processed.
I don't think you have access to the underlying transformation matrix so you would probably have to use trigonometric functions to approximate the coordinate.

*hugs*

cozziekuns

#5
I realize that the angling of a sprite's screen x and y values can be changed with ox and oy values, but like you said I want to know the screen x and screen y co-ordinates of the top right corner of the bounding box after the sprite's angle has been processed. I too was thinking about using trigonometric functions, though I'm not sure how accurate it would be, and I'm not sure where to start.

EDIT: Nevermind, I eventually sorted it out. If anyone wants a demo of the snippet I made, it's here:


module Math
 
  def self.sind(value)
    return sin((Math::PI / 180) * value)
  end
 
  def self.cosd(value)
    return cos((Math::PI / 180) * value)
  end
 
end

@sprite = ::Sprite.new
@sprite.bitmap = Bitmap.new(32, 32)
@sprite.bitmap.fill_rect(0, 0, 32, 32, Color.new(255, 255, 255))
@sprite.angle = 0
@sprite.x = (544 - 32) / 2
@sprite.y = (416 - 32) / 2
@sprite.ox = 0
@sprite.oy = 0

loop do
  Graphics.update
  speed = 1
  @sprite.x += (speed * Math.sind(@sprite.angle + 90) * 2)
  @sprite.y += -(speed * Math.cosd(@sprite.angle + 90) * 2)
end


Thanks guys for helping me out! Resolved.