Skip to main content

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password
Register
  • Home
  • Browse
    • 2D Art
    • 3D Art
    • Concept Art
    • Textures
    • Music
    • Sound Effects
    • Documents
    • Featured Tutorials
  • Submit Art
  • Collect
    • My Collections
    • Art Collections
  • Forums
  • FAQ
  • Leaderboards
    • All Time
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
    • Weekly
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
  • ❤ Donate
Programming

2D semi-realistic physics

Fracture
Friday, March 16, 2012 - 10:49

hello everyone

 

i'm working with an engine which has native Sonic-like "sticky" physics, with acceleration/deceleration on slopes, running on walls/ceiling, the sort of things you'd expect in a Sonic game.

A few days ago, the new fast loop feature was released, and i'm pretty sure that will allow me to process real-time + (almost)real-world physics.

 

What i'm looking for:

References and knowledge about physics in 2-d planes.

What i already have:

-basic movement physics, like gravity & jumps, acceleration, deceleration, speed, some trajectories like straight, elliptical, sinusoidal.

-support for variable-controlled movements. for example, i made the camera pan based on the player's speed. it never uses a fixed value.

 

What i aim to:

Develop physics similar to Angry Birds, Worms. and the like. The physics are meant to be applied to objects other than the player, and the tiles act as ground/walls. I don't want to destroy tiles, but i want to create destructible, physics enabled, interactive objects.

 

few examples:

 

*throw projectiles in arcs (bow and arrow, bullet drop)

*tumbling objects, weight and flexibility.

 

please be aware that i do not know how to read ALL code, try and put it in pseudo code as much as possible.

"if «this» happens" "define variable «X», value «Y»"

instead of

if "this"

{

var float "X" "Y"

}

 

Any ideas?

  • Log in or register to post comments
LeeZH
joined 14 years 10 months ago
Friday, March 16, 2012 - 16:15
LeeZH's picture

Well, the easiest thing to do is to simply plug in Box2D. There have been various ports to different languages like Flash and JavaScript and used extensively by numerous games, so it's no slouch. I wouldn't be supprised if Angry Birds used them as well.

However, if you insist on making things yourself, I am a first year mechanical engineering student so I could try helping out. Just ask any topic and I'll answer as much as I can.

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Friday, March 16, 2012 - 22:07

thank you.

 

I'm actually wanting to do it by myself, so i can learn some maths and physics in the process, fields in which i admit i'm not too good, but i'll try my best to keep up :)

first thing i'm wanting to try is objects that upon collision with the player, receive the player's speed. i've gotten fairly ahead in this, but i'm failing to grasp the logic of bounces and gravity combined.

also i'm not a "real" coder, i get by with an engine controlled by scripts, so i can't adopt any physics libraries.

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
LeeZH
joined 14 years 10 months ago
Saturday, March 17, 2012 - 04:59
LeeZH's picture

Just so we're on the same page, I'm assuming you know how to use vectors, because they simplify calculations *A LOT*. However, if your engine doesn't support vector maths, you can technically split them into x and y components, although things like calculating magnitudes would be unnecessarily messy.

I've noticed you got some trouble combining forces, and this is probably due to the way "sticky" physics in older 2d games were done. In older platform games, they often calculate just the velocity and apply it to the position. If you want a more realistic representation, there is acceleration / force to consider as well.

Newton's first law states that an object is stationary or at constant velocity unless there is a net force is applying on it. Newton's second law states that an object accelerates proportionally to the net force per mass.

Each object should store its own velocity, position and mass.

set total_force as (0, 0)

do calculations for each force and add it to total_force

add (total_force / mass * time) to velocity

add (velocity * time) to position

Let's start with the easiest of all the forces, gravity. Gravity on the surface of the Earth is 9.81 x mass. The 9.81 I've given is in SI units, so you'll need to convert it to whatever scale you're using. Over time, as we learn the other forces, we'll add it to the code above.

For now, you could try out simple trajectory physics by setting the velocity vector and seeing how it goes.

EDIT: I realised my earlier explaination might end up troublesome if we ever decide to move on to rotations.

  • Log in or register to post comments
LeeZH
joined 14 years 10 months ago
Saturday, March 17, 2012 - 04:16
LeeZH's picture

The second thing I want to get into before bouncing is the concept of normals. The name itself is a bit confusing, but essentially, it's a vector that represents the direction the face of a surface is pointing at.

EDIT: Meh, got some work dumped on me. Will finish this later. Don't worry, I'll get there eventually.

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Saturday, March 17, 2012 - 04:24

i know a thing or two about vector maths, in fact this night i discussed with a team mate the use of atan2 for point-at-target trajectory, and we were successful on that. the engine has a good support for vector maths and trigonometry.

 

the sticky physics are only applied to the player. other objects have only gravity, when called. and these are the ones i want to give a new life. as for the player, in physics, it behaves just like i idealized.

i also prefer a good looking illusion than an over-calculated precision. not that i don't want any precision, but the more time saved in development and in cpu-time, the best. again, a good balance of both can be achieved (for example, i don't need to calculate the rotation of a moving body in the air, i'll just rotate it by the speed it moves)

 

i will study the info you passed on, and try to build something with it during the weekend. thanks again :)

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Saturday, March 17, 2012 - 04:27

i'm familiar with normals. i have worked with 3d and know the concept pretty well. normal map support is on my wishlist for future engine features, when it moves to hardware acceleration.

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
LeeZH
joined 14 years 10 months ago
Saturday, March 17, 2012 - 04:49
LeeZH's picture

Ah, didn't notice you've posted while I was trying to ninja-edit my stuff. :P

Yeah, you might want to go over my changes.

To be honest, all that stuff about shaving CPU time off is no longer applicable because that was back in the days when 300MHz was amazing. I went over it so that you would know why those older games were done that way.

Also, calculating individual forces and adding them up would really help in the long run, because there is so many things you can add to it, like lift, drag, magnetic pull, collisions, etc. and the system would still work.

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Saturday, March 17, 2012 - 04:59

the engine's minimum specs are p3 processors or similar, i didn't want to go too much over that, and already did. the engine creator reported that his notebook can't run my game at full speed, cpu intel atom (?).i had other reports of people with older machines getting slow performances, and i try to shave as much as possible, without sacrificing too much.

 

the fact that hardware drawing is not supported it probably the major source of cpu overhead. the engine was designed for maximum compatibility, but i have a habit of pushing it to higher specs. It was never designed to host this kind of game in the first place, but it's doing it pretty well and i'd like to keep going.

 

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Wednesday, March 28, 2012 - 20:34

i managed to create the base of what i needed to implement such physics: movement based on angle.

 

provided i give an angle value and a speed value it will move it accordingly, as well as rotate it correctly.

 

xspeed = (-sin(deg2rad(movement_angle))*speed)*player_direction"

yspeed = -cos(deg2rad(movement_angle))*speed

 

with this i was able to make an aim based projectile system, rather than the earlier 6-direction system.

 

also smart shots that look to their target.

 

now i have another problem... how would i translate a circular trajectory into x/y speeds?

i want to make the aim arc around a 180º angle in front of the player, and the best i've managed is one that moves in 8 directions.

i've read about a constant velocity vector, and an angular speed vector, one would move the object straight in the angle it is facing, while the other would force rotation at a constant rate.

i'm not too good in maths so it's hard for me to know and memorize the meaning of some symbols that i've seen, so if this could be broken down into simple arithmetics i'd be much appreciated.

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
MoikMellah
joined 14 years 1 month ago
Thursday, March 29, 2012 - 01:12
MoikMellah's picture

I'm assuming 'circular trajectory' means 'moving in a circle around a fixed point'?  Correct me if my first assumption is off.

If that's the case, you don't need separate x and y speeds, really - just the velocity that the object is traveling around the circle.  You should be able to do something like this:

oneRadian = hypotenuse(y,x);
angleInRadians = atan2(y,x);
newAngleInRadians = angleInRadians + (velocity / oneRadian);

..And then convert back to x/y coords from there. Not complete code by any means, but hopefully it'll give you a starting point.

-mm

My project: Bits & Bots

  • Log in or register to post comments
LeeZH
joined 14 years 10 months ago
Thursday, March 29, 2012 - 06:02
LeeZH's picture

Is it just me or did my really lengthy post on bouncing that I posted a while back just disappear? D:

Also, just asking: By circular trajectory did you actually mean this (parabolic trajectory)? Implementing a parabolic trajectory is simpler and faster since sin/cos functions are relatively slow functions.

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Thursday, March 29, 2012 - 12:43

@MoikMellah

 

exactly that, thank you. i'll try that.

 

@LeeZH

that is also interesting, and will probably solve my problems with bounces

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
LeeZH
joined 14 years 10 months ago
Thursday, March 29, 2012 - 14:33
LeeZH's picture

What you could do for parabolic path is have your projectile set up like this:

pos_x = 0
pos_y = 0
vel_x = - sin(deg2rad(aim_angle)) * speed * player_direction
vel_y = - cos(deg2rad(aim_angle)) * speed

And then while the game is running you have it run like this:

vel_y = vel_y + gravity * delta_time
pos_x = pos_x + vel_x * delta_time
pos_y = pos_y + vel_y * delta_time
if pos_y < ground_level then
     pos_y = 0
     vel_y = - vel_y * elasticity

Where the elasticity of an object is how bouncy it is, between 0 and 1. For instance, a tennis ball should be around 0.7.

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Thursday, March 29, 2012 - 19:37

@ MoikMellah

 

totally unrelated question:

I read a bit through your site and noticed you wanted to develop a MetroidVania engine. Any chance you (or anyone with coding skills) could do a crossover with the engine i'm using? It's made in C and Allegro 4, , and the lack of hardware support really drives me crazy sometimes, like being stuck with 320*240 of internal resolution (meeeeh).

 

I ask you this because it is on a CC-BY-SA-3.0 license, full fledged platformer engine with the added perks of optional 360º physics, and a very easy scripting API. The perfect engine for beginner to intermediate developers. HW support would suit it for experienced developers as well.

My game is itself, a MV spin-off, since you don't get a map, but there are linked areas, rooms, RPG system, fighting and of course platforming goodness. The concept of levels and phases is pretty much nonexistent .

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments
Fracture
joined 13 years 10 months ago
Wednesday, February 6, 2013 - 10:06

double posting on a possibly defunct thread :p

YES BOX2D!!!

using Construct 2 now, my problems with physics are over. thank you all for your time :)

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/

  • Log in or register to post comments