Author Topic: Convert "Game Maker" Direction, Speed, Gravity and Friction to GLBasic Formulas  (Read 7877 times)

Offline Millerszone

  • Dr. Type
  • ****
  • Posts: 307
  • www.smashedfly.com
    • View Profile
    • Smashed Fly
I'm working on a pinball type game. With "Game Maker" you had commands
like "Speed, Direction, Friction, Gravity, Etc...".
I'm having trouble with "Direction" of the ball in GLBasic.
The more the angle/direction the ball moves, the faster it moves. I would like
the ball to stay the same speed, unless I command the ball to move faster or slower.

I would appreciate if someone could show me an example of the commands above
converted to GLBasic. I'm sure it's very easy for most.

Thank you.
« Last Edit: 2010-Sep-29 by KidNovak »
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

Offline Scott_AW

  • Mr. Polyvector
  • ***
  • Posts: 214
    • View Profile
    • SAW Game Design
Re: Direction, Speed, Gravity and Friction.
« Reply #1 on: 2010-Sep-29 »
How are you doing your movement?

All those values are just basic variables in a type structure, thats how GM works.

Objects are like types, they have a bunch of variables, then those get processed.

You'd use the angle with sin and cos multiplied by the speed and added to the position for basic movement.

Gravity would be a global value that is deducted from the Y value every loop, unless a block is under the object.

For friction, use as a global value that affects speed.
If you loop the movement within the loop, like if your speed is 2 there would be two loops.  Then each loop the speed is reduced by the friction value.
« Last Edit: 2010-Sep-29 by Scott_AW »
Current Project, Orbital Contract Defense
http://gamejolt.com/games/adventure/code-name-ocd/9887/

BlackShadow now open source/resource(requires duke3d)
http://gamejolt.com/games/adventure/black-shadow-3d/9885/

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: Direction, Speed, Gravity and Friction.
« Reply #2 on: 2010-Sep-29 »
Hi KidNova. Without knowing how you are currently implementing direction of a ball in GLBasic its hard to guess what the problem is. So, in general, I just agree with what Scott_AW said.  :)

Also, when he mentions trigonometry (the Sin and Cos functions) well she is going to become your new friend if you're planning on a pinball game, even if you hate her. Equations for speed and direction will definitely need to use trigonometry as part of the calculations otherwise the chances are you'll get some odd looking ways of dealing with movement. Trigonometry is useful for all sorts of other game mechanics too.

In addition you may also want/need to use some classical mechanics equations if you want a really natural looking interaction between the virtual metal spheres and the pinball playfield. I say this because you're going to need to work out how to do collision detection with all those smooth angled pinball surfaces and how the ball(s) will react to them. Unfortunately I don't have ready-made GLBasic code that covers these things (others may do). You could search the net or there are books about basic physics for game development out there too.

Offline Millerszone

  • Dr. Type
  • ****
  • Posts: 307
  • www.smashedfly.com
    • View Profile
    • Smashed Fly
Re: Direction, Speed, Gravity and Friction.
« Reply #3 on: 2010-Sep-29 »
Actually I didn’t even start the movement code for GLB, I don’t know how to begin.
Basically what I’m trying to say is, what would be the GLBasic formula for the
GM “direction” command? (ex. direction=(cos or sin)????*speed)

These would be my starting ball at the pitchers mound variables:
y=224
x=268
gravity=0.125
friction=0.015
direction=270
speed=9

I believe once I know the “direction” formula, then I should be able to figure out
how to implement gravity, fiction.

Here is the "BALL" GM code/script for my Baseball Pinball game:
Code: (glbasic) [Select]
Information about object: obj_ball

Sprite: spr_ball
Solid: true
Visible: true
Depth: 0
Persistent: false
Parent: <no parent>
Mask: <same as sprite>

Create Event:
play sound sd_ball_launch; looping: false
for all obj_bat_rest: set variable canswing to true
set variable canpitch to false
set the gravity to 0.125 in direction 270
set the friction to 0.015
set variable direction to 270
set variable speed to 9

 Step Event:
if at relative position (0,8) there is object obj_bat
      set speed to 15 and direction to obj_bat.facing
      if sound sd_bat_hit is not playing
            play sound sd_bat_hit; looping: false
      if speed is smaller than 7
            if y is larger than 400
            if x is equal to 210
            with a chance of 1 out of 12 do  perform the next action
            set variable direction relative to 7.5
else
      if speed is smaller than 7
            if y is larger than 400
            if x is equal to 210
            with a chance of 1 out of 12 do  perform the next action
            set variable direction relative to -7.5

Collision Event with object obj_bat_rest:
play sound sd_bump_hit; looping: false
bounce precisely against solid objects
set variable speed to 3

Collision Event with object obj_top_rails:
if sound sd_rail_hit is not playing
      play sound sd_rail_hit; looping: false
if speed is larger than 5
      set variable speed relative to -1
bounce precisely against solid objects

Collision Event with object obj_bottom_rails:
if sound sd_rail_hit is not playing
      play sound sd_rail_hit; looping: false
if speed is larger than 5
      set variable speed relative to -1
bounce precisely against solid objects

Other Event: Outside Room:
destroy the instance
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

Offline Bursar

  • Mr. Drawsprite
  • **
  • Posts: 63
    • View Profile
Here's the code for my player update function from Glowing Rocks from Outer Space - an Asteroids clone:
Code: (glbasic) [Select]
// left and right arrow to rotate
IF KEY(203) THEN DEC player.angle, player.rotationSpeed
IF KEY(205) THEN INC player.angle, player.rotationSpeed

// keep angle within bounds
IF player.angle > 360 THEN DEC player.angle, 360
IF player.angle < 0 THEN INC player.angle, 360

// up arrow to thrust
IF KEY(200)
// calculate angle to thrust at
player.vectorX = COS(player.angle)*acceleration
player.vectorY = SIN(player.angle)*acceleration

// add new direction vector to current velocities
player.velocityX = player.velocityX + player.vectorX
player.velocityY = player.velocityY + player.vectorY
ELSE
// apply friction to slow down
player.velocityX = player.velocityX/friction
player.velocityY = player.velocityY/friction
ENDIF

// cap maximum spped
IF player.velocityX > topSpeed THEN player.velocityX = topSpeed
IF player.velocityY > topSpeed THEN player.velocityY = topSpeed
IF player.velocityX < -topSpeed THEN player.velocityX = -topSpeed
IF player.velocityY < -topSpeed THEN player.velocityY = -topSpeed

// update player position
player.x=player.x+player.velocityX
player.y=player.y+player.velocityY

As you can see, the left and right arrow keys change the angle of the ship, but in your case you will need to do this by calculating reflections and bounce angles as the ball hits objects on the pinball table.

The up arrow gives a fixed amount of thrust (acceleration = 0.1) and that's fed into the player vectors (which works out how much of the thrust is in the X direction, and how much is in Y).

If the player isn't thrusting, then the velocity is reduced by the friction amount (friction = 1.0045).

Finally I cap the player speed so the ship doesn't go to fast, and then I update its X and Y postions ready for drawing.

The complete source code is available here if you want to see how it all works together.

Hope it helps a bit :)

Offline Millerszone

  • Dr. Type
  • ****
  • Posts: 307
  • www.smashedfly.com
    • View Profile
    • Smashed Fly
Thanks Bursar,

Worked perfect, and I even learned how to use Types with your code.

I downloaded your game and code "Glowing Rocks from Outer Space" several days ago,
but didn't look at the code, i was to busy playing your game. I think I'll study the code
for the next couple of days. Excellent game!

The next problem I will probably have is determining what angle the ball will go when hit by the bat.

Thanks!

« Last Edit: 2010-Sep-30 by KidNovak »
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5

Offline Hatonastick

  • Dr. Type
  • ****
  • Posts: 474
  • Amstrad CPC 6128
    • View Profile
Wouldn't you be better off getting and learning how to use Box2D?  Assuming they've got it working now that is.

http://www.glbasic.com/forum/index.php?topic=3561.0

What GameMaker has built-in is essentially a 2D physics engine along these lines -- although Mark might have written it himself, I don't know enough about the internals of GM to comment (I'm one of many ex-GM users on this website).  I remember using it to power a 3D game I was working on.  You can use a 2D physics engine to handle 3D as long as your movement is only in 2 dimensions.  Erm not that this is what you asked.  I have a (bad) habit of answering unasked questions that appear in my head without external influence.  =D
« Last Edit: 2010-Sep-30 by Hatonastick »
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Offline Bursar

  • Mr. Drawsprite
  • **
  • Posts: 63
    • View Profile
Excellent, glad you're making progress :)

Hat: I thought about mentioning a physics engine as well, but didn't want to complicate things.

Offline Millerszone

  • Dr. Type
  • ****
  • Posts: 307
  • www.smashedfly.com
    • View Profile
    • Smashed Fly
I have most of the code finished for the ball move movement. I have to tweak the bat hitting the ball a little more.
I'll have to look more into Box2D. Will it work with the iPad?
Hardware: iMac 27", MacBook Air, PC 3.5Ghz Quad
Developing Tools: GLBasic SDK, Gideros Studio, PureBasic
Developing for: iOS, Android, Windows, OS X, webOS, HTML5