Change origin at runtime

Post your work in progress addons and get feedback

Post » Mon Nov 30, 2015 2:22 pm

Hey guys.
First of all I know little about programming (let alone javascript), but I really needed this functionality in the sprite plugin.
So i fiddled around with the SDK and added 2 actions in the sprite plugin. To my surprise, it seems to work. The problem is that the collision poly doesn't get updated and I would really need some help from the gurus here!

Please backup the official sprite plugin before trying this.

https://dl.dropboxusercontent.com/u/14855413/c2/origruntime/sprite.rar

capx:
https://dl.dropboxusercontent.com/u/14855413/c2/origruntime/hotspot1.capx
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Tue Dec 01, 2015 2:54 pm

@rexrainbow
@R0J0hound
@ruskul
@newt
Sorry for tagging you guys (i'm sure I forget others too), but I would greatly appreciate if you can take a look at this and give me some guidelines.
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Tue Dec 01, 2015 6:46 pm

You may try looking in this plugin here:
plugin-polygon-update-2013-04-23_t77970?&hilit=polygon

In it there's an action to move the origin around and the collision polygon is updated. Maybe that could be a reference.
B
89
S
30
G
95
Posts: 5,154
Reputation: 63,444

Post » Wed Dec 02, 2015 11:04 am

@R0J0hound thank you very much!

I took a look at this excellent plugin and if I understand right, the setOrigin action offsets the polygon vertices?
What I'm aiming for is exactly how the Construct 2 editor works: When you change the origin of a sprite inside the editor, the origin stays at the current x and y and it's just the texture that "jumps" to an offset position. In the polygon plugin that doesn't seem to be the case.
However, I found a line of code inside the plugin that seems to do what I want but i can't figure out what values to pass:
this.collision_poly = new cr.CollisionPoly(new Array(0,0,0,0,0,0,0,0));

It seems these are pairs of x,y values for each point in the collision poly, starting from top-left and going in a clockwise fashion? I can't understand if these are local or world coordinates.

Thanks in advance.
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Wed Dec 02, 2015 5:03 pm

Best I know those are local points relative to the top left of the unrotated and unscaled object. It could be center too, you'd just have to test it. Worst case it would be relative to the hotspot.

Just a thought, I haven't looked at your code but it might be enough to just call update_bbox() after changing the hotspot.
B
89
S
30
G
95
Posts: 5,154
Reputation: 63,444

Post » Wed Dec 02, 2015 9:36 pm

@R0J0hound
I will try to figure out what those values mean. I already called update_bbox() and set_bbox_changed(), it's just the collision poly points that don't get updated and I suppose I'll have to find a way to shift them manually.
Once more thank you for your time!
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Thu Dec 03, 2015 2:34 pm

OK it seems I found something:
Firstly, It seems that if the collision poly is set to bounding box, the first point is bottom-left and the rest are in a clockwise fashion.
With the following code I was able to calculate the new collision poly:

this.type.animations[0].frames[0].poly_pts[0]= -hx/this.width
this.type.animations[0].frames[0].poly_pts[2]= -hx/this.width
this.type.animations[0].frames[0].poly_pts[4]= 1-(hx/this.width)
this.type.animations[0].frames[0].poly_pts[6]= 1-(hx/this.width)

this.type.animations[0].frames[0].poly_pts[1]= 1-(hy/this.height)
this.type.animations[0].frames[0].poly_pts[3]= -hy/this.height
this.type.animations[0].frames[0].poly_pts[5]= -hy/this.height
this.type.animations[0].frames[0].poly_pts[7]= 1-(hy/this.height)

Now the problem is that it's not instantly updated. It's only when you perform a scale or rotation operation that it "snaps" into its proper position.

Updated plugin and CAPX:
https://dl.dropboxusercontent.com/u/14855413/c2/originatruntime2/sprite.rar
https://dl.dropboxusercontent.com/u/14855413/c2/originatruntime2/hotspot1.capx

If you try it out, first enter some new values for x,y origin and then use right click to rotate and middle click to scale.

Please if someone can share his thoughts or can find why the poly is updated only after a rotation/scale operation I would really appreciate it.
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Fri Dec 04, 2015 5:55 am

Any chance you could do a quick copy paste of the code? I can't download the files currently but I could look at the code if it were here. Thanks!
Image
B
32
S
11
G
2
Posts: 563
Reputation: 5,106

Post » Fri Dec 04, 2015 8:59 am

@ruskul , sure man and thanks in advance!

edittime:
Code: Select all
AddNumberParam("HotspotX", "Set the sprite's HotspotX", "1");
AddAction(12, 0, "Set HotspotX", "Size & Position", "Set hotspotX to <i>{0}</i>", "Set the sprite's HotspotX", "SetHotspotX");

AddNumberParam("HotspotY", "Set the sprite's HotspotY", "1");
AddAction(13, 0, "Set HotspotY", "Size & Position", "Set hotspotY to <i>{0}</i>", "Set the sprite's HotspotY", "SetHotspotY");



runtime:
Code: Select all
    Acts.prototype.SetHotspotX = function (hx)
    {
        this.hotspotX=hx/this.width;
        this.type.animations[0].frames[0].poly_pts[0]= -hx/this.width
        this.type.animations[0].frames[0].poly_pts[2]= -hx/this.width
        this.type.animations[0].frames[0].poly_pts[4]= 1-(hx/this.width)
        this.type.animations[0].frames[0].poly_pts[6]= 1-(hx/this.width)
        this.set_bbox_changed();
        log("points = "+this.type.animations[0].frames[0].poly_pts );
       
    };

    Acts.prototype.SetHotspotY = function (hy)
    {
        this.hotspotY=hy/this.height;
        this.type.animations[0].frames[0].poly_pts[1]= 1-(hy/this.height)
        this.type.animations[0].frames[0].poly_pts[3]= -hy/this.height
        this.type.animations[0].frames[0].poly_pts[5]= -hy/this.height
        this.type.animations[0].frames[0].poly_pts[7]= 1-(hy/this.height)
        this.set_bbox_changed();
        log("points = "+this.type.animations[0].frames[0].poly_pts );
       
   };
B
13
S
5
G
1
Posts: 116
Reputation: 1,805

Post » Sat Dec 05, 2015 10:14 pm

Its cool you have gotten this far with it. For claiming to not know much code, you are well on your way. I am curious, I know its not ideal but can you set the angle of the sprite to itself.angle right after changing the polygon. You said that updates the polygon right? You could simply call it good if it works from there... I say this, but that solution would drive me up the wall...

So, what I think is, I imagine that internally contruct 2 only refreshes the polygon when it needs to. Basically, Lets say you have the polygons points stored in A. When construct 2 runs, it stores a new set of points in B. These are calculated based on the rotation and scale of the sprite. Calculating B takes a bit of work and there is no reason to perform that work every tick unless you have to. Changing an angle or scale would be a good reason to recalculate B. If you think about it, A is only useful for calculating B, where B is actually the real points of a collision polygon in the game world. Does this all make sense? Its the same reason you tell construct 2 to update the bbox. There is no reason to recalculate its position when it isn't changing. The same thing is true of the collision polygons points relative positions based on rotation.

I think if you look into the sprite you might find a clue in .SetAngle()... I am going to take a peek when I get a chance, but thats where I would start.

I am completely pulling this out of nowhere, just a hunch. But it would explain why
Image
B
32
S
11
G
2
Posts: 563
Reputation: 5,106

Next

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 1 guest