Rotation Methods in .NET Have Flaws

Image manipulation / transformation

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 3080 Views - Last Post: 28 January 2010 - 02:45 PM Rate Topic: -----

#1 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Rotation Methods in .NET Have Flaws

Post icon  Posted 28 January 2010 - 03:29 AM

http://www.dreaminco...htm#lastComment

In reference to this post, I am wondering how to achieve full animated rotations (yes I am making a video game) -- without parts of the image being cropped off during certain angles.

Apart from that, the submission works great.

Any ideas on how to rectify this problem though?

Regards,

Trent Jackson

Is This A Good Question/Topic? 0
  • +

Replies To: Rotation Methods in .NET Have Flaws

#2 ThatThatGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 15-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 03:54 AM

Developing Video games using GDI+ really is a mess work.....

as GDI+ is slow + it requires you to call the repaint the form again and again.... + makes the application slower and performs drastically worse...


why not take a bit of time and learn WPF using XAML....... you must have about it its in VS 2008



on top of that ..... you can rotate any object you like in WPF... with all types of Matrix Transformations.....

Why im recommending you this is .... GDI+ does software buffering of graphics where WPF does Hardware buffering of graphics
Was This Post Helpful? 0
  • +
  • -

#3 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 04:20 AM

View PostThatThatGuy, on 28 Jan, 2010 - 02:54 AM, said:

Developing Video games using GDI+ really is a mess work.....

as GDI+ is slow + it requires you to call the repaint the form again and again.... + makes the application slower and performs drastically worse...


why not take a bit of time and learn WPF using XAML....... you must have about it its in VS 2008



on top of that ..... you can rotate any object you like in WPF... with all types of Matrix Transformations.....

Why im recommending you this is .... GDI+ does software buffering of graphics where WPF does Hardware buffering of graphics


I disagree.

I feel that .NET coupled with GDI+ is well suited for small to mid sized video game projects.

1. It takes but only 2 lines of code to to turn a bitmap into a sprite.
2. It is an excellent object orientated language.
3. Runs just as fast as Java (I've done the tests)

In fact, C# is the programming language for the XBox.

.NET is only going to keep getting better and better.

I'm sticking with it.

Trent Jackson

This post has been edited by T.Jackson: 28 January 2010 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 04:39 AM

Anyhow,

I think I am going to shake and run with this method: http://jelle.druyts....enterInNET.aspx

It is a bit mathematically inefficient, but I can't fault it.

< 1mS to do a rotation p/ frame on a Pentium 4.

She'll do.

Easy greasy,

Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#5 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:06 AM

Another couple of years and the graphics libraries will probably wrap around hardware accelerated drivers.

The Window's wrap arounds at the moment are abstract, but the .NET syntax and all round general mythology are set in concrete. Won't change, I guarantee you!

Easy greasy,

Trent Jackson

This post has been edited by T.Jackson: 28 January 2010 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:23 AM

Wouldn't the simple solution be to take account of the clipping when first designing the sprites and not to use the areas which could be clipped?
Was This Post Helpful? 0
  • +
  • -

#7 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:28 AM

View PostAdamSpeight2008, on 28 Jan, 2010 - 04:23 AM, said:

Wouldn't the simple solution be to take account of the clipping when first designing the sprites and not to use the areas which could be clipped?


Yes indeed it would.

I need full 360 degree animated rotations.

I have tried everything that I can think of.

It is rare that I post questions on forums, I am usually able to work most things out on my own.

I'm stumped on this one buddy.

Got any other ideas?

Trent Jackson

This post has been edited by T.Jackson: 28 January 2010 - 05:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:34 AM

Nice remake of Galaga by Namco 1981 happening here (similar but different)

You know the game, then you'd know the rotations that I need.


Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:42 AM

Lets say the sprite is 32 x 32 pixels.

The most clipping will occur at 45 degrees (135, 225 and 315).
So to account for this the initial sprite has to be,

L=x * sqr(2) * 2

So (32/2) = 16, because we need to centre the sprite.

16 * sqr(2) = 22.627416
=>23

23 * 2
=46 pixels a side.

Remember you don't use the 7 pixels band around the edge,
Was This Post Helpful? 0
  • +
  • -

#10 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:47 AM

View PostAdamSpeight2008, on 28 Jan, 2010 - 04:42 AM, said:

Lets say the sprite is 32 x 32 pixels.

The most clipping will occur at 45 degrees (135, 225 and 315).
So to account for this the initial sprite has to be,

L=x * sqr(2) * 2

So (32/2) = 16, because we need to centre the sprite.

16 * sqr(2) = 22.627416
=>23

23 * 2
=46 pixels a side.

Remember you don't use the 7 pixels band around the edge,


Mate,

You've completely lost me on that.

I have tried making the sprite equal proportions to no avail.

Some code would be good.

I will credit you in the game.

Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#11 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:55 AM

Don't rotate the sprite.

Draw the unrotated sprite using a graphics object from the game area surface with a translation and rotation applied.
Was This Post Helpful? 0
  • +
  • -

#12 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 05:57 AM

View Postcrepitus, on 28 Jan, 2010 - 04:55 AM, said:

Don't rotate the sprite.

Draw the unrotated sprite using a graphics object from the game area surface with a translation and rotation applied.


Can you further a bit more on that?

Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#13 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 06:17 AM

The common trend for this today is to go into Photoshop and turn 1 sprite into 100 by manually rotating them.

Blit, blit bang! It usually is a faster approach.

Computers are fast enough now to do real time rotations of 100 sprites on the screen.

Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#14 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 06:27 AM

View PostThatThatGuy, on 28 Jan, 2010 - 02:54 AM, said:

as GDI+ is slow + it requires you to call the repaint the form again and again.... + makes the application slower and performs drastically worse...


Incidentally, this is not true.

You only invoke the paint method when you have completed a frame.

GDI+ is marginally slower than GDI, this is true.

Some people believe that this is due to some unnecessary complexity with GDI+ (google it)


Trent Jackson
Was This Post Helpful? 0
  • +
  • -

#15 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Rotation Methods in .NET Have Flaws

Posted 28 January 2010 - 07:06 AM

View PostT.Jackson, on 28 Jan, 2010 - 11:57 AM, said:

Can you further a bit more on that?


A Graphics object is a toolkit for drawing on some surface - commonly on a Bitmap or a Control. Each Graphics object can draw on one thing.

For my explanation of this, imagine that the graphics object is like the transparent plastic sheet that Disney animators used to use, only it's huge - much bigger than the surface. You have two things then - the surface you are drawing on below, and the sheet above. They both have co-ordinate systems, which initially line up with each other - (0, 0) is at the same point, and the x and y axis all agree with each other.

When you do a TranslateTransform(x, y), it is like moving the (0, 0) of the plastic sheet so that it overlies the point (x, y) of the surface.

When you do a RotateTransform(angle), it is like rotating the plastic sheet by angle degrees about its origin (plastic sheet co-ordinates).

When you do a DrawImage(image, point), you are using the plastic sheet co-ordinates.

So - if you want to draw an image rotated about its centre then:

1) Get a graphics object tied to the surface that you want to draw upon
2) TranslateTransform(x, y) where x will be the centre of rotation (usually also the centre of the sprite). This shifts (0,0) of the graphics object.
3) RotateTransform(angle) to rotate the axis of the graphics object about its (0, 0)
4) Draw the image, so that it's centre is at (0, 0)
5) Reset the graphics transform.

All done. It doesn't clip because you are drawing into the huge surface of the form - there's loads of room.

Option Strict On
Option Explicit On

Public Class Form1

	Private sprites As New List(Of Sprite)
	Friend WithEvents Timer1 As New Timer
	Private Const MaxSprite As Integer = 10

	Private Class Sprite
		Implements IDisposable

		Private disposedValue As Boolean
		Private Shared rand As New Random

		Private m_bounds As Rectangle
		Public Property Bounds() As Rectangle
			Get
				Return m_bounds
			End Get
			Set(ByVal value As Rectangle)
				m_bounds = value
			End Set
		End Property

		Private m_bitmap As Bitmap
		Public Property Bitmap() As Bitmap
			Get
				Return m_bitmap
			End Get
			Set(ByVal value As Bitmap)
				m_bitmap = value
			End Set
		End Property

		Private m_rotation As Integer
		Public Property Rotation() As Integer
			Get
				Return m_rotation
			End Get
			Set(ByVal value As Integer)
				m_rotation = value
			End Set
		End Property

		Public Shared Function GetRandomSprite(ByVal hostBounds As Rectangle) As Sprite
			Dim sprite As New Sprite
			With sprite
				Dim randWidth As Integer = rand.Next(10, 100)
				Dim randHeight As Integer = rand.Next(10, 100)
				Dim bm As New Bitmap(randWidth, randHeight)
				Using g As Graphics = Graphics.FromImage(bm)
					Dim smallestDimension As Integer = Math.Min(randWidth, randHeight)
					For i As Integer = 0 To smallestDimension \ 2
						Dim rec As New Rectangle(i, i, randWidth - (2 * i), randHeight - (2 * i))
						Using p As New Pen(Color.FromArgb(rand.Next Or &HFF000000))
							g.DrawRectangle(p, rec)
						End Using
					Next
				End Using
				sprite.Bitmap = bm
				sprite.Bounds = New Rectangle(rand.Next(0, hostBounds.Width - randWidth), rand.Next(0, hostBounds.Height - randHeight), randWidth, randHeight)
				sprite.Rotation = rand.Next(0, 360)
			End With
			Return sprite
		End Function

		' IDisposable
		Protected Overridable Sub Dispose(ByVal disposing As Boolean)
			If Not Me.disposedValue Then
				If disposing Then
					If m_bitmap IsNot Nothing Then m_bitmap.Dispose()
				End If
			End If
			Me.disposedValue = True
		End Sub

#Region " IDisposable Support "
		' This code added by Visual Basic to correctly implement the disposable pattern.
		Public Sub Dispose() Implements IDisposable.Dispose
			' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
			Dispose(True)
			GC.SuppressFinalize(Me)
		End Sub
#End Region

	End Class

	Sub New()
		' This call is required by the Windows Form Designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.
		Me.DoubleBuffered = True
		Timer1.Interval = 100
		Timer1.Start()
	End Sub

	Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		sprites.Add(Sprite.GetRandomSprite(Me.ClientRectangle))
		If sprites.Count > MaxSprite Then
			Dim sprite As Sprite = sprites(0)
			sprites.RemoveAt(0)
			sprite.Dispose()
		End If
		Me.Refresh()
	End Sub

	Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
		MyBase.OnPaint(e)
		For Each aSprite As Sprite In sprites
			' move the origin of the graphics to the centre of the sprite
			e.Graphics.TranslateTransform(aSprite.Bounds.X + (aSprite.Bounds.Width \ 2), _
				aSprite.Bounds.Y + (aSprite.Bounds.Height \ 2))
			' rotate the co-ordinates about the origin
			e.Graphics.RotateTransform(aSprite.Rotation)
			' draw the image so that its centre is at (0, 0)
			e.Graphics.DrawImageUnscaled(aSprite.Bitmap, -(aSprite.Bounds.Width \ 2), -(aSprite.Bounds.Width \ 2))
			' reset the co-ordinate system
			e.Graphics.ResetTransform()
		Next
	End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2