/* edit: changed title */
Hi,
I'm currently toying around with ideas to calculate movement input and create movement functions (using Rex' function plugin).
(I can't post a cap right now as I'm on the go and can't access my pc for a while.)
Question 1
I have a LineOfSight for my Moving OBjects that connects to possible targets. In my current solution I check the angle of the LOS and use a switch case to determine the next movement (on a tiled grid with grid movement).
The move function is the same for my player MOB as well as NPC MOBs.
some pseudo code for my current 4way movement
switch LOS.angle
- case angle > 315 & angle < 45
- => call function(move, right, {distance})
- case angle > 45 and angle < 135
- => call function(move, down, {distance})
and so on...
As the move function translates right and down into the correct angle again, it would be easier to call the function with the correct value. I'm working with grid movement, so there are only 8 valid angles. I need a formula that takes the LOS angle and "snaps" it to the nearest valid angle for movement.
Is there a way to get this behaviour:
x = input -> output
x = 00.0 -> 0
x < 22.5 -> 0
x >= 22.5 -> 45
x = 45.0 -> 45
x < 67.5 -> 45
x >= 67.5 -> 90
x = 90.0 -> 90
...
I've seen formulas with ((X*Y)+Z)/N or something like that but except for the one time I copied a formula (i didn't understand but that magically worked anyway) I never worked with those in C2. It would save me a lot of events for my movement logic, when going from 4way to 8way movement.
Question 2a
I'm currently using 5 events to get the user input for movement (4way).
When enabling 8way movement, I'd need at least 9 events
some pseudo code for my future 8way movement
left pressed
- => set parameter movementAngle to 180
- => set parameter movementDistance to 16
- down pressed
- - => set parameter movementAngle to 135
- - => set parameter movementDistance to 22
- up pressed
- - => set parameter movementAngle to 215
- - => set parameter movementDistance to 22
right pressed
- => set parameter movementAngle to 0
- => set parameter movementDistance to 16
- down pressed
- - => set parameter movementAngle to 45
- - => set parameter movementDistance to 22
- up pressed
- - => set parameter movementAngle to 315
- - => set parameter movementDistance to 22
down pressed
- => set parameter movementAngle to 90
- => set parameter movementDistance to 16
up pressed
- => set parameter movementAngle to 270
- => set parameter movementDistance to 16
NOT up pressed
NOT down pressed
NOT right pressed
NOT left pressed
MOB.DistanceToMove = 0
Is there a way to simplify this?
Question 2b
For my 4way grid movement, I'm using the following
some pseudo code for my current 4way movement
MOB.DistanceToMove > 0
- => MobSprite: Set animation to MOB.MobType & "Walking"
- => MOB: Move 100*dt pixels at angle MOB.AngleToMove
- => MOB: Substract 100*dt from DistanceToMove
MOB.DistanceToMove < 0
- => MOB: Move 100*dt pixels at angle MOB.AngleToMove
- => MOB: Set DistanceToMove to 0
MOB.DistanceToMove = 0
- => MobSprite: Set animation to MOB.MobType & "Default"
As I'm using a 16 pixel grid, I simply set the MOB.DistanceToMove to 16 and I'm fine. If I implement 8way movement now, I don't know exactly what DistanceToMove I have to use. Pythagoras tells me it should be 22.62741699..... I think that might lead to problems on the long run. <img src="smileys/smiley2.gif" border="0" align="middle" />
I appreciate any help I can get that leads to cleaner code and less events. I'll be at my home pc again in 2 days, I'll add a capx then in case there's no good solution by then but judging from the stuff I read here, there ought to be someone smart enough to help me.
Pretty please? <img src="smileys/smiley9.gif" border="0" align="middle" />