davidyu / Sonar

Networked multiplayer SHMUP-lite
http://lewenyu.com/sonar
MIT License
0 stars 0 forks source link

Implement unidirectional bouncing sonar #9

Closed davidyu closed 10 years ago

davidyu commented 10 years ago

Define a maximum distance for this unidirectional sonar. Use mouse to aim.

Thoughts on implementation:

Use the point of origin of sonar and the cursor's position to define a ray. Do line-line intersections. If it hits an edge, create an incident ray and continue to do line-line intersections for that ray. Keep going until the max distance is exhausted. Return the ordered list/array of line segments that define the path of this sonar.

Followup: implement aim cursor

davidyu commented 10 years ago

Here's a simpler implementation:

  1. construct two points that are x distance apart (x is the length of the sonar representation; so when we connect these two points it becomes a line representing the unidirectional sonar).
  2. shoot them in the same direction. The leading point is shot first. After it travels x distance, the trailing point is shot from the same location. Alternatively, shoot both at the same time, with the leading point x length away from the trailing point in the initial direction.
  3. if at any time a point collides with a wall, its velocity is changed so it bounces off the wall. Leave a point at the point of collision at the wall until the trailing point arrives and bounces off the wall. This means we can have many points if the sonar is bouncing off a very complex nook of the map.
  4. connect all points.

Question: after the leading point travels some distance (let's say y), it fades or expires. Does the trailing point follow -- IE: it travels y-x distance, or does it keep going and travel a total of y distance as well?

Answer: use timed effect and expire all points at the end of some time! Perfect.

davidyu commented 10 years ago

Overall requirements:

  1. A component that allows me to keep track of a collection of ordered points (which are entities with pos-ish cmps) See bitmap-based optimization below.
  2. A rendering system that tracks a collection of points and connects the right ones See bitmap-based optimization below: just need to track a single point.
  3. A physics-ish system that bounces moving points and creates new ones and removes expired ones (by expired I mean points which are behind the trailing point)

Solutions

  1. Add a new component, BounceCmp to mark an object that ricochets off walls See bitmap based optimization below
  2. Reuse RenderTraceSys or create a new render system? In 3ad6eb9417a18d95b41bd001dc866880ca454770 we use a separate RenderTrailSys.
  3. Hijack PhysicsSys for bouncing? Done in 28d468ab698043e935d7f4c925929675ecd9ad89.

Bitmap blending optimization

Instead of connecting and maintaining multiple points, use Flash's bitmap and blending functionality to simulate the appearance of a trail.

How to actually do this?

  1. Naive method (done in 28d468ab698043e935d7f4c925929675ecd9ad89):

    Draw the point onto a bitmap (maybe a circle with a small 2-pixel radius) - tried in 3ad6eb9417a18d95b41bd001dc866880ca454770, this still leaves gaps Next time I draw, multiply the alpha of all pixels on the bitmap by 0.9 (apply a ColorTransform) Draw the point. And so on and so forth.

  2. With Brensenham's line-drawing algorithm to fill in the gaps (done with c6afa2f6bf1e7408c034c297e9c3d3ae96a1c910 and 1f03d66ea47ed47b6531962d1b366704d7d43a25):

    Draw a line between the current pos and the previous pos using Brensenham.

    • We do this because it's probably better to use setPixels (probably) than blasting the whole bitmap by drawing it on a vector sprite and copying everything onto the bitmap.

Pros: much simpler to implement Cons: some annoyances with a proper camera. Bitmap sprites are noticeably more memory intensive so we cannot create a bitmap as large as the entire world. Instead, we create one that fits the viewport/screen and move that and the contents within it as the camera changes.

davidyu commented 10 years ago

Clean up work: