This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-07-10 17:38:33

Whackalicious
Scratcher
Registered: 2007-06-15
Posts: 11

Strafing?

Well, Mayhem sorta suggested to add strafing to my top-down shooter project, the problem is, however, I can't quite figure out how to have the sprite strafe to the left or right depending on it's direction, which depends on the mouse. Any suggestions?

Offline

 

#2 2007-07-11 01:12:31

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Strafing?

Hmm - I threw that suggestion out without considering the complexity of the issue.

The non-mathematical way would be to have a seperate "movement control" sprite that sets direction of movement, independently of the direction of shooting.  Your current sprite would be set up with permanent "goto movement sprite" and "point towards mouse" commands.

Then your movement sprite would work thus:

When up pressed, point towards mouse, move forward (amount)
When down arrow pressed, point towards mouse, move forward (negative amount)
When left arrow pressed, point towards mouse, turn left 90, move forward (amount)
When right arrow pressed, point towards mouse, turn right 90, move forward (amount)

*****

The other method would be to calculate the required changes in x and y geometrically.  Without sin/cos/tan functions, though, that would be a bit of a pain.

****

Did you resolve your shooting issue, by the way?

Last edited by Mayhem (2007-07-11 01:12:53)


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#3 2007-07-11 09:27:19

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: Strafing?

Cos and sin aren't that hard.  Just create a new sprite and three variables; PlayerDirection, cos, and sin.  Then for the sprite do this:

[When I receive [calculatesincos]]
[go to x(0) y(0)]
[point in direction (PlayerDirection)]
[move(1) steps]
[set cos to (y position) ]
[set sin to (x position) ]

In your player sprite where you're updating the direction based on the mouse position do this in your forever loop right after [go to front]:

  [set direction to (PlayerDirection) ]
  [broadcast [calculatesincos]]

This will calculate the sin and cos for the player direction every time it gets set.  Then in your key command section:

[if < key [d] pressed? > ]
  [go to x: ( ( xposition ) - ( ( (Stamina ) / (12.5) ) * ( cos ) ) ) y: ( ( y position + ( ( ( Stamina ) / (12.5) ) * ( sin ) ) ) ]
[if < key [a] pressed? > ]
  [go to x: ( ( xposition ) + ( ( (Stamina ) / (12.5) ) * ( cos ) ) ) y: ( ( y position - ( ( ( Stamina ) / (12.5) ) * ( sin ) ) ) ]

This takes the direction vector and adds it to the position, but it does it using a perpendicular vector instead of the forward vector.  It's basically doing this:
  x = x - (y direction)
  y = y + (x direction)
instead of what normally happens which is this:
  x = x + (x direction)
  y = y + (y direction)


If you keep your mouse in one position this will actually cause you to circle strafe since your direction is always pointing at the mouse pointer.

Last edited by Canthiar (2007-07-11 09:30:30)

Offline

 

#4 2007-07-11 17:16:27

Whackalicious
Scratcher
Registered: 2007-06-15
Posts: 11

Re: Strafing?

Thank you for your help Canthinar. Your method worked flawlessly.

Last edited by Whackalicious (2007-07-11 17:16:37)

Offline

 

Board footer