April 25, 2019, 04:20:43 AM

Author Topic: Rotate rectangle with mouse?  (Read 92 times)

Offline wombats

  • Jr. Member
  • **
  • Posts: 29
Rotate rectangle with mouse?
« on: April 14, 2019, 09:32:09 AM »
Hi,

I'm trying to rotate a rectangle with the mouse. However, what is throwing me off is that in my project, objects have a handle (ox/oy in the following code) that is subtracted from their positions when drawing. How should I implement that into the rotation?
Code: [Select]
Graphics 640, 480

Global x:Float = 320 - 16
Global y:Float = 240 - 16
Global w:Int = 32
Global h:Int = 32
Global ox:Int = 16
Global oy:Int = 16
Global angle:Float = 45
Global rotate:Int = False

Repeat

Cls

   Local lx:Float = x - ox
   Local ly:Float = y - oy
   
   If MouseDown(MOUSE_LEFT)
   
      If MouseX() => lx And MouseX() < lx + w And MouseY() => ly And MouseY() < ly + h
         rotate = True
      EndIf
     
   Else
      rotate = False
   EndIf
     
   If rotate
     
   angle = -ATan2(MouseX() - (lx + (w / 2)), MouseY() - (ly + (h / 2)))
   If angle < 0 Then angle = 360 - -angle
     
   EndIf
   
   SetRotation(angle)
   DrawRect(lx, ly, w, h)
   
   Flip

Until KeyHit(KEY_ESCAPE) Or AppTerminate()

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: Rotate rectangle with mouse?
« Reply #1 on: April 14, 2019, 09:46:46 AM »
You could just backup the handle, set it to the one you like, rotate as you wish and set the handle back.

Or you pass the handle/offset to the draw command

MyDraw(offsetX, offsetY)
-> DrawRect(x + offsetX, y + offsetY, width, height)


bye
Ron

Offline wombats

  • Jr. Member
  • **
  • Posts: 29
Re: Rotate rectangle with mouse?
« Reply #2 on: April 14, 2019, 11:49:12 AM »
Hi,

Thank you for the reply. I have another question. I am trying to replicate behavior like this:

If the rectangle gets bigger or smaller, do I need to recalculate the handle? How would I do that?

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: Rotate rectangle with mouse?
« Reply #3 on: April 14, 2019, 11:52:17 AM »
Multiply with the scale change?

handle offset eg was 10,10
size changed from 100,100 to 150,100
-> scale changed by *1.5, *1.0
-> adjust offset by *1.5, *1.0
-> offset = 10*1.5, 10*1.0
-> offset = 15, 10


bye
Ron

Offline wombats

  • Jr. Member
  • **
  • Posts: 29
Re: Rotate rectangle with mouse?
« Reply #4 on: April 14, 2019, 12:12:33 PM »
Hi,

True, but I wouldn't be using scale functions as the rectangle could be resized to any width and height.

Truth be told, I'm not using BlitzMax for this, but I have found this forum very friendly and approachable, so I thought I'd ask here.

Offline Derron

  • Hero Member
  • *****
  • Posts: 1953
Re: Rotate rectangle with mouse?
« Reply #5 on: April 14, 2019, 12:50:07 PM »
Scale = newWidth / oldWidth.

Bye
Ron

Offline wombats

  • Jr. Member
  • **
  • Posts: 29
Re: Rotate rectangle with mouse?
« Reply #6 on: April 14, 2019, 02:07:36 PM »
Thank you. I appreciate your help