Jump to content
  • Advertisement
Sign in to follow this  
Thanamos

Distance to obstacle in flash

This topic is 2615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

tl;dr;
How can I find the edge of an object in flash when I know the origin and a movement vector?
I am making a sideways scroller in flash and for the first time I am trying to create a tiny bit more advanced physics then usual.

Usually this would be the onTick event:

GetPlayerPositionRectangle();
AdjustRectangleBasedOnKeysPressed(); // eg. If key.LEFT is pressed, decrease rectangle.x by a constant speed
if (WillNewRectangleFitInTheWorld())
MovePlayerToNewPosition


This works fine for having a constant speed, but say that the speed is 4 pixels per frame, if the wall is 3 pixels away this will fail and the players will never be able to get closer to the wall, this looks really bad.

So I want to add another line to the function, and it's there im facing a design question.

I want to write:
GetPlayerPositionRectangle();
GetDirectionVectorBasedOnKeysPressed(); //This will allow me to add gravity and other stuff
AdjustLengthOfVectorBasedOnTimeSinceLastFrame();
ForEveryCornerOfTheRectangleGetDistanceToObstableAccordingToVector();
MoveXYBasedOnDistanceToObstaclesLimitedBySpeed();

This is a better solution for more reasons, I can have a better boundingbox on the moving object.
The moving object can move closer to obstacles,


But the real issue here is getting the distance to the edge of an object based on a direction arrow, here is a small illustration:
[attachment=2038:physics.png]

(Black boxes are obstacles, red box is bounding box, grey arrow is the movement vector, green line is where the point I need to find)

I am fairly certain that this has been asked before, but Im not sure what to search for to get answers, so here goes.

Share this post


Link to post
Share on other sites
Advertisement
You might check out this [Edit: Formatting isn't working correctly on the forums right now, but that's a link] article. Also, search the forum archives for e.g. 'platformer collision detection' or 'platformer sat'.

Depending on your needs, you could also consider using an existing physics engine (I believe there's a Flash port of Box2D, for example).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!