Rotate rectangle with mouse?

Started by wombats, April 14, 2019, 09:32:09

Previous topic - Next topic

wombats

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?
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()

Derron

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

wombats

Hi,

Thank you for the reply. I have another question. I am trying to replicate behavior like this: https://i.imgur.com/tYNfU67.mp4

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

Derron

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

wombats

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.

Derron

Scale = newWidth / oldWidth.

Bye
Ron

wombats

Thank you. I appreciate your help