Pinning to rotated rectangle area

Get help using Construct 2

Post » Tue Mar 18, 2014 7:04 am

Hi there,

I have a spite that is acting like a bucket of sorts. It is rectangular and starts off portrait in orientation. I can easily pin objects to this area randomly scattered around its surface *but*, if I rotate the sprite 90% clockwise that doesn't take effect when pinning. It basically pins to the previous portrait orientation which is effectively now hanging off the edges of my sprite.

Is there a way I can pin to this rectangle regardless of its rotation? Many thanks in advance.

Nick.

Image
Correct

Image
Incorrect
B
4
Posts: 20
Reputation: 242

Post » Tue Mar 18, 2014 7:28 am

I should add that they rotate with the sprite just fine, keeping their location within the container, it's just when adding them to the container it only works correctly when the sprite is portrait in orientation.

This is my event

Image
B
4
Posts: 20
Reputation: 242

Post » Tue Mar 18, 2014 9:04 am

It's because you are using the height and width of the object in the code ( the first line. ) Even when the bucket is sideways, you still use x and y coordinates and add random amounts related to it's standing position height and width.
You can give the bucket object an instance variable called "IsStanding" then;

on touched bucket
if bucket is standing
{use the code in the screenshot you shared }
else
{replace the formula: ((TrollySpace.X - TrollySpace.Height /2) + random(trollySpace.Height) , (TrollySpace.Y-TrollySpace.Width/2) + random(TrollySpace.Width))}


I hope I could describe it :)
B
18
S
4
G
1
Posts: 332
Reputation: 3,149

Post » Tue Mar 18, 2014 9:54 am

Hi there,

thanks for the speedy response! I've tried as you suggested and that makes it work for horizontal also, but the only problem is that it means the sprite has to be in either vertical or horizontal orientation, it's a car sprite and can rotate 360 degrees.

Sometimes the sprite is at 45 degrees or some other arbitrary angle.
B
4
Posts: 20
Reputation: 242

Post » Tue Mar 18, 2014 9:57 am

I think I'm going to try rotating the x and y coordinates manually to put them within the rectangle at any angle, I'll update with my findings.
B
4
Posts: 20
Reputation: 242

Post » Tue Mar 18, 2014 8:26 pm

I have managed to get a single algorith working for 90 degree angles, But for some reason it's not working with anything inbetween,

X =

(((((PinArea.X -PinArea.Width / 2) + random(PinArea.Width)) - PinArea.X) * cos(PinArea.Angle) - (((PinArea.Y -PinArea.Height / 2) + random(PinArea.Height)) - PinArea.Y) * sin(PinArea.Angle)) + PinArea.X)


Y =

(((((PinArea.X -PinArea.Width / 2) + random(PinArea.Width)) - PinArea.X) * sin(PinArea.Angle) + (((PinArea.Y -PinArea.Height / 2) + random(PinArea.Height)) - PinArea.Y) * cos(PinArea.Angle)) + PinArea.Y)
B
4
Posts: 20
Reputation: 242

Post » Tue Mar 18, 2014 10:54 pm

Yeah you are on the right track. But when it comes to math, I am fairly limited myslef. Try changing places of sin and cos... That's what I do and pray it works :)
B
18
S
4
G
1
Posts: 332
Reputation: 3,149

Post » Wed Mar 19, 2014 8:29 am

Okay so here's the issue, firstly cos and sin expect the value in radians not degrees. So to convert to radians from degrees you do this,

pi * angle / 180

Although oddly when I try this in c# compared to construct 2 I get,

C#

radians =1.5707963267949
cos = 6.12303176911189E-17 - sin = 1

Construct 2

radians =1.5707963267949
cos = 0.9996242169 - sin = 0.0274121336

also the bog standard windows calculator gives the same answer as construct 2. Weird?!
B
4
Posts: 20
Reputation: 242

Post » Wed Mar 19, 2014 7:19 pm

Okay finally got it, much simplified using functions now,

RotatePointX (object x, object y, area x, area y, angle)

((Function.Param(0) - Function.Param(2)) * cos(Function.Param(4)) - (Function.Param(1) - Function.Param(3)) * sin(Function.Param(4)) + Function.Param(2))

RotatePointY (object x, object y, area x, area y, angle)

((Function.Param(0) - Function.Param(2)) * sin(Function.Param(4)) + (Function.Param(1) - Function.Param(3)) * cos(Function.Param(4)) + Function.Param(3))

then to use it,

Call set position on the object

x = Function.Call("RotatePointX", PinObject.X, PinObject.Y, PinArea.X, PinArea.Y, PinArea.Angle)
y = Function.Call("RotatePointY", PinObject.X, PinObject.Y, PinArea.X, PinArea.Y, PinArea.Angle)
B
4
Posts: 20
Reputation: 242

Post » Wed Mar 19, 2014 7:26 pm

Message: firkinfedup is not yet permitted to post plain text URLS (300 rep required). 1 URLS removed. Why?
Here is the capx file
B
4
Posts: 20
Reputation: 242

Next

Return to How do I....?

Who is online

Users browsing this forum: Karpakis and 10 guests