Page 1 of 1

SCR Move - SinCosRadMove

Posted: Mon Dec 12, 2011 7:19 pm
by Nixola
I read this post, and I wanted to try to do something like that. So, in ~2½ hours, I coded this. My code is a mess ( I don't like love.update() ), and graphics are even worse, but it works.
In the top-left corner I put an HP bar (just in case I go on with this... thing) and a fuel bar, that replenish itself while standing still. Spacebar decreases HP by 10 and fuel by 90%, and ↓ set both to 100%.
EDIT: I missed a 0 (so ↓ only set fuel to 10%) and forgot to change window's title

Re: SCR Move - SinCosRadMove

Posted: Mon Dec 12, 2011 10:29 pm
by Robin
Nice.

Here's an exercise for you: it looks a bit like Astroid at this point. Only in space there is no friction: if there is no force working on an object it just goes on in the same direction forever. To stop or change direction, you'll need to turn and accelerate the other way. See if you can implement this.

Re: SCR Move - SinCosRadMove

Posted: Tue Dec 13, 2011 12:37 pm
by Nixola
I improved a bit SCR Move while you were writing your post (added small deceleration and a speedometer), when I read it I wanted to go to sleep (00:40 AM in Italy). Now I've gotta go, after lunch I'll try to implement that.
P.S: I wrote "coming soon" 'cause I didn't understand how to code the speedometer (I told you, it was late :oops: ), and when I understood it I didn't have the time to cancel the writing.
EDIT: forgot the attachment
EDIT: too many of them :shock:

Re: SCR Move - SinCosRadMove

Posted: Tue Dec 13, 2011 1:17 pm
by hughes
Neat! Looks good so far.

You probably noticed things don't look quite right when turning at high speeds. If you're going for an asteroids-feel where you drift along and rotate freely without turning like an airplane, you'll want the thrust to control acceleration instead of velocity.

What I mean is...

Code: Select all

in love.load
  vx = 0
  vy = 0
  thrust = 10 (or some appropriate number)
end

in love.update
  fx = 0 -- horizontal force
  fy = 0 -- vertical force
  if up key is pressed
    -- rocket is firing, apply thrust
    fx = fx + thrust*math.cos(angle)
    fy = fy + thrust*math.sin(angle)
  end
  -- the thrust only modifies the velocity
  vx = vx + fx*dt
  vy = vy + fy*dt
  -- now the resulting velocity modifies the position
  x = x + vx*dt
  y = y + vy*dt
end
Try this and you'll find that using forces in addition to storing the velocity makes for much more realistic motion.

Re: SCR Move - SinCosRadMove

Posted: Tue Dec 13, 2011 1:33 pm
by Nixola
I was thinking about something like that... Thank you ^^

Re: SCR Move - SinCosRadMove

Posted: Tue Dec 13, 2011 8:11 pm
by josefnpat
This is very very smooth, nice job!

May I suggest you round the percentage below the health bar and the speed on the spedometer

Also, alias the the throttle image (the thing that shows how fast you're going).


:)

Re: SCR Move - SinCosRadMove

Posted: Thu Dec 15, 2011 11:41 am
by coffee
Nixola, good work in engine but I have the feeling that you are using the wrong anchor point to rotate your ship (or you are not updating well that point). The rotation of the ship don't feel natural and ship isn't really rotating but like circle outside-facing around one point at the back of the ship. Or you really wish a rotation like this?

Re: SCR Move - SinCosRadMove

Posted: Thu Dec 15, 2011 2:50 pm
by Nixola
The point is the center of the ship (ship: 32x32px, offset: 16, 16), now II'm working on this post, I rewrited everything and controlled it at least 15 times, but I can't make the ship move like this :?

Re: SCR Move - SinCosRadMove

Posted: Fri Dec 16, 2011 3:00 pm
by Nixola
After 3 days hunting an error, I found a "math.sin" instead of a "math.cos" ( :x ), I named this new version "SCR Asteroids", even if there aren't asteroids (for now). I'll draw a better ship (maype :P ) and implement enemies spaceships. Until then, don't press spacebar. DON'T.

Re: SCR Move - SinCosRadMove

Posted: Fri Dec 16, 2011 4:22 pm
by hughes
Sweet! You've got it! That type of motion works really well and has a very spacey feel.