Hi,
It has been so long since I have not posted anything here. But I have never stopped scripting. I am currently creating a game engine The player can move in any direction pixel by pixel.
I added a function to the Sprite class which determines the coordinates of the vertices of a sprite based on the angle of rotation.
[ruby]class Sprite
attr_accessor :rectangle
alias :rectangle_initialize :initialize unless $@
alias :rectangle_angle :angle= unless $@
def initialize(*args)
rectangle_initialize(*args)
@rectangle=Rectangle.new
end
def angle=(a)
rectangle_angle(a)
c=Math.cos(r=a*PI/180.0)
s=Math.sin(r)
hs=self.bitmap.height*s/2
ls=self.bitmap.width*s/2
hc=self.bitmap.height*c/2
lc=self.bitmap.width*c/2
@rectangle.a.set(self.x-lc-hs,self.y-hc+ls)
@rectangle.b.set(self.x+lc-hs,self.y-hc-ls)
@rectangle.b.set(self.x+lc-hs,self.y-hc-ls)
@rectangle.c.set(self.x+lc+hs,self.y+hc-ls)
@rectangle.d.set(self.x-lc+hs,self.y+hc+ls)
end
end
[/ruby]
Note: Rectangle#a=[x,y]
This code works when Sprite#ox and Sprite#oy are the center of the sprite.
How to calculate the coordinates as ox and oy are not the center ? (e.g: ox = self.bitmap.width/5)
Trigonometry is so far :sad:
Thanks in advance,
Regards,
Berka
It has been so long since I have not posted anything here. But I have never stopped scripting. I am currently creating a game engine The player can move in any direction pixel by pixel.
I added a function to the Sprite class which determines the coordinates of the vertices of a sprite based on the angle of rotation.
[ruby]class Sprite
attr_accessor :rectangle
alias :rectangle_initialize :initialize unless $@
alias :rectangle_angle :angle= unless $@
def initialize(*args)
rectangle_initialize(*args)
@rectangle=Rectangle.new
end
def angle=(a)
rectangle_angle(a)
c=Math.cos(r=a*PI/180.0)
s=Math.sin(r)
hs=self.bitmap.height*s/2
ls=self.bitmap.width*s/2
hc=self.bitmap.height*c/2
lc=self.bitmap.width*c/2
@rectangle.a.set(self.x-lc-hs,self.y-hc+ls)
@rectangle.b.set(self.x+lc-hs,self.y-hc-ls)
@rectangle.b.set(self.x+lc-hs,self.y-hc-ls)
@rectangle.c.set(self.x+lc+hs,self.y+hc-ls)
@rectangle.d.set(self.x-lc+hs,self.y+hc+ls)
end
end
[/ruby]
Note: Rectangle#a=[x,y]
This code works when Sprite#ox and Sprite#oy are the center of the sprite.
How to calculate the coordinates as ox and oy are not the center ? (e.g: ox = self.bitmap.width/5)
Trigonometry is so far :sad:
Thanks in advance,
Regards,
Berka