How do I Set an object's Angle to it's Sine Angle?

Get help using Construct 2

Post » Sat Mar 28, 2015 8:58 pm

I want to have an enemy face in the direction it is moving towards relative to it's Sine.

Anyone have any clue on how you could code this?
B
4
Posts: 13
Reputation: 226

Post » Sat Mar 28, 2015 9:46 pm

If you have access to the X and Y motion vectors for your object, the angle of motion can be obtained from the expression angle(0, 0, VectorX, VectorY). However, I may be misunderstanding exactly what you're looking for -- a capx or a diagram would be useful.
Moderator
B
104
S
39
G
19
Posts: 1,584
Reputation: 18,167

Post » Sun Mar 29, 2015 12:54 am

linkman2004 wrote:If you have access to the X and Y motion vectors for your object, the angle of motion can be obtained from the expression angle(0, 0, VectorX, VectorY). However, I may be misunderstanding exactly what you're looking for -- a capx or a diagram would be useful.


Yeah, let me draw you a quick picture.
B
4
Posts: 13
Reputation: 226

Post » Sun Mar 29, 2015 1:11 am

Message: Bohimen is not yet permitted to post plain text URLS (300 rep required). 1 URLS removed. Why?
Not sure if that worked, but basically I'm trying to have the enemy face in the certain direction in which he is moving. So when the sine is moving left - Angle Left. When the sine is moving Right - Angle Right. ect.
Last edited by Bohimen on Sun Mar 29, 2015 1:14 am, edited 1 time in total.
B
4
Posts: 13
Reputation: 226

Post » Sun Mar 29, 2015 1:13 am

Message: Bohimen is not yet permitted to post plain text URLS (300 rep required). 1 URLS removed. Why?
So yeah... good old Scirra doesn't want me to give you a picture... Annoying how they don't have a direct profile upload box and have you post it elsewhere and link it in, just to tell you you haven't posted enough and can't link anything...
B
4
Posts: 13
Reputation: 226

Post » Sun Mar 29, 2015 1:16 am

imagehousing.com/imageupload.php?id=1226259

just add w_w_w. in front.
B
4
Posts: 13
Reputation: 226

Post » Sun Mar 29, 2015 1:50 am

I've prepared a quick example to show how this can be done. I'm not sure how you have your object moving, so I set this one up to move at 100 pixels per second along the X-axis and to use the sine behavior on the Y-axis, but the same concepts should apply regardless.

Sine Facing example

There's a small bit of calculus involved to get what we want. We start off with our position functions for the object, which are as follows(not Construct 2 expressions, but close):

X: InitialX + 100t
Y: InitialY + Magnitude * sin(CycleAngle)

Take the first derivative to find the velocity:

Vx: 100
Vy: Magnitude * cos(CycleAngle)

Now that we have the velocity, we can use the angle() expression that I outlined in my original post to find the direction of travel. Note that with the sine behavior, Sine.CyclePosition is just the normalized angle used when calculating the oscillation, so we multiply 360 to turn it back into degrees.

As an additional note, please try to avoid so many posts in a row in the future -- you can always edit new information into your previous post.
Moderator
B
104
S
39
G
19
Posts: 1,584
Reputation: 18,167

Post » Sun Mar 29, 2015 6:32 am

Alright, will do. Thanks, this is pretty much what I'm wanting to accomplish with my sprite, the biggest difference is that is it angled downward and is moving using a bullet which changes as it passes over an invisible line. With this code I could just change it's Self.Y to whatever speed that I'm wanting so that's not really an issue.

Quick question though: If I were trying to code this to go at a downward angle and with a bullet behavior to the object, would this solution still work the same? The way you explained this is a bit complicated and I'm not sure if I follow what you are saying when it comes to replicating this mechanic.

When setting the object's angle this line of code: angle(0, 0, 100, Self.Sine.Magnitude * cos(Self.Sine.CyclePosition * 360)) -- I'm not sure I follow this entirely.
B
4
Posts: 13
Reputation: 226

Post » Sun Mar 29, 2015 9:08 am

here a very generic expression that adapts that principle and should work regardless of how movement is applied (you probably need to set oldx and oldy to the current position after creating the object, so you get no jitter on the first frame). Ofc if the bullet behavior sets the angle, those two might collide, so in case it is needed, deactivate that.
https://copy.com/eCpuz2P8WGfKCpyS
Image

If it jitters too much on irregular movement (like slow mouse drag) or maybe because of bad framerates, you might have to take the difference over more than 1 frame, then the code gets a bit more complicated.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
20
S
9
G
1
Posts: 786
Reputation: 3,729

Post » Sun Mar 29, 2015 8:06 pm

I don't really get these solutions. Unless I'm missing something I think you could just set the object's angle like this: 90+Object.Sine.Value
If you're letting the bullet behavior set the object's angle, you might want to set the sine to "value only" so they don't fight each other.
example https://dl.dropboxusercontent.com/u/135 ... hingy.capx
B
27
S
8
G
3
Posts: 185
Reputation: 4,272

Next

Return to How do I....?

Who is online

Users browsing this forum: Aphrodite, kirby and 22 guests